ws

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 23 Imported by: 3

Documentation

Overview

Package ws provides a simple web server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotModified      = NewError(http.StatusNotModified)
	ErrBadRequest       = NewError(http.StatusBadRequest)
	ErrUnauthorized     = NewError(http.StatusUnauthorized)
	ErrForbidden        = NewError(http.StatusForbidden)
	ErrNotFound         = NewError(http.StatusNotFound)
	ErrMethodNotAllowed = NewError(http.StatusMethodNotAllowed)
	ErrTooManyRequests  = NewError(http.StatusTooManyRequests)
)

Errors.

Functions

func Compress added in v0.1.1

func Compress(data []byte) ([]byte, error)

Compress compresses the data.

func RealIP

func RealIP(r *http.Request) (netip.Addr, error)

RealIP returns the real ip of the client.

Types

type Checker

type Checker interface {
	Check() error
}

Checker represents a model checker.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents a custom error.

func Errorf

func Errorf(format string, vs ...any) *Error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

func NewError

func NewError(code int, vs ...any) *Error

NewError creates a new custom error by http code.

func (*Error) Error

func (a *Error) Error() string

Error implements the error interface.

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router represents a router.

func (*Router) AddCreator added in v0.1.1

func (a *Router) AddCreator(creator di.Creator) *Router

AddCreator adds the creator.

func (*Router) AddScoped added in v0.1.1

func (a *Router) AddScoped(constructor any, itype any) *Router

AddScoped adds a scoped service.

func (*Router) AddShared added in v0.1.1

func (a *Router) AddShared(constructor any, itype any) *Router

AddShared adds a shared service.

func (*Router) AddValue added in v0.1.1

func (a *Router) AddValue(value any, itype any) *Router

AddValue adds a value service.

func (*Router) Invoke added in v0.1.1

func (a *Router) Invoke(function any) (di.Result, error)

Invoke runs the given function after instantiating its dependencies.

func (*Router) Redirect

func (a *Router) Redirect(p string, url string) *Router

Redirect redirects key to p.

func (*Router) Register

func (a *Router) Register(p string, rcvr any, methods ...string) *Router

Register registers the receiver's methods.

func (*Router) RegisterData

func (a *Router) RegisterData(p string, data []byte, modtime time.Time) *Router

RegisterData registers the data.

func (*Router) RegisterFS

func (a *Router) RegisterFS(p string, fsys fs.FS) *Router

RegisterFS registers the fs.

func (*Router) RegisterFunc

func (a *Router) RegisterFunc(p string, fn any, methods ...string) *Router

RegisterFunc registers the function handler.

func (*Router) Route

func (a *Router) Route(p string) *Router

Route returns the sub router.

type Server

type Server struct {
	*Router
	// contains filtered or unexported fields
}

Server represents a web server.

func New

func New(injector *di.Injector) *Server

New creates a new server.

func (*Server) Addr

func (a *Server) Addr(addr string) *Server

Addr optionally specifies the TCP address for the server to listen on.

func (*Server) Config

func (a *Server) Config(fn func(s *http.Server)) *Server

Config configures the raw http.Server.

func (*Server) IdleTimeout

func (a *Server) IdleTimeout(d time.Duration) *Server

IdleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled.

func (*Server) Limiter added in v0.0.2

func (a *Server) Limiter(freq int, dur time.Duration, cap int) *Server

Limiter limits the request frequency.

func (*Server) Logger

func (a *Server) Logger(logger *log.Logger) *Server

Logger optionally specifies the logger.

func (*Server) MaxBodyBytes

func (a *Server) MaxBodyBytes(n int) *Server

MaxBodyBytes controls the maximum number of bytes the decoder will read the body.

func (*Server) MaxHeaderBytes

func (a *Server) MaxHeaderBytes(n int) *Server

MaxHeaderBytes controls the maximum number of bytes the server will read parsing the request header's keys and values.

func (*Server) ReadHeaderTimeout

func (a *Server) ReadHeaderTimeout(d time.Duration) *Server

ReadHeaderTimeout is the amount of time allowed to read request headers.

func (*Server) ReadTimeout

func (a *Server) ReadTimeout(d time.Duration) *Server

ReadTimeout is the maximum duration for reading the entire request, including the body.

func (*Server) ServeHTTP

func (a *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP should write reply headers and data to the ResponseWriter and then return.

func (*Server) Start

func (a *Server) Start(ctx context.Context, wg *sync.WaitGroup) error

Start starts the server.

func (*Server) TLS

func (a *Server) TLS(certfile, keyfile string) *Server

TLS sets the certfile and keyfile for TLS.

func (*Server) WriteTimeout

func (a *Server) WriteTimeout(d time.Duration) *Server

WriteTimeout is the maximum duration before timing out writes of the response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL