Documentation
¶
Index ¶
- Constants
- type Context
- func (c Context) ErrorJSONResponse(status int, message string)
- func (c Context) Params() url.Values
- func (c Context) Request() *http.Request
- func (c Context) ResponseWriter() http.ResponseWriter
- func (c Context) RouterParams() Storage
- func (ctx *Context) SetContentType(contentType string)
- func (c Context) SuccessJSONResponse(body any)
- type ErrorObject
- type MiddlewareFunction
- type ResponseObject
- type Route
- type RouterMethods
- type SimpleRouter
- func (sr SimpleRouter) Delete(route string, handler func(*Context)) RouterMethods
- func (sr SimpleRouter) Get(route string, handler func(*Context)) RouterMethods
- func (sr SimpleRouter) GetRoutes(method string) []*Route
- func (sr SimpleRouter) Head(route string, handler func(*Context)) RouterMethods
- func (sr SimpleRouter) Options(route string, handler func(*Context))
- func (sr SimpleRouter) Patch(route string, handler func(*Context)) RouterMethods
- func (sr SimpleRouter) Post(route string, handler func(*Context)) RouterMethods
- func (sr SimpleRouter) Put(route string, handler func(*Context)) RouterMethods
- func (sr *SimpleRouter) RunServer(addr string) error
- func (sr *SimpleRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (sr *SimpleRouter) Use(middlewares ...MiddlewareFunction)
- type Storage
Constants ¶
const ( MethodGet = "GET" MethodPost = "POST" MethodPut = "PUT" MethodPatch = "PATCH" MethodDelete = "DELETE" MethodOptions = "OPTIONS" MethodHead = "HEAD" )
const ( MessageBodyIsNotValid = "Request body is not valid" MessageMethodNotAllowed = "Method not allowed" MessagePageNotFound = "Page not found" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
func (Context) ErrorJSONResponse ¶ added in v1.1.1
func (Context) ResponseWriter ¶
func (c Context) ResponseWriter() http.ResponseWriter
Returns basic HTTP response writer
func (Context) RouterParams ¶
Returns RouterParams structure with params of route as key value
If you have route "/user/:id/posts/:postId" and request URL path "/user/1/posts/45" RouterParams will have map with keys took from route and values which it will take from request URL path `["id": "1", "postId": "45"]`
func (*Context) SetContentType ¶ added in v1.1.1
Set response content type
func (Context) SuccessJSONResponse ¶ added in v1.1.1
type ErrorObject ¶
type MiddlewareFunction ¶ added in v1.1.3
type MiddlewareFunction func(http.ResponseWriter, *http.Request) bool
type ResponseObject ¶
type ResponseObject[T any] struct { Error *ErrorObject `json:"error"` Body T `json:"body"` }
type Route ¶
func (*Route) Middleware ¶ added in v1.1.3
func (r *Route) Middleware(middlewares ...MiddlewareFunction)
Store all middllewares for a specific router
Every middleware should return TRUE if the rule succeeds If the middleware returns FALSE - other middlewares will not be triggered
type RouterMethods ¶ added in v1.1.3
type RouterMethods interface {
Middleware(middlewares ...MiddlewareFunction)
}
type SimpleRouter ¶
type SimpleRouter struct {
Routes *routes
ContentType string
// contains filtered or unexported fields
}
Initial struct to create HTTP server provide this structure to http.ListenAndServe function It has a list of routes which is stored to serve
func (SimpleRouter) Delete ¶
func (sr SimpleRouter) Delete(route string, handler func(*Context)) RouterMethods
Add route by method DELETE
func (SimpleRouter) Get ¶
func (sr SimpleRouter) Get(route string, handler func(*Context)) RouterMethods
Add route by method GET
func (SimpleRouter) GetRoutes ¶
func (sr SimpleRouter) GetRoutes(method string) []*Route
func (SimpleRouter) Head ¶
func (sr SimpleRouter) Head(route string, handler func(*Context)) RouterMethods
Add route by method HEAD
func (SimpleRouter) Options ¶
func (sr SimpleRouter) Options(route string, handler func(*Context))
Add route by method OPTIONS
func (SimpleRouter) Patch ¶
func (sr SimpleRouter) Patch(route string, handler func(*Context)) RouterMethods
Add route by method PATCH
func (SimpleRouter) Post ¶
func (sr SimpleRouter) Post(route string, handler func(*Context)) RouterMethods
Add route by method POST
func (SimpleRouter) Put ¶
func (sr SimpleRouter) Put(route string, handler func(*Context)) RouterMethods
Add route by method PUT
func (*SimpleRouter) RunServer ¶ added in v1.1.1
func (sr *SimpleRouter) RunServer(addr string) error
Runs server with http.ListenAndServe
func (*SimpleRouter) ServeHTTP ¶
func (sr *SimpleRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)
Implements an http.Handler interface to use it like server handler in http.ListenAndServe
func (*SimpleRouter) Use ¶ added in v1.1.3
func (sr *SimpleRouter) Use(middlewares ...MiddlewareFunction)