Documentation
¶
Index ¶
- Variables
- func Bind(r *http.Request, out interface{}, methods ...string) error
- func BindForm(r *http.Request, out interface{}) error
- func BindJson(r *http.Request, out interface{}) error
- func BindQuery(r *http.Request, out interface{}) error
- func GetVal(ctx context.Context, key string) interface{}
- func GetVals(ctx context.Context) map[string]interface{}
- func GetValue(r *http.Request, key string) interface{}
- func GetValues(r *http.Request) map[string]interface{}
- func Handle(pattern string, svc interface{})
- func Params(r *http.Request) map[string]string
- func Query(r *http.Request, name string) string
- func SetVal(ctx context.Context, key string, val interface{}) context.Context
- func SetValue(r *http.Request, key string, val interface{}) *http.Request
- func Vars(ctx context.Context) map[string]string
- type DefaultReader
- type DefaultWriter
- type Error
- type Handler
- func (h *Handler) AddService(path string, svc interface{})
- func (h *Handler) NotFound(handler interface{})
- func (h *Handler) Routes() (routes []string)
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Use(fns ...Middleware)
- func (h *Handler) WithPrefix(prefix string) *Handler
- type Init
- type Json
- type Middleware
- type Middlewares
- type RequestReader
- type Response
- type ResponseWriter
- type Route
- type Router
Constants ¶
This section is empty.
Variables ¶
var (
ErrReaderReturnLen = errors.New("reader args len does not match")
)
Functions ¶
func Handle ¶
func Handle(pattern string, svc interface{})
Handle registers a struct or a *Handler for the given pattern in the http.DefaultServeMux.
Types ¶
type DefaultReader ¶
DefaultReader processes request with json.Encoder, urlencoded form and multipart for structs if it's just basic types it will be read from body as array such as [1, "hello", false] you can overwrite bind to apply validation library, etc
type DefaultWriter ¶
type DefaultWriter struct { // Optional ErrorHandler, called whenever unhandled errors occurs, defaults to logging errors ErrorHandler func(error) Errors map[error]Error EscapeJsonHtml bool }
DefaultWriter uses json.Encoder for output and manages error handling. Adding Errors mapping can help with your existing error to a proper Error{}
func (*DefaultWriter) Write ¶
func (dw *DefaultWriter) Write(w http.ResponseWriter, r *http.Request, types []reflect.Type, vals []reflect.Value)
Write implements the DefaultWriter ResponseWriter returning (int, any, error) will write status int, any response if error is nil returning (any, error) will write any response if error is nil with status 200 or 400, 500 depdening on your error returning (int, any, any, error) will write status int slice of [any, any] response if error is nil
func (*DefaultWriter) WriteJSON ¶
func (dw *DefaultWriter) WriteJSON(w http.ResponseWriter, out interface{})
This writes application/json content type uses status code 200 on valid ones and 500 on uncaught, 400 on malformed json, etc. use Json{Status, Content} to specify a code
func (*DefaultWriter) WriteResponse ¶
func (dw *DefaultWriter) WriteResponse(w http.ResponseWriter, resp *Response)
type Handler ¶
type Handler struct { // Writer controls the output of your service, defaults to DefaultWriter Writer ResponseWriter // Reader controls the input of your service, defaults to DefaultReader Reader RequestReader // contains filtered or unexported fields }
func NewHandler ¶
func NewHandler(svc interface{}) *Handler
NewHandler creates a handler for a given struct.
func (*Handler) AddService ¶
AddService adds a new service to specified route. You can put {param} in this route.
func (*Handler) NotFound ¶
func (h *Handler) NotFound(handler interface{})
NotFound sets the notFound handler and calls it if no route matches
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP calls the method with the matched route.
func (*Handler) Use ¶
func (h *Handler) Use(fns ...Middleware)
Use adds a middleware to your services.
func (*Handler) WithPrefix ¶
WithPrefix prefixes your service with given path. You can't use parameters here. This is useful if you want to register this handler with another third party router.
type Init ¶
type Init interface {
Init(*Handler)
}
Init interface to access and override handler configs
type Json ¶
type Json struct { Status int Content interface{} }
Json response to specify a status code for default writer
type Middlewares ¶
type Middlewares interface {
Middlewares() []Middleware
}
Middlewares interface for common middleware for a struct
type RequestReader ¶
RequestReader is called for input for your method if your parameter contains a things other than *http.Request, http.ResponseWriter, context.Context you'll get a slice of types and you must return values corresponding to those types
type ResponseWriter ¶
type ResponseWriter interface {
Write(http.ResponseWriter, *http.Request, []reflect.Type, []reflect.Value)
}
ResponseWriter is called on outputs of your methods. slice of reflect.Type & Value is the types and returned values
type Route ¶
type Route struct { // Handler is the method name you want to use for this route Handler string // optional path, will use default behaviour if not present Path string // optional methods, will allow all if not present Methods []string // optional middlewares, run specific middleware for this route Middlewares []Middleware }
Route for doing overrides with router interface and method restrictions.