Documentation ¶
Index ¶
- Constants
- type Config
- type Context
- func (c *Context) BindJSON(obj interface{}) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) (string, error)
- func (c *Context) DeviceType() string
- func (c *Context) Form(key string) string
- func (c *Context) FormFile(key string) (*multipart.FileHeader, error)
- func (c *Context) Get(key string) (interface{}, bool)
- func (c *Context) JSON(code int, i interface{}) error
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Param(name string) string
- func (c *Context) ParamInt(key string) (int, error)
- func (c *Context) Query(key string) string
- func (c *Context) QueryInt(key string) (int, error)
- func (c *Context) QueryIntWithDefault(key string, defaultValue int) (int, error)
- func (c *Context) Redirect(code int, location string) error
- func (c *Context) Render(code int, viewName string, data interface{}) error
- func (c *Context) RequestHeader(key string) string
- func (c *Context) RespHeader(key, value string)
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) (err error)
- func (c *Context) Set(key string, val interface{})
- func (c *Context) SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, ...)
- func (c *Context) SetStatus(code int)
- func (c *Context) SetStdContext(ctx context.Context)
- func (c *Context) Status() int
- func (c *Context) StdContext() context.Context
- func (c *Context) String(code int, s string) error
- type ErrorHandler
- type HandlerFunc
- type MiddlewareFunc
- type MiddlewareHandler
- type NapNap
- func (nap *NapNap) All(path string, handler HandlerFunc)
- func (nap *NapNap) Delete(path string, handler HandlerFunc)
- func (nap *NapNap) Get(path string, handler HandlerFunc)
- func (nap *NapNap) Head(path string, handler HandlerFunc)
- func (nap *NapNap) Options(path string, handler HandlerFunc)
- func (nap *NapNap) Patch(path string, handler HandlerFunc)
- func (nap *NapNap) Post(path string, handler HandlerFunc)
- func (nap *NapNap) Put(path string, handler HandlerFunc)
- func (nap *NapNap) Run(engine *Server) error
- func (nap *NapNap) RunAll(addrs []string) error
- func (nap *NapNap) RunAutoTLS(engine *Server) error
- func (nap *NapNap) RunTLS(engine *Server) error
- func (nap *NapNap) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (nap *NapNap) SetRender(templateRootPath string)
- func (nap *NapNap) SetTemplate(t *template.Template)
- func (nap *NapNap) Use(mHandler MiddlewareHandler)
- func (nap *NapNap) UseFunc(aFunc func(c *Context, next HandlerFunc))
- type Param
- type ResponseWriter
- type Server
Constants ¶
const ( // CONNECT HTTP method CONNECT = "CONNECT" // DELETE HTTP method DELETE = "DELETE" // GET HTTP method GET = "GET" // HEAD HTTP method HEAD = "HEAD" // OPTIONS HTTP method OPTIONS = "OPTIONS" // PATCH HTTP method PATCH = "PATCH" // POST HTTP method POST = "POST" // PUT HTTP method PUT = "PUT" // TRACE HTTP method TRACE = "TRACE" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { Addr string Domain string // abc123.com, abc456.com CertCachePath string TLSCertFile string TLSKeyFile string ReadTimeout time.Duration WriteTimeout time.Duration }
Config ...
type Context ¶
type Context struct { NapNap *NapNap Request *http.Request Writer ResponseWriter // contains filtered or unexported fields }
Context represents the context of the current HTTP request. It holds request and response objects, path, path parameters, data and registered handler.
func FromContext ¶
FromContext return a napnap context from the standard context
func NewContext ¶
func NewContext(napnap *NapNap, req *http.Request, writer ResponseWriter) *Context
NewContext returns a new context instance
func (*Context) BindJSON ¶
BindJSON binds the request body into provided type `obj`. The default binder does it based on Content-Type header.
func (*Context) ClientIP ¶
ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. Use X-Forwarded-For before X-Real-Ip as nginx uses X-Real-Ip with the proxy's IP.
func (*Context) ContentType ¶
ContentType returns the Content-Type header of the request.
func (*Context) DeviceType ¶
DeviceType returns user's device type which includes web, mobile, tab, tv
func (*Context) FormFile ¶
func (c *Context) FormFile(key string) (*multipart.FileHeader, error)
FormFile returns file.
func (*Context) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
func (*Context) QueryIntWithDefault ¶
QueryIntWithDefault returns query parameter by key and cast the value to int. If the value doesn't exist, the default value will be used.
func (*Context) RequestHeader ¶
RequestHeader is a intelligent shortcut for c.Request.Header.Get(key)
func (*Context) RespHeader ¶
RespHeader is a intelligent shortcut for c.Writer.Header().Set(key, value) It writes a header in the response. If value == "", this method removes the header `c.Writer.Header().Del(key)`
func (*Context) SaveUploadedFile ¶
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) (err error)
SaveUploadedFile uploads the form file to specific dst.
func (*Context) Set ¶
Set saves data in the context. It also lazy initializes c.Keys if it was not used previously.
func (*Context) SetCookie ¶
func (c *Context) SetCookie( name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool, )
SetCookie allows us to create an cookie
func (*Context) SetStdContext ¶
SetStdContext allow us to save the golang context to request
func (*Context) StdContext ¶
StdContext return golang standard context
type ErrorHandler ¶ added in v1.1.0
ErrorHandler defines a function to handle HTTP errors
type HandlerFunc ¶
HandlerFunc defines a function to server HTTP requests
func WrapHandler ¶
func WrapHandler(h http.Handler) HandlerFunc
WrapHandler wraps `http.Handler` into `napnap.HandlerFunc`.
type MiddlewareFunc ¶
type MiddlewareFunc func(c *Context, next HandlerFunc)
MiddlewareFunc is an adapter to allow the use of ordinary functions as NapNap handlers.
func (MiddlewareFunc) Invoke ¶
func (m MiddlewareFunc) Invoke(c *Context, next HandlerFunc)
Invoke function is a middleware entry
type MiddlewareHandler ¶
type MiddlewareHandler interface {
Invoke(c *Context, next HandlerFunc)
}
MiddlewareHandler is an interface that objects can implement to be registered to serve as middleware in the NapNap middleware stack.
type NapNap ¶
type NapNap struct { MaxRequestBodySize int64 ErrorHandler ErrorHandler NotFoundHandler HandlerFunc // contains filtered or unexported fields }
NapNap is root level of framework instance
func (*NapNap) All ¶ added in v1.1.0
func (nap *NapNap) All(path string, handler HandlerFunc)
All is a shortcut for adding all methods
func (*NapNap) Delete ¶ added in v1.1.0
func (nap *NapNap) Delete(path string, handler HandlerFunc)
Delete is a shortcut for router.Add("DELETE", path, handle)
func (*NapNap) Get ¶ added in v1.1.0
func (nap *NapNap) Get(path string, handler HandlerFunc)
Get is a shortcut for router.Add("GET", path, handle)
func (*NapNap) Head ¶ added in v1.1.0
func (nap *NapNap) Head(path string, handler HandlerFunc)
Head is a shortcut for router.Add("HEAD", path, handle)
func (*NapNap) Options ¶ added in v1.1.0
func (nap *NapNap) Options(path string, handler HandlerFunc)
Options is a shortcut for router.Add("OPTIONS", path, handle)
func (*NapNap) Patch ¶ added in v1.1.0
func (nap *NapNap) Patch(path string, handler HandlerFunc)
Patch is a shortcut for router.Add("PATCH", path, handle)
func (*NapNap) Post ¶ added in v1.1.0
func (nap *NapNap) Post(path string, handler HandlerFunc)
Post is a shortcut for router.Add("POST", path, handle)
func (*NapNap) Put ¶ added in v1.1.0
func (nap *NapNap) Put(path string, handler HandlerFunc)
Put is a shortcut for router.Add("PUT", path, handle)
func (*NapNap) RunAutoTLS ¶
RunAutoTLS will run http/2 server
func (*NapNap) ServeHTTP ¶
func (nap *NapNap) ServeHTTP(w http.ResponseWriter, req *http.Request)
Conforms to the http.Handler interface.
func (*NapNap) SetTemplate ¶
SetTemplate function allows user to set their own template instance.
func (*NapNap) Use ¶
func (nap *NapNap) Use(mHandler MiddlewareHandler)
Use adds a Handler onto the middleware stack. Handlers are invoked in the order they are added to a NapNap.
func (*NapNap) UseFunc ¶
func (nap *NapNap) UseFunc(aFunc func(c *Context, next HandlerFunc))
UseFunc adds an anonymous function onto middleware stack.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter ContentLength() int Status() int // contains filtered or unexported methods }
ResponseWriter wraps the original http.ResponseWriter
func NewResponseWriter ¶
func NewResponseWriter() ResponseWriter
NewResponseWriter returns a ResponseWriter which wraps the writer