Documentation ¶
Index ¶
- Constants
- Variables
- func ResponseHeaderFromCtx(ctx context.Context) http.Header
- type Application
- func (app *Application) DELETE(path string, h Handler, opts ...RouteOption)
- func (app *Application) GET(path string, h Handler, opts ...RouteOption)
- func (app *Application) Group(prefix string, opts ...RouteOption) *Group
- func (app *Application) PATCH(path string, h Handler, opts ...RouteOption)
- func (app *Application) POST(path string, h Handler, opts ...RouteOption)
- func (app *Application) PUT(path string, h Handler, opts ...RouteOption)
- func (app *Application) Run() error
- type CORSConfig
- type CustomHTTPResponse
- type Decoder
- type Encoder
- type ErrorHandler
- type Group
- func (g *Group) DELETE(path string, h Handler, opts ...RouteOption)
- func (g *Group) GET(path string, h Handler, opts ...RouteOption)
- func (g *Group) PATCH(path string, h Handler, opts ...RouteOption)
- func (g *Group) POST(path string, h Handler, opts ...RouteOption)
- func (g *Group) PUT(path string, h Handler, opts ...RouteOption)
- type GzipConfig
- type HTTPError
- type Handler
- type Logger
- type Middleware
- type Option
- type OptionFn
- type Request
- type RouteOption
- type RouteOptionFn
Constants ¶
const ( HeaderAcceptEncoding = "Accept-Encoding" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlMaxAge = "Access-Control-Max-Age" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderOrigin = "Origin" HeaderVary = "Vary" )
Headers
Variables ¶
var DefaultCORSConfig = CORSConfig{ AllowOrigins: []string{"*"}, AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete}, }
DefaultCORSConfig is the default configuration for the WithCORS middleware.
var ( // DefaultGzipConfig is the default config for Gzip middleware. DefaultGzipConfig = GzipConfig{ Level: gzip.DefaultCompression, } )
Functions ¶
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application is a web application.
func Default ¶
func Default() *Application
Default returns an Application with a default set of configurations.
func (*Application) DELETE ¶
func (app *Application) DELETE(path string, h Handler, opts ...RouteOption)
DELETE registers a new DELETE route for a path with handler.
func (*Application) GET ¶
func (app *Application) GET(path string, h Handler, opts ...RouteOption)
GET registers a new GET route for a path with handler.
func (*Application) Group ¶
func (app *Application) Group(prefix string, opts ...RouteOption) *Group
Group creates a group of sub-routes
func (*Application) PATCH ¶
func (app *Application) PATCH(path string, h Handler, opts ...RouteOption)
PATCH registers a new PATCH route for a path with handler.
func (*Application) POST ¶
func (app *Application) POST(path string, h Handler, opts ...RouteOption)
POST registers a new POST route for a path with handler.
func (*Application) PUT ¶
func (app *Application) PUT(path string, h Handler, opts ...RouteOption)
PUT registers a new PUT route for a path with handler.
type CORSConfig ¶
type CORSConfig struct { AllowOrigins []string AllowMethods []string AllowHeaders []string AllowCredentials bool MaxAge int }
CORSConfig defines the config for WithCORS middleware.
type CustomHTTPResponse ¶
type CustomHTTPResponse interface {
WriteTo(w http.ResponseWriter)
}
CustomHTTPResponse defines an interface to support custom HTTP response.
type Decoder ¶
type Decoder interface { // Decode decodes a request to a struct. req.PostForm is called in advanced. Decode(obj interface{}, req *http.Request) error }
Decoder defines a request decoder.
type Encoder ¶
type Encoder interface { // Encode encodes obj and writes to http.ResponseWriter. Encode(w http.ResponseWriter, obj interface{}) error }
Encoder define a request decoder.
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, err error)
ErrorHandler defines a handler which handles error.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a group of sub-routes. It can be used for a group of routes which share same middlewares.
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, h Handler, opts ...RouteOption)
DELETE registers a new DELETE route for a path with handler.
func (*Group) GET ¶
func (g *Group) GET(path string, h Handler, opts ...RouteOption)
GET registers a new GET route for a path with handler.
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, h Handler, opts ...RouteOption)
PATCH registers a new PATCH route for a path with handler.
type GzipConfig ¶
type GzipConfig struct { // Gzip compression level. // Optional. Default value -1. Level int }
GzipConfig defines the config for Gzip middleware.
type HTTPError ¶
HTTPError is a simple implementation of HTTP Error.
func (*HTTPError) WriteTo ¶ added in v0.3.0
func (err *HTTPError) WriteTo(w http.ResponseWriter)
WriteTo implements CustomHTTPResponse. It encodes the response as JSON format.
type Logger ¶
type Logger interface {
// Println prints out logs like fmt.Println.
Println(...interface{})
}
Logger defines a Logger.
type Middleware ¶
Middleware defines a middleware to provide additional logic.
func WithCORS ¶
func WithCORS(cfg CORSConfig) Middleware
WithCORS returns a middleware to support Cross-Origin Resource Sharing.
func (Middleware) ApplyRoute ¶
func (m Middleware) ApplyRoute(r *route)
ApplyRoute implements RouteOption.
type Option ¶
type Option interface {
Apply(app *Application)
}
Option defines an application Option.
type OptionFn ¶
type OptionFn func(app *Application)
OptionFn defines a function that implements Option
func WithLogger ¶
WithLogger specifies a custom Logger for tha application.
type Request ¶
type Request interface { // HTTPRequest returns the http.Request. HTTPRequest() *http.Request // Decode decodes the request to an object. Decode(obj interface{}) error }
Request defines a HTTP request.
type RouteOption ¶
type RouteOption interface {
ApplyRoute(r *route)
}
RouteOption defines an option to customize a route.
type RouteOptionFn ¶
type RouteOptionFn func(r *route)
RouteOptionFn defines a function implementation of RouteOption.
func WithDecoder ¶
func WithDecoder(d Decoder) RouteOptionFn
WithDecoder specifies the decoder which will be used.
func WithEncoder ¶
func WithEncoder(e Encoder) RouteOptionFn
WithEncoder specifies the encoder which will be used to encode payload to HTTP response.
func WithErrorHandler ¶
func WithErrorHandler(errHandler ErrorHandler) RouteOptionFn
WithErrorHandler is a RouteOption to specify a custom ErrorHandler.
func WithGzip ¶
func WithGzip(cfg GzipConfig) RouteOptionFn
WithGzip returns a middleware which compresses HTTP response using gzip compression.
func WithRecovery ¶
func WithRecovery() RouteOptionFn
WithRecovery returns a middleware which recovers from panics.
func (RouteOptionFn) Apply ¶
func (fn RouteOptionFn) Apply(app *Application)
Apply implements Option.
func (RouteOptionFn) ApplyRoute ¶
func (fn RouteOptionFn) ApplyRoute(r *route)
ApplyRoute implements RouteOption.