Documentation ¶
Index ¶
- Variables
- func ErrorRedirectTo(status int, redirect string) error
- func NewFormCodec() *codec
- func NewJSONCodec() *codec
- func NewLogger(logger *slog.Logger, keys []fmt.Stringer) *slog.Logger
- func NewQueryCodec() *queryCodec
- func NewURLParamCodec() *urlParamCodec
- func URLParam[Reg any](ctx Context[Reg], name string) string
- func WrapErrorWithStatus(status int, err error) error
- type AccessLogger
- type Codec
- type CodecList
- type Context
- type ContextFactory
- type Decoder
- type DecoderFunc
- type DefaultContextFactory
- type DeferDoTiming
- type DeferFunc
- type DeferFuncCallerStack
- type DeferFuncError
- type Encoder
- type EncoderFunc
- type ErrCodecDecode
- type ErrCodecEncode
- type ErrorBody
- type ErrorHooker
- type ErrorMessage
- type ErrorWithRedirect
- type ErrorWithStatus
- type Handler
- type HandlerFunc
- type ListenAndServeOption
- type RawBodyCodec
- type Router
- func (r *Router[Reg]) Connect(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Delete(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Get(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Head(pattern string, h Handler[Reg])
- func (r *Router[Reg]) ListenAndServe(ctx gocontext.Context, addr string, opts ...ListenAndServeOption) error
- func (r *Router[Reg]) MethodNotAllowed(h Handler[Reg])
- func (r *Router[Reg]) Mount(pattern string, h http.Handler)
- func (r *Router[Reg]) NotFound(h Handler[Reg])
- func (r *Router[Reg]) Options(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Patch(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Post(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Put(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Route(pattern string, fn func(r *Router[Reg])) *Router[Reg]
- func (r *Router[Reg]) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router[Reg]) Trace(pattern string, h Handler[Reg])
- func (r *Router[Reg]) Use(middlewares ...func(http.Handler) http.Handler)
- func (r *Router[Reg]) With(middlewares ...func(http.Handler) http.Handler) *Router[Reg]
- type RouterOption
- func WithAccessLogger[Reg any](al AccessLogger) RouterOption[Reg]
- func WithChiRouter[Reg any](cr chi.Router) RouterOption[Reg]
- func WithCodec[Reg any](codec Codec) RouterOption[Reg]
- func WithContextFactory[Reg any](cf ContextFactory[Reg]) RouterOption[Reg]
- func WithDefaultMiddleware[Reg any](middlewares ...func(http.Handler) http.Handler) RouterOption[Reg]
- func WithErrorHooker[Reg any](eh ErrorHooker) RouterOption[Reg]
- func WithLogger[Reg any](logger *slog.Logger) RouterOption[Reg]
- type Transformer
- type Validatable
- type ValidateError
- type WrapResponseWriter
Constants ¶
This section is empty.
Variables ¶
var ( ErrRequestNotSupportedAtThisCodec = errors.New("request not supported at this codec") ErrRequestContinueDecode = errors.New("request continue decode") ErrResponseNotSupportedAtThisCodec = errors.New("response not supported at this codec") DefaultCodecList = CodecList{ NewURLParamCodec(), NewQueryCodec(), NewFormCodec(), NewJSONCodec(), NewRawBodyCodec(), &nopCodec{}, } )
Functions ¶
func ErrorRedirectTo ¶ added in v0.4.0
func NewFormCodec ¶
func NewFormCodec() *codec
NewFormCodec returns a new FormCodec. This codec supports request decoding only. The content type header of the request is application/x-www-form-urlencoded. If you want to use this codec, you need to set the struct field tag like a `form:"name"`.
func NewJSONCodec ¶
func NewJSONCodec() *codec
NewJSONCodec returns a new JSONCodec. This codec supports request and response encoding and decoding. The content type header of the request is application/json and */*, and the content type of the response is application/json.
func NewLogger ¶ added in v0.2.0
NewLogger returns a new logger with the given logger. This logger output with the informwation with request ID. If the given logger is nil, it returns use the default logger. keys is the whitelist of keys that use read from context.Context.
func NewQueryCodec ¶
func NewQueryCodec() *queryCodec
NewQueryCodec returns a new QueryCodec. This codec supports request decoding only. If you want to query parameter that like a /hello?name=world, you can set the struct field tag like a `query:"name"`.
func NewURLParamCodec ¶
func NewURLParamCodec() *urlParamCodec
NewURLParamCodec returns a new URLParamCodec. This codec supports request decoding only. If you want to url parameter that like a /hello/{name}, you can set the struct field tag like a `urlparam:"name"`.
func WrapErrorWithStatus ¶
Types ¶
type AccessLogger ¶ added in v0.2.0
type Codec ¶
type Codec interface { Name() string Decode(r *http.Request, v any) error Encode(w http.ResponseWriter, r *http.Request, v any) error }
Codec is a interface for encoding and decoding request and response.
type CodecList ¶
type CodecList []Codec
CodecList is list of Codec. This codec process the request and response in order.
type Context ¶
type Context[Reg any] interface { gocontext.Context Request() *http.Request Response() http.ResponseWriter Registry() Reg Defer(fn DeferFunc, priority ...DeferDoTiming) DeferDo(priority DeferDoTiming) error }
type ContextFactory ¶
type ContextFactory[Reg any] interface { Build(w http.ResponseWriter, req *http.Request) (Context[Reg], error) }
func NewContextHookFactory ¶
func NewContextHookFactory[Reg any](fn func(w http.ResponseWriter, req *http.Request) (Reg, error)) ContextFactory[Reg]
type DecoderFunc ¶
type DefaultContextFactory ¶
type DefaultContextFactory[Reg any] struct { // contains filtered or unexported fields }
func (*DefaultContextFactory[Reg]) Build ¶
func (d *DefaultContextFactory[Reg]) Build(w http.ResponseWriter, req *http.Request) (Context[Reg], error)
type DeferDoTiming ¶ added in v0.2.0
type DeferDoTiming int
const ( DeferDoTimingBeforeResponse DeferDoTiming = iota DeferDoTimingAfterResponse )
type DeferFuncCallerStack ¶ added in v0.2.0
func (*DeferFuncCallerStack) String ¶ added in v0.2.0
func (d *DeferFuncCallerStack) String() string
type DeferFuncError ¶ added in v0.2.0
type DeferFuncError struct { Err error Caller *DeferFuncCallerStack }
func (*DeferFuncError) Error ¶ added in v0.2.0
func (d *DeferFuncError) Error() string
func (*DeferFuncError) Unwrap ¶ added in v0.2.0
func (d *DeferFuncError) Unwrap() error
type EncoderFunc ¶
type ErrCodecDecode ¶
type ErrCodecDecode struct {
// contains filtered or unexported fields
}
func (*ErrCodecDecode) Error ¶
func (e *ErrCodecDecode) Error() string
func (*ErrCodecDecode) Unwrap ¶
func (e *ErrCodecDecode) Unwrap() error
type ErrCodecEncode ¶
type ErrCodecEncode struct {
// contains filtered or unexported fields
}
func (*ErrCodecEncode) Error ¶
func (e *ErrCodecEncode) Error() string
func (*ErrCodecEncode) Unwrap ¶
func (e *ErrCodecEncode) Unwrap() error
type ErrorHooker ¶
type ErrorMessage ¶
type ErrorMessage struct {
Error ErrorBody `json:"error"`
}
type ErrorWithRedirect ¶ added in v0.4.0
type ErrorWithStatus ¶
type Handler ¶
type Handler[Reg any] interface { // contains filtered or unexported methods }
func NewHandler ¶
func NewHandler[Req any, Res any, Reg any](h HandlerFunc[Req, Res, Reg]) Handler[Reg]
type ListenAndServeOption ¶ added in v0.3.0
type ListenAndServeOption func(*listenAndServeConfig)
func WithDisableTanukiupProxy ¶ added in v0.3.0
func WithDisableTanukiupProxy() ListenAndServeOption
func WithNoSetDefaultLogger ¶ added in v0.3.1
func WithNoSetDefaultLogger() ListenAndServeOption
func WithShutdownTimeout ¶ added in v0.3.0
func WithShutdownTimeout(d time.Duration) ListenAndServeOption
type RawBodyCodec ¶ added in v0.4.0
type RawBodyCodec struct{}
RawBodyCodec is a codec that reads the request body as is.
func NewRawBodyCodec ¶ added in v0.4.0
func NewRawBodyCodec() *RawBodyCodec
func (*RawBodyCodec) Decode ¶ added in v0.4.0
func (r *RawBodyCodec) Decode(req *http.Request, v any) error
func (*RawBodyCodec) Encode ¶ added in v0.4.0
func (r *RawBodyCodec) Encode(w http.ResponseWriter, req *http.Request, v any) error
func (*RawBodyCodec) Name ¶ added in v0.4.0
func (r *RawBodyCodec) Name() string
type Router ¶
type Router[Reg any] struct { // contains filtered or unexported fields }
func NewRouter ¶
func NewRouter[Reg any](reg Reg, opts ...RouterOption[Reg]) *Router[Reg]
NewRouter creates a new Router.
The registry is used to create a context.
func RouteWithTransformer ¶
func (*Router[Reg]) ListenAndServe ¶ added in v0.3.0
func (r *Router[Reg]) ListenAndServe(ctx gocontext.Context, addr string, opts ...ListenAndServeOption) error
ListenAndServe starts the server. If the context is canceled, the server will be shutdown.
func (*Router[Reg]) MethodNotAllowed ¶
type RouterOption ¶
func WithAccessLogger ¶ added in v0.2.0
func WithAccessLogger[Reg any](al AccessLogger) RouterOption[Reg]
func WithChiRouter ¶
func WithChiRouter[Reg any](cr chi.Router) RouterOption[Reg]
func WithCodec ¶
func WithCodec[Reg any](codec Codec) RouterOption[Reg]
func WithContextFactory ¶
func WithContextFactory[Reg any](cf ContextFactory[Reg]) RouterOption[Reg]
func WithDefaultMiddleware ¶ added in v0.2.0
func WithErrorHooker ¶
func WithErrorHooker[Reg any](eh ErrorHooker) RouterOption[Reg]
func WithLogger ¶ added in v0.2.0
func WithLogger[Reg any](logger *slog.Logger) RouterOption[Reg]
type Transformer ¶
func NewTransformer ¶
func NewTransformer[Reg1 any, Reg2 any](fn func(ctx Context[Reg1]) (Reg2, error)) Transformer[Reg1, Reg2]
type Validatable ¶
type Validatable interface {
Validate() error
}
type ValidateError ¶
type ValidateError struct {
// contains filtered or unexported fields
}
func (*ValidateError) Error ¶
func (v *ValidateError) Error() string
func (*ValidateError) Status ¶
func (v *ValidateError) Status() int
func (*ValidateError) Unwrap ¶
func (v *ValidateError) Unwrap() error
type WrapResponseWriter ¶ added in v0.2.0
type WrapResponseWriter interface { http.ResponseWriter Status() int BytesWritten() int }