Documentation ¶
Index ¶
- Constants
- Variables
- func NewContext(conf ContextConfig) *context
- func WithAddress(address int) routerOptionFunc
- func WithBodyReader(reader bodyReaderFn) routerOptionFunc
- func WithContext(ctx ctxpkg.Context) routerOptionFunc
- func WithDefaultStatusCode(statusCode int) routerOptionFunc
- func WithEmptyTreeHandler(handler HandlerFunc) routerOptionFunc
- func WithMaxBodySize(size int64) routerOptionFunc
- func WithMiddlewaresEnabled(areEnabled bool) routerOptionFunc
- func WithNotFoundHandler(h HandlerFunc) routerOptionFunc
- func WithOptionsHandler(h HandlerFunc) routerOptionFunc
- func WithPanicHandler(h PanicHandlerFunc) routerOptionFunc
- func WithServerName(name string) routerOptionFunc
- type Context
- type ContextConfig
- type ContextKey
- type File
- type HandlerFunc
- type Logger
- type Middleware
- type MiddlewareFunc
- type MiddlewareMatcherFunc
- type MiddlewareOptionFunc
- type PanicHandlerFunc
- type Route
- type Router
Constants ¶
const ( JsonContentType string = "application/json" JsonContentTypeUTF8 string = JsonContentType + "; charset=UTF-8" TextHtmlContentType string = "text/html" MultiPartFormContentType string = "multipart/form-data" )
const ( InfoLogLevel logTypeValue = 1 << iota WarningLogLevel ErrorLogLevel )
const ( MiddlewarePreRunner middlewareType = "preRunner" MiddlewarePostRunner middlewareType = "postRunner" )
Variables ¶
var (
ErrCtNotMultipart = errors.New("the content-type is not multipart/form-data")
)
Functions ¶
func NewContext ¶ added in v1.1.0
func NewContext(conf ContextConfig) *context
NewContext creates and returns a new context.
func WithAddress ¶
func WithAddress(address int) routerOptionFunc
WithAddress allows to configure address of the router where it will be listening.
func WithBodyReader ¶
func WithBodyReader(reader bodyReaderFn) routerOptionFunc
WithBodyReader allows to configure a default body reader function or disable it (by passing in <nil>).
func WithContext ¶
WithContext allows to configure basecontext of the router which will be passed to each and every handler.
func WithDefaultStatusCode ¶
func WithDefaultStatusCode(statusCode int) routerOptionFunc
WithDefaultStatusCode allows to configure the default statusCode of the response without specifying it explicitly.
func WithEmptyTreeHandler ¶ added in v1.2.0
func WithEmptyTreeHandler(handler HandlerFunc) routerOptionFunc
WithEmptyTreeHandler allows to configure the handler in case of an empty method tree event.
func WithMaxBodySize ¶
func WithMaxBodySize(size int64) routerOptionFunc
WithMaxBodySize allows to configure maximum size incoming, decodable formdata.
func WithMiddlewaresEnabled ¶ added in v1.2.0
func WithMiddlewaresEnabled(areEnabled bool) routerOptionFunc
WithMiddlewaresEnabled allows to configure the state of the middlewares.
func WithNotFoundHandler ¶
func WithNotFoundHandler(h HandlerFunc) routerOptionFunc
WithNotFoundHandler allows to configure 404 handler of the router.
func WithOptionsHandler ¶
func WithOptionsHandler(h HandlerFunc) routerOptionFunc
WithOptionsHandler allows to configure OPTIONS method handler of the router.
func WithPanicHandler ¶
func WithPanicHandler(h PanicHandlerFunc) routerOptionFunc
WithPanicHandler allows to configure a recover function which is called if a panic happens somewhere.
func WithServerName ¶
func WithServerName(name string) routerOptionFunc
WithServerName allows to configure the server name of the instance.
Types ¶
type Context ¶
type Context interface { Logger // ---- Methods about the Context itself. Reset(http.ResponseWriter, *http.Request) Empty() GetContextId() uint64 // ---- Request GetRequest() *http.Request GetRequestMethod() string GetUrl() string GetCleanedUrl() string GetQueryParams() url.Values GetQueryParam(string) string BindValue(ContextKey, any) GetBindedValue(ContextKey) any GetRequestHeader(string) string GetContentType() string GetParam(string) string GetParams() pathParams GetRequestHeaders() http.Header GetBody() []byte ParseForm() error GetFormFile(string) (File, error) GetFormValue(string) (string, error) // ---- Response SendJson(anyValue, ...int) SendNotFound() SendInternalServerError() SendMethodNotAllowed() SendOk() SendRaw([]byte, int, http.Header) Pipe(*http.Response) SetStatusCode(int) WriteResponse(b []byte) AppendHttpHeader(header http.Header) WriteToResponseNow() Copy(io.Reader) GetLog() *contextLog }
type ContextConfig ¶ added in v1.1.0
type ContextKey ¶ added in v1.0.3
type ContextKey string
type HandlerFunc ¶
type HandlerFunc func(Context)
type Middleware ¶
type Middleware interface { DoesMatch(Context) bool Execute(Context, HandlerFunc) IsAlwaysAllowed() bool }
func NewMiddleware ¶
func NewMiddleware(handler MiddlewareFunc, opts ...MiddlewareOptionFunc) Middleware
NewMiddleware creates and returns a new middleware based upon the given MiddlewareFunc and matchers.
type MiddlewareFunc ¶
type MiddlewareFunc func(Context, HandlerFunc)
type MiddlewareMatcherFunc ¶
type MiddlewareOptionFunc ¶ added in v1.2.0
type MiddlewareOptionFunc func(*middleware)
func MiddlewareWithAlwaysAllowed ¶ added in v1.2.0
func MiddlewareWithAlwaysAllowed(isAlwaysAllowed bool) MiddlewareOptionFunc
MiddlewareWithAlwaysAllowed configures, whether a middleware should run even if the middlewares are globally disallowed.
func MiddlewareWithMatchers ¶ added in v1.2.0
func MiddlewareWithMatchers(matchers ...MiddlewareMatcherFunc) MiddlewareOptionFunc
MiddlewareWithMatchers allows to configure the matchers for a given middleware.
type PanicHandlerFunc ¶
type PanicHandlerFunc func(Context, interface{})
type Route ¶
type Route interface { RegisterMiddlewares(...MiddlewareFunc) Route GetUrl() string // contains filtered or unexported methods }
type Router ¶
type Router interface { ServeHTTP(http.ResponseWriter, *http.Request) Serve(Context) ListenWithContext(ctxpkg.Context) Listen() RegisterMiddlewares(middlewares ...Middleware) RegisterPostMiddlewares(middlewares ...Middleware) // All the available methods to register: Get(string, HandlerFunc) Route Post(string, HandlerFunc) Route Put(string, HandlerFunc) Route Delete(string, HandlerFunc) Route Head(string, HandlerFunc) Route }