Documentation ¶
Index ¶
- Constants
- Variables
- func CleanPath(p string) string
- func Errorf(msg string, v ...interface{})
- func Nanotime() int64
- func UnJSONBytes(b []byte, v ...interface{}) (interface{}, error)
- type App
- func (app *App) AddFlag(f Flag)
- func (app *App) CORSMiddleware() func(*Context)
- func (app *App) DELETE(route string, handler interface{}) *App
- func (app *App) Domain(domain string) *Router
- func (app *App) Forbidden(ctx *Context)
- func (app *App) GET(route string, handler interface{}) *App
- func (app *App) HEAD(route string, handler interface{}) *App
- func (app *App) HTTP() *Router
- func (app *App) HTTPS() *Router
- func (app *App) Handle(method, route string, handler interface{}) *App
- func (app *App) HandleMethodNotAllowed(newValue bool) (oldValue bool)
- func (app *App) HandleOPTIONS(newValue bool) (oldValue bool)
- func (app *App) Health()
- func (app *App) HealthHandler(ctx *Context)
- func (app *App) JSON(route string, handler interface{}) *App
- func (app *App) ListenAndServe(addr ...string) error
- func (app *App) ListenAndServeAll(httpAddr ...string)
- func (app *App) ListenAndServeAllDev(httpAddr ...string)
- func (app *App) ListenAndServeAutoTLS(addr string, cachePath ...string) error
- func (app *App) ListenAndServeAutoTLSDev(addr string, cachePath ...string) error
- func (app *App) MethodNotAllowed(c func(ctx *Context)) *App
- func (app *App) NotFound(notFoundHandler func(*Context)) *App
- func (app *App) OPTIONS(route string, handler interface{}) *App
- func (app *App) PATCH(route string, handler interface{}) *App
- func (app *App) POST(route string, handler interface{}) *App
- func (app *App) PUT(route string, handler interface{}) *App
- func (app *App) PanicHandler(panicHandler func(*Context, interface{})) *App
- func (app *App) Redir(url string) func(*Context)
- func (app *App) RegFlags()
- func (app *App) SPAIndex(path string) *Router
- func (app *App) Serve(ln net.Listener) error
- func (app *App) ServeDir(path string) func(*Context)
- func (app *App) ServeDirCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, ...) func(*Context)
- func (app *App) ServeDirNoCache(path string) func(*Context)
- func (app *App) ServeDirNoCacheCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, ...) func(*Context)
- func (app *App) ServeFile(route, file string) *Router
- func (app *App) ServeInfrastructure(i *infrastructure.Infrastructure)
- func (app *App) Sub(path string) *SubRouter
- func (app *App) ToTLSHandler() func(*Context)
- func (app *App) Use(middleware interface{}) error
- func (app *App) UseAfterRequest(middleware interface{}) error
- func (app *App) UsePre(middleware interface{}) error
- type Context
- func (c *Context) CORS() *Context
- func (c *Context) Err500(message ...interface{}) *Context
- func (c *Context) Forbidden()
- func (c *Context) GETKeys() []string
- func (c *Context) GETKeysBytes() [][]byte
- func (c *Context) GETParam(argName string) []string
- func (c *Context) GETParams() map[string][]string
- func (c *Context) HTML() *Context
- func (c *Context) JSON(v interface{}) error
- func (c *Context) JSONError(v interface{}) error
- func (ctx *Context) LogHeaders()
- func (ctx *Context) Proxy(url string) error
- func (c *Context) RouteArg(argName string) string
- func (c *Context) RouteArgErr(argName string) (string, error)
- func (c *Context) ToJSON(v interface{}) ([]byte, error)
- func (c *Context) ToTLS()
- func (c *Context) UnJSON(v interface{}) error
- func (c *Context) UnJSONBytes(b []byte, v ...interface{}) (interface{}, error)
- func (c *Context) Writef(format string, a ...interface{})
- func (c *Context) Writeln(a ...interface{})
- type Cookies
- type FirewallSettings
- type Flag
- type Flags
- type RequestHandler
- type RequestHandlerErr
- type Router
- func (r *Router) Allowed(path, reqMethod string) (allow string)
- func (r *Router) DELETE(route string, handler interface{}) *Router
- func (r *Router) Forbidden(ctx *Context)
- func (r *Router) GET(route string, handler interface{}) *Router
- func (r *Router) HEAD(route string, handler interface{}) *Router
- func (r *Router) HTTP() *Router
- func (r *Router) HTTPS() *Router
- func (r *Router) Handle(method, route string, handler interface{}) *Router
- func (r *Router) HandleMethodNotAllowed(newValue bool) (oldValue bool)
- func (r *Router) HandleOPTIONS(newValue bool) (oldValue bool)
- func (r *Router) Handler() func(*Context)
- func (r *Router) JSON(route string, handler interface{}) *Router
- func (r *Router) Lookup(method, path string, ctx *Context) (RequestHandler, bool)
- func (r *Router) MethodNotAllowed(c func(ctx *Context))
- func (r *Router) NotFound(notFoundHandler func(*Context))
- func (r *Router) OPTIONS(route string, handler interface{}) *Router
- func (r *Router) PATCH(route string, handler interface{}) *Router
- func (r *Router) POST(route string, handler interface{}) *Router
- func (r *Router) PUT(route string, handler interface{}) *Router
- func (r *Router) PanicHandler(panicHandler func(*Context, interface{}))
- func (r *Router) Redir(route, url string)
- func (r *Router) SPAIndex(path string) *Router
- func (r *Router) ServeFile(route, file string) *Router
- func (r *Router) ServeFiles(path string, rootPath string)
- func (r *Router) Sub(path string) *SubRouter
- type Settings
- type SubRouter
- func (r *SubRouter) DELETE(route string, handler interface{}) *SubRouter
- func (r *SubRouter) Forbidden(ctx *Context)
- func (r *SubRouter) GET(route string, handler interface{}) *SubRouter
- func (r *SubRouter) HEAD(route string, handler interface{}) *SubRouter
- func (r *SubRouter) HTTP() *SubRouter
- func (r *SubRouter) HTTPS() *SubRouter
- func (r *SubRouter) JSON(route string, handler interface{}) *SubRouter
- func (r *SubRouter) OPTIONS(route string, handler interface{}) *SubRouter
- func (r *SubRouter) PATCH(route string, handler interface{}) *SubRouter
- func (r *SubRouter) POST(route string, handler interface{}) *SubRouter
- func (r *SubRouter) PUT(route string, handler interface{}) *SubRouter
- func (r *SubRouter) Redir(route, url string)
- func (r *SubRouter) ServeFile(route, file string) *SubRouter
- func (r *SubRouter) Sub(path string) *SubRouter
- func (r *SubRouter) ToTLSHandler() func(*Context)
Constants ¶
const ( // MethodDELETE is the HTTP DELETE method MethodDELETE = "DELETE" // MethodGET is the HTTP GET method MethodGET = "GET" // MethodHEAD is the HTTP HEAD method MethodHEAD = "HEAD" // MethodOPTIONS is the HTTP OPTIONS method MethodOPTIONS = "OPTIONS" // MethodPATCH is the HTTP PATCH method MethodPATCH = "PATCH" // MethodPOST is the HTTP POST method MethodPOST = "POST" // MethodPUT is the HTTP PUT method MethodPUT = "PUT" )
const ( // GET method GET = "GET" // HEAD method HEAD = "HEAD" // OPTIONS method OPTIONS = "OPTIONS" // POST method POST = "POST" // PUT method PUT = "PUT" // PATCH method PATCH = "PATCH" // DELETE method DELETE = "DELETE" // CONNECT method CONNECT = "CONNECT" // PathAny used to minimize memory allocations PathAny = "*" // PathSlashAny used to minimize memory allocations PathSlashAny = "/*" // PathSlash used to minimize memory allocations PathSlash = "/" // HeaderAllow used to minimize memory allocations HeaderAllow = "Allow" )
const Slash = "/"
Slash constant used to minimize string allocations
const Version = "1.0.0"
Version gives you the gramework version you use now
Variables ¶
var ( // ErrTLSNoEmails occurs when no emails provided but user tries to use AutoTLS features ErrTLSNoEmails = errors.New("auto tls: no emails provided") // ErrArgNotFound used when no route argument is found ErrArgNotFound = errors.New("undefined argument") )
var ( // DefaultContentType cached to minimize memory allocations DefaultContentType = []byte("text/plain; charset=utf-8") // QuestionMark cached to minimize memory allocations QuestionMark = []byte("?") // SlashByte cached to minimize memory allocations SlashByte = byte('/') )
var ( // ErrEmptyMiddleware can be returned by App.Use*, if middleware is nil ErrEmptyMiddleware = errors.New("can't use nil middleware") // ErrUnsupportedMiddlewareType can be returned by App.Use*, if middleware type is unsupported ErrUnsupportedMiddlewareType = errors.New("unsupported middleware type") )
Logger handles default logger
Functions ¶
func CleanPath ¶
CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.
The following rules are applied iteratively until no further processing can be done:
- Replace multiple slashes with a single slash.
- Eliminate each . path name element (the current directory).
- Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
- Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.
If the result of this process is an empty string, "/" is returned
func UnJSONBytes ¶
UnJSONBytes serializes and writes a json-formatted response to user
Types ¶
type App ¶
type App struct { EnableFirewall bool Logger log.Interface TLSEmails []string Settings Settings HandleUnknownDomains bool Flags *Flags // contains filtered or unexported fields }
App represents a gramework app
func (*App) AddFlag ¶
AddFlag adds a Flag to flag queue that will be parsed if flags wasn't parsed yet
func (*App) CORSMiddleware ¶
CORSMiddleware provides gramework handler with ctx.CORS() call
func (*App) Handle ¶
Handle registers a new request handle with the given path and method. For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used. This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*App) HandleMethodNotAllowed ¶
HandleMethodNotAllowed changes HandleMethodNotAllowed mode in the router
func (*App) HandleOPTIONS ¶
HandleOPTIONS changes HandleOPTIONS mode in the router
func (*App) HealthHandler ¶
HealthHandler serves info about memory usage
func (*App) JSON ¶
JSON register internal handler that sets json content type and serves given handler with GET method
func (*App) ListenAndServe ¶
ListenAndServe HTTP on given addr. runs flag.Parse() if !flag.Parsed() to support --bind flag.
func (*App) ListenAndServeAll ¶
ListenAndServeAll serves HTTP and HTTPS automatically. HTTPS is served on :443. If it can't serve http or https, it logs an error and exit the server with app.Logger.Fatalf().
func (*App) ListenAndServeAllDev ¶
ListenAndServeAllDev serves HTTP and HTTPS automatically with localhost HTTPS support via self-signed certs. HTTPS is served on :443. If it can't serve http or https, it logs an error and exit the server with app.Logger.Fatalf().
func (*App) ListenAndServeAutoTLS ¶
ListenAndServeAutoTLS serves TLS requests
func (*App) ListenAndServeAutoTLSDev ¶
ListenAndServeAutoTLSDev serves non-production grade TLS requests. Supports localhost.localdomain.
func (*App) MethodNotAllowed ¶
MethodNotAllowed sets MethodNotAllowed handler
func (*App) PanicHandler ¶
PanicHandler set a handler for unhandled panics
func (*App) Redir ¶
Redir sends 301 redirect to the given url
it's equivalent to
ctx.Redirect(url, 301)
func (*App) RegFlags ¶
func (app *App) RegFlags()
RegFlags registers current flag queue in flag parser
func (*App) ServeDirCustom ¶
func (app *App) ServeDirCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, indexNames []string) func(*Context)
ServeDirCustom gives you ability to serve a dir with custom settings
func (*App) ServeDirNoCache ¶
ServeDirNoCache gives you ability to serve a dir without caching
func (*App) ServeDirNoCacheCustom ¶
func (app *App) ServeDirNoCacheCustom(path string, stripSlashes int, compress bool, generateIndexPages bool, indexNames []string) func(*Context)
ServeDirNoCacheCustom gives you ability to serve a dir with custom settings without caching
func (*App) ServeInfrastructure ¶
func (app *App) ServeInfrastructure(i *infrastructure.Infrastructure)
ServeInfrastructure serves Infrastructure info It's an integration of our module
func (*App) Sub ¶
Sub let you quickly register subroutes with given prefix like app.Sub("v1").Sub("users").GET("view/:id", "hi").DELETE("delete/:id", "hi"), that give you /v1/users/view/:id and /v1/users/delete/:id registered
func (*App) ToTLSHandler ¶
ToTLSHandler returns handler that redirects user to HTTP scheme
func (*App) UseAfterRequest ¶
UseAfterRequest the middleware after request processing
type Context ¶
type Context struct { *fasthttp.RequestCtx Logger log.Interface App *App Cookies Cookies // contains filtered or unexported fields }
Context is a gramework request context
func (*Context) GETKeysBytes ¶
GETKeysBytes returns GET parameters keys as []byte
func (*Context) JSONError ¶
JSONError sets Internal Server Error status, serializes and writes a json-formatted response to user
func (*Context) LogHeaders ¶
func (ctx *Context) LogHeaders()
func (*Context) RouteArgErr ¶
RouteArgErr returns an argument value as a string or empty string and ErrArgNotFound if argument was not found
func (*Context) UnJSONBytes ¶
UnJSONBytes serializes and writes a json-formatted response to user
type Cookies ¶
Cookies handles a typical cookie storage
type FirewallSettings ¶
type FirewallSettings struct { // MaxReqPerMin is a max request per minute count MaxReqPerMin int64 // BlockTimeout in seconds BlockTimeout int64 }
FirewallSettings represents a new firewall settings. Internal firewall representation copies this settings atomically.
type Flags ¶
type Flags struct {
// contains filtered or unexported fields
}
Flags is a flags storage
type RequestHandler ¶
type RequestHandler func(*Context)
RequestHandler describes a standard request handler type
func NewGrameHandler ¶
func NewGrameHandler(h http.Handler) RequestHandler
NewGrameHandler wraps net/http handler to fasthttp request handler, so it can be passed to fasthttp server.
While this function may be used for easy switching from net/http to fasthttp, it has the following drawbacks comparing to using manually written fasthttp request handler:
- A lot of useful functionality provided by fasthttp is missing from net/http handler.
- net/http -> fasthttp handler conversion has some overhead, so the returned handler will be always slower than manually written fasthttp handler.
So it is advisable using this function only for quick net/http -> fasthttp switching. Then manually convert net/http handlers to fasthttp handlers according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .
This adaptor is a fork of https://github.com/valyala/fasthttp/tree/master/fasthttpadaptor We're embedding it because we don't want additional allocation, but we need exactly gramework request handler, not fasthttp request handler. The package provides helper functions for converting net/http request handlers to fasthttp request handlers. See the original license in /3rd-Party Licenses/fasthttp
func NewGrameHandlerFunc ¶
func NewGrameHandlerFunc(h http.HandlerFunc) RequestHandler
NewGrameHandlerFunc wraps net/http handler func to gramework request handler, so it can be passed to gramework router.
While this function may be used for easy switching from net/http to gramework, it has the following drawbacks comparing to using manually written gramework request handler:
- A lot of useful functionality provided by fasthttp is missing from net/http handler.
- net/http -> fasthttp handler conversion has some overhead, so the returned handler will be always slower than manually written fasthttp handler.
So it is advisable using this function only for quick net/http -> fasthttp switching. Then manually convert net/http handlers to fasthttp handlers according to https://github.com/valyala/fasthttp#switching-from-nethttp-to-fasthttp .
This adaptor is a fork of https://github.com/valyala/fasthttp/tree/master/fasthttpadaptor We're embedding it because we don't want additional allocation, but we need exactly gramework request handler, not fasthttp request handler. The package provides helper functions for converting net/http request handlers to fasthttp request handlers. See the original license in /3rd-Party Licenses/fasthttp
type RequestHandlerErr ¶
RequestHandlerErr describes a standard request handler with error returned type
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router handles internal handler conversion etc.
func (*Router) Handle ¶
Handle registers a new request handle with the given path and method. For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used. This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*Router) HandleMethodNotAllowed ¶
HandleMethodNotAllowed changes HandleMethodNotAllowed mode in the router
func (*Router) HandleOPTIONS ¶
HandleOPTIONS changes HandleOPTIONS mode in the router
func (*Router) JSON ¶
JSON register internal handler that sets json content type and serves given handler with GET method
func (*Router) Lookup ¶
func (r *Router) Lookup(method, path string, ctx *Context) (RequestHandler, bool)
Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values. Otherwise the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.
func (*Router) MethodNotAllowed ¶
MethodNotAllowed sets MethodNotAllowed handler
func (*Router) PanicHandler ¶
PanicHandler set a handler for unhandled panics
func (*Router) Redir ¶
Redir sends 301 redirect to the given url
it's equivalent to
ctx.Redirect(url, 301)
func (*Router) ServeFiles ¶
ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler.
router.ServeFiles("/src/*filepath", "/var/www")
type SubRouter ¶
type SubRouter struct {
// contains filtered or unexported fields
}
SubRouter handles subs registration like app.Sub("v1").GET("someRoute", "hi")
func (*SubRouter) JSON ¶
JSON register internal handler that sets json content type and serves given handler with GET method
func (*SubRouter) Redir ¶
Redir sends 301 redirect to the given url
it's equivalent to
ctx.Redirect(url, 301)
func (*SubRouter) Sub ¶
Sub let you quickly register subroutes with given prefix like app.Sub("v1").Sub("users").GET("view/:id", "hi").DELETE("delete/:id", "hi"), that give you /v1/users/view/:id and /v1/users/delete/:id registered
func (*SubRouter) ToTLSHandler ¶
ToTLSHandler returns handler that redirects user to HTTP scheme
Source Files ¶
- adaptor.go
- app.go
- app_handler.go
- app_healthcheck.go
- app_listenAndServe.go
- app_listenAndServeAll.go
- app_listenAndServeAutoTLS.go
- app_serve.go
- const.go
- context.go
- context_logHeaders.go
- context_proxy.go
- cookie.go
- errors.go
- fasthttprouter_cache.go
- fasthttprouter_path.go
- fasthttprouter_router.go
- fasthttprouter_tree.go
- firewall.go
- infrastructure.go
- log.go
- middleware.go
- nanotime.go
- new.go
- regFlags.go
- router.go
- router_default.go
- router_domain.go
- router_internals.go
- sendfile.go
- subroute.go
- types.go
- version.go