Documentation ¶
Index ¶
- Constants
- type Application
- func (app *Application) AddRoute(method string, pattern string, handlers []HandlerFunc)
- func (app *Application) Delete(pattern string, handlers ...HandlerFunc)
- func (app *Application) Get(pattern string, handlers ...HandlerFunc)
- func (app *Application) Group(prefix string) *Group
- func (app *Application) Head(pattern string, handlers ...HandlerFunc)
- func (app *Application) LoadHTMLGlob(pattern string)
- func (app *Application) Options(pattern string, handlers ...HandlerFunc)
- func (app *Application) Patch(pattern string, handlers ...HandlerFunc)
- func (app *Application) Post(pattern string, handlers ...HandlerFunc)
- func (app *Application) Put(pattern string, handlers ...HandlerFunc)
- func (app *Application) Run(address ...string)
- func (app *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (app *Application) SetFuncMap(funcMap template.FuncMap)
- func (app *Application) Static(root string, prefix string)
- func (app *Application) Use(middlewares ...Middleware)
- type Config
- type Context
- func (c *Context) AddHeader(key, value string)
- func (c *Context) Body() []byte
- func (c *Context) Cookie(name string) *http.Cookie
- func (c *Context) Cookies() []*http.Cookie
- func (c *Context) DelData(key string)
- func (c *Context) DelHeader(key string)
- func (c *Context) Fail(code int, message string)
- func (c *Context) File(filepath string) error
- func (c *Context) GetData(key string) interface{}
- func (c *Context) HTML(code int, name string, data interface{})
- func (c *Context) Header(key string) string
- func (c *Context) Headers() http.Header
- func (c *Context) JSON(code int, obj interface{})
- func (c *Context) JSONBody(v interface{}, valid ...bool) error
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) ParamFloat32(key string) (float32, error)
- func (c *Context) ParamFloat64(key string) (float64, error)
- func (c *Context) ParamInt(key string) (int, error)
- func (c *Context) ParamInt64(key string) (int64, error)
- func (c *Context) ParamString(key string) string
- func (c *Context) ParamUInt(key string) (uint, error)
- func (c *Context) ParamUInt64(key string) (uint64, error)
- func (c *Context) Params() map[string]string
- func (c *Context) Queries() map[string][]string
- func (c *Context) Query(key string) string
- func (c *Context) QueryFloat32(key string) (float32, error)
- func (c *Context) QueryFloat64(key string) (float64, error)
- func (c *Context) QueryInt(key string) (int, error)
- func (c *Context) QueryInt32(key string) (int32, error)
- func (c *Context) QueryInt64(key string) (int64, error)
- func (c *Context) QueryInt8(key string) (int8, error)
- func (c *Context) QueryString(key string) string
- func (c *Context) QueryUInt(key string) (uint, error)
- func (c *Context) QueryUInt32(key string) (uint32, error)
- func (c *Context) QueryUInt64(key string) (uint64, error)
- func (c *Context) QueryUInt8(key string) (uint8, error)
- func (c *Context) RawBody() []byte
- func (c *Context) Redirect(code int, url string)
- func (c *Context) Referer() string
- func (c *Context) RemoteAddr() string
- func (c *Context) SetBody(body []byte)
- func (c *Context) SetCookie(key string, value string)
- func (c *Context) SetCustomCookie(cookie *http.Cookie)
- func (c *Context) SetData(key string, value interface{})
- func (c *Context) SetHeader(key string, value string)
- func (c *Context) SetStatus(code int)
- func (c *Context) SkipFlush()
- func (c *Context) Status() int
- func (c *Context) StringBody() string
- func (c *Context) Success(data interface{})
- func (c *Context) Text(code int, text string)
- func (c *Context) UserAgent() string
- func (c *Context) XML(code int, obj interface{})
- type Group
- func (g *Group) AddRoute(method string, pattern string, handlers []HandlerFunc)
- func (g *Group) Delete(pattern string, handlers ...HandlerFunc)
- func (g *Group) Get(pattern string, handlers ...HandlerFunc)
- func (g *Group) Group(prefix string) *Group
- func (g *Group) Head(pattern string, handlers ...HandlerFunc)
- func (g *Group) Options(pattern string, handlers ...HandlerFunc)
- func (g *Group) Patch(pattern string, handlers ...HandlerFunc)
- func (g *Group) Post(pattern string, handlers ...HandlerFunc)
- func (g *Group) Put(pattern string, handlers ...HandlerFunc)
- func (g *Group) Use(middlewares ...HandlerFunc)
- type HandlerFunc
- type JSONMarshal
- type JSONUnmarshal
- type Map
- type Middleware
Constants ¶
const ( MIMETextPlain = "text/plain" MIMETextHTML = "text/html" MIMEApplicationXML = "application/xml" MIMEApplicationJSON = "application/json" )
MIME types
const ( HeaderContentType = "Content-Type" HeaderContentDisposition = "Content-Disposition" )
Header keys
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct { Config *Config Logger *lightlog.ConsoleLogger // contains filtered or unexported fields }
func DefaultApp ¶
func DefaultApp() *Application
DefaultApp returns a new instance of the Application struct with default middlewares
func NewApp ¶
func NewApp(c ...*Config) *Application
NewApp returns a new instance of the Application struct.
func (*Application) AddRoute ¶
func (app *Application) AddRoute(method string, pattern string, handlers []HandlerFunc)
AddRoute is a function that adds a new route to the router. It composes the global middlewares, route-specific middlewares, and the actual handler function to form a single MiddlewareFunc, and then adds it to the router.
func (*Application) Delete ¶
func (app *Application) Delete(pattern string, handlers ...HandlerFunc)
Delete adds a new route with method "DELETE" to the router.
func (*Application) Get ¶
func (app *Application) Get(pattern string, handlers ...HandlerFunc)
Get adds a new route with method "GET" to the router.
func (*Application) Group ¶
func (app *Application) Group(prefix string) *Group
Group returns a new instance of the Group struct with the given prefix.
func (*Application) Head ¶
func (app *Application) Head(pattern string, handlers ...HandlerFunc)
Head adds a new route with method "HEAD" to the router.
func (*Application) LoadHTMLGlob ¶ added in v0.5.0
func (app *Application) LoadHTMLGlob(pattern string)
LoadHTMLGlob loads HTML templates from a glob pattern and sets them in the Application struct. It uses the template.Must function to panic if there is an error parsing the templates. It also sets the funcMap in the Application struct to the funcMap passed in as an argument.
func (*Application) Options ¶
func (app *Application) Options(pattern string, handlers ...HandlerFunc)
Options adds a new route with method "OPTIONS" to the router.
func (*Application) Patch ¶
func (app *Application) Patch(pattern string, handlers ...HandlerFunc)
Patch adds a new route with method "PATCH" to the router.
func (*Application) Post ¶
func (app *Application) Post(pattern string, handlers ...HandlerFunc)
Post adds a new route with method "POST" to the router.
func (*Application) Put ¶
func (app *Application) Put(pattern string, handlers ...HandlerFunc)
Put adds a new route with method "PUT" to the router.
func (*Application) Run ¶
func (app *Application) Run(address ...string)
Run starts the HTTP server and listens for incoming requests.
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP is the function that handles HTTP requests. It finds the matching route, creates a new Context, sets the route parameters, and executes the MiddlewareFunc chain.
func (*Application) SetFuncMap ¶ added in v0.5.0
func (app *Application) SetFuncMap(funcMap template.FuncMap)
SetFuncMap sets the funcMap in the Application struct to the funcMap passed in as an argument.
func (*Application) Static ¶ added in v0.5.0
func (app *Application) Static(root string, prefix string)
Static serves static files from the given root directory with the given prefix. It uses the os.Executable function to get the path of the executable file, and then joins it with the root and the path after the prefix to get the full file path. If the file exists, it is served with a 200 status code using the http.ServeFile function. If the file does not exist, a 404 status code is returned with the text "Not Found".
func (*Application) Use ¶
func (app *Application) Use(middlewares ...Middleware)
Use adds one or more Middlewares to the array of middlewares in the Application struct.
type Config ¶ added in v0.4.0
type Config struct { AppName string JSONEncoder JSONMarshal JSONDecoder JSONUnmarshal NotFoundHandler HandlerFunc // Handler function for 404 Not Found error EnableDebug bool }
type Context ¶
type Context struct { App *Application Req *http.Request Res http.ResponseWriter Method string // HTTP method of the originReq Path string // URL path of the originReq // contains filtered or unexported fields }
Context represents the context of an HTTP request/response.
func NewContext ¶ added in v0.1.3
NewContext creates a new context object with the given HTTP response writer and req.
func (*Context) HTML ¶ added in v0.5.0
HTML writes an HTML response with the given status code, template name, and data.
func (*Context) JSONBody ¶
JSONBody parses the origin request body as JSON and stores the result in v.
func (*Context) Next ¶
func (c *Context) Next()
Next calls the next middleware function in the chain.
func (*Context) ParamFloat32 ¶ added in v0.6.0
ParamFloat32 returns the value of a URL parameter as a float32 for a given key.
func (*Context) ParamFloat64 ¶ added in v0.6.0
ParamFloat64 returns the value of a URL parameter as a float64 for a given key.
func (*Context) ParamInt ¶ added in v0.6.0
ParamInt returns the value of a URL parameter as an integer for a given key.
func (*Context) ParamInt64 ¶ added in v0.6.0
ParamInt64 returns the value of a URL parameter as an int64 for a given key.
func (*Context) ParamString ¶ added in v0.6.0
ParamString returns the value of a URL parameter as a string for a given key.
func (*Context) ParamUInt ¶ added in v0.6.0
ParamUInt returns the value of a URL parameter as a uint for a given key.
func (*Context) ParamUInt64 ¶ added in v0.6.0
ParamUInt64 returns the value of a URL parameter as a uint64 for a given key.
func (*Context) QueryFloat32 ¶ added in v0.7.0
QueryFloat32 returns the value of a given query parameter as a float32.
func (*Context) QueryFloat64 ¶ added in v0.7.0
QueryFloat64 returns the value of a given query parameter as a float64.
func (*Context) QueryInt ¶ added in v0.7.0
QueryInt returns the value of a given query parameter as an int.
func (*Context) QueryInt32 ¶ added in v0.7.0
QueryInt32 returns the value of a given query parameter as an int32.
func (*Context) QueryInt64 ¶ added in v0.7.0
QueryInt64 returns the value of a given query parameter as an int64.
func (*Context) QueryInt8 ¶ added in v0.7.0
QueryInt8 returns the value of a given query parameter as an int8.
func (*Context) QueryString ¶ added in v0.7.0
QueryString returns the value of a given query parameter as a string.
func (*Context) QueryUInt ¶ added in v0.7.0
QueryUInt returns the value of a given query parameter as a uint.
func (*Context) QueryUInt32 ¶ added in v0.7.0
QueryUInt32 returns the value of a given query parameter as a uint32.
func (*Context) QueryUInt64 ¶ added in v0.7.0
QueryUInt64 returns the value of a given query parameter as a uint64.
func (*Context) QueryUInt8 ¶ added in v0.7.0
QueryUInt8 returns the value of a given query parameter as a uint8.
func (*Context) Redirect ¶
Redirect redirects the originReq to a new URL with the given status code.
func (*Context) RemoteAddr ¶
RemoteAddr returns the remote address of the request.
func (*Context) SetCustomCookie ¶
SetCustomCookie sets a custom cookie in the response.
func (*Context) SkipFlush ¶ added in v0.3.0
func (c *Context) SkipFlush()
SkipFlush sets the skipFlush flag to true, which prevents the response buffer from being flushed.
func (*Context) StringBody ¶
StringBody returns the origin request body as a string.
func (*Context) Success ¶
func (c *Context) Success(data interface{})
Success writes a successful response with the given data.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a group of routes with a common prefix and middleware.
func (*Group) AddRoute ¶
func (g *Group) AddRoute(method string, pattern string, handlers []HandlerFunc)
AddRoute adds a new route to the Application with the given method, pattern, and handlers. The route's path is the full prefix of the Group concatenated with the given pattern. The route's handlers are the middleware functions of the Group and its ancestors concatenated with the given handlers.
func (*Group) Delete ¶
func (g *Group) Delete(pattern string, handlers ...HandlerFunc)
Delete adds a new DELETE route to the Application with the given pattern and handlers.
func (*Group) Get ¶
func (g *Group) Get(pattern string, handlers ...HandlerFunc)
Get adds a new GET route to the Application with the given pattern and handlers.
func (*Group) Group ¶
Group creates a new Group with the given prefix and adds it as a child of the current Group.
func (*Group) Head ¶
func (g *Group) Head(pattern string, handlers ...HandlerFunc)
Head adds a new HEAD route to the Application with the given pattern and handlers.
func (*Group) Options ¶
func (g *Group) Options(pattern string, handlers ...HandlerFunc)
Options adds a new OPTIONS route to the Application with the given pattern and handlers.
func (*Group) Patch ¶
func (g *Group) Patch(pattern string, handlers ...HandlerFunc)
Patch adds a new PATCH route to the Application with the given pattern and handlers.
func (*Group) Post ¶
func (g *Group) Post(pattern string, handlers ...HandlerFunc)
Post adds a new POST route to the Application with the given pattern and handlers.
func (*Group) Put ¶
func (g *Group) Put(pattern string, handlers ...HandlerFunc)
Put adds a new PUT route to the Application with the given pattern and handlers.
func (*Group) Use ¶
func (g *Group) Use(middlewares ...HandlerFunc)
Use adds the given middleware functions to the Group's middleware stack.
type HandlerFunc ¶
type HandlerFunc func(*Context)
HandlerFunc is a function type that represents the actual handler function for a route.
type JSONMarshal ¶ added in v0.4.0
type JSONUnmarshal ¶ added in v0.4.0
type Middleware ¶ added in v0.1.1
type Middleware = HandlerFunc
func Logger ¶
func Logger() Middleware
Logger returns a middleware function that logs incoming requests
func Recovery ¶
func Recovery(handler ...HandlerFunc) Middleware
Recovery returns a middleware that recovers from panics and sends a 500 response with an error message.