Documentation ¶
Overview ¶
Package baa provides an express Go web framework.
package main import ( "github.com/go-baa/baa" ) func main() { app := baa.New() app.Get("/", func(c *baa.Context) { c.String(200, "Hello World!") }) app.Run(":8001") }
Index ¶
- Constants
- Variables
- func IsParamChar(c byte) bool
- type Baa
- func (b *Baa) Any(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Debug() bool
- func (b *Baa) DefaultNotFoundHandler(c *Context)
- func (b *Baa) Delete(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Error(err error, c *Context)
- func (b *Baa) Get(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) GetDI(name string) interface{}
- func (b *Baa) Group(pattern string, f func(), h ...HandlerFunc)
- func (b *Baa) Head(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Logger() Logger
- func (b *Baa) NotFound(c *Context)
- func (b *Baa) Options(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Patch(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Post(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Put(pattern string, h ...HandlerFunc) RouteNode
- func (b *Baa) Render() Renderer
- func (b *Baa) Route(pattern, methods string, h ...HandlerFunc) RouteNode
- func (b *Baa) Router() Router
- func (b *Baa) Run(addr string)
- func (b *Baa) RunServer(s *http.Server)
- func (b *Baa) RunTLS(addr, certfile, keyfile string)
- func (b *Baa) RunTLSServer(s *http.Server, crtFile, keyFile string)
- func (b *Baa) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (b *Baa) Server(addr string) *http.Server
- func (b *Baa) SetAutoHead(v bool)
- func (b *Baa) SetAutoTrailingSlash(v bool)
- func (b *Baa) SetDI(name string, h interface{})
- func (b *Baa) SetDIer(v DIer)
- func (b *Baa) SetDebug(v bool)
- func (b *Baa) SetError(h ErrorHandleFunc)
- func (b *Baa) SetNotFound(h HandlerFunc)
- func (b *Baa) Static(prefix string, dir string, index bool, h HandlerFunc)
- func (b *Baa) StaticFile(pattern string, path string) RouteNode
- func (b *Baa) URLFor(name string, args ...interface{}) string
- func (b *Baa) Use(m ...Middleware)
- func (b *Baa) Websocket(pattern string, h func(*websocket.Conn)) RouteNode
- type Context
- func (c *Context) Baa() *Baa
- func (c *Context) Body() *RequestBody
- func (c *Context) Break()
- func (c *Context) DI(name string) interface{}
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Error(err error)
- func (c *Context) Fetch(tpl string) ([]byte, error)
- func (c *Context) Get(key string) interface{}
- func (c *Context) GetCookie(name string) string
- func (c *Context) GetCookieBool(name string) bool
- func (c *Context) GetCookieFloat64(name string) float64
- func (c *Context) GetCookieInt(name string) int
- func (c *Context) GetCookieInt32(name string) int32
- func (c *Context) GetCookieInt64(name string) int64
- func (c *Context) GetFile(name string) (multipart.File, *multipart.FileHeader, error)
- func (c *Context) Gets() map[string]interface{}
- func (c *Context) HTML(code int, tpl string)
- func (c *Context) IsAJAX() bool
- func (c *Context) IsMobile() bool
- func (c *Context) JSON(code int, v interface{})
- func (c *Context) JSONP(code int, callback string, v interface{})
- func (c *Context) JSONString(v interface{}) (string, error)
- func (c *Context) Next()
- func (c *Context) NotFound()
- func (c *Context) Param(name string) string
- func (c *Context) ParamBool(name string) bool
- func (c *Context) ParamFloat(name string) float64
- func (c *Context) ParamInt(name string) int
- func (c *Context) ParamInt32(name string) int32
- func (c *Context) ParamInt64(name string) int64
- func (c *Context) Params() map[string]string
- func (c *Context) ParseForm(maxSize int64) error
- func (c *Context) Posts() map[string]interface{}
- func (c *Context) Query(name string) string
- func (c *Context) QueryBool(name string) bool
- func (c *Context) QueryEscape(name string) string
- func (c *Context) QueryFloat(name string) float64
- func (c *Context) QueryInt(name string) int
- func (c *Context) QueryInt32(name string) int32
- func (c *Context) QueryInt64(name string) int64
- func (c *Context) QueryJSON(v interface{}) error
- func (c *Context) QueryStrings(name string) []string
- func (c *Context) QueryTrim(name string) string
- func (c *Context) QueryXML(v interface{}) error
- func (c *Context) Querys() map[string]interface{}
- func (c *Context) Redirect(code int, url string) error
- func (c *Context) Referer() string
- func (c *Context) RemoteAddr() string
- func (c *Context) Render(code int, tpl string)
- func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
- func (c *Context) RouteName() string
- func (c *Context) SaveToFile(name, savePath string) error
- func (c *Context) Set(key string, v interface{})
- func (c *Context) SetCookie(name string, value string, others ...interface{})
- func (c *Context) SetParam(name, value string)
- func (c *Context) String(code int, s string)
- func (c *Context) Text(code int, s []byte)
- func (c *Context) URL(hasQuery bool) string
- func (c *Context) UserAgent() string
- func (c *Context) Value(key interface{}) interface{}
- func (c *Context) XML(code int, v interface{})
- type DI
- type DIer
- type ErrorHandleFunc
- type HandlerFunc
- type Logger
- type Middleware
- type Node
- type Render
- type Renderer
- type RequestBody
- type Response
- func (r *Response) CloseNotify() <-chan bool
- func (r *Response) Flush()
- func (r *Response) GetWriter() io.Writer
- func (r *Response) Header() http.Header
- func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (r *Response) Pusher() (http.Pusher, bool)
- func (r *Response) SetWriter(w io.Writer)
- func (r *Response) Size() int64
- func (r *Response) Status() int
- func (r *Response) Write(b []byte) (int, error)
- func (r *Response) WriteHeader(code int)
- func (r *Response) Wrote() bool
- type RouteNode
- type Router
- type Tree
- func (t *Tree) Add(method, pattern string, handlers []HandlerFunc) RouteNode
- func (t *Tree) GroupAdd(pattern string, f func(), handlers []HandlerFunc)
- func (t *Tree) Match(method, pattern string, c *Context) ([]HandlerFunc, string)
- func (t *Tree) NamedRoutes() map[string]string
- func (t *Tree) Routes() map[string][]string
- func (t *Tree) SetAutoHead(v bool)
- func (t *Tree) SetAutoTrailingSlash(v bool)
- func (t *Tree) URLFor(name string, args ...interface{}) string
Constants ¶
const ( // DEV mode DEV = "development" // PROD mode PROD = "production" // TEST mode TEST = "test" )
const ( // CharsetUTF8 ... CharsetUTF8 = "charset=utf-8" // MediaTypes ApplicationJSON = "application/json" ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8 ApplicationJavaScript = "application/javascript" ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8 ApplicationXML = "application/xml" ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8 ApplicationForm = "application/x-www-form-urlencoded" ApplicationProtobuf = "application/protobuf" TextHTML = "text/html" TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8 TextPlain = "text/plain" TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8 MultipartForm = "multipart/form-data" )
const ( GET int = iota POST PUT DELETE PATCH OPTIONS HEAD // RouteLength route table length RouteLength )
Variables ¶
var ( // ErrJSONPayloadEmpty is returned when the JSON payload is empty. ErrJSONPayloadEmpty = errors.New("JSON payload is empty") // ErrXMLPayloadEmpty is returned when the XML payload is empty. ErrXMLPayloadEmpty = errors.New("XML payload is empty") )
var Env string
Env default application runtime environment
var RouterMethodName = map[int]string{ GET: "GET", POST: "POST", PUT: "PUT", DELETE: "DELETE", PATCH: "PATCH", OPTIONS: "OPTIONS", HEAD: "HEAD", }
RouterMethodName declare method name with route table key
var RouterMethods = map[string]int{ "GET": GET, "POST": POST, "PUT": PUT, "DELETE": DELETE, "PATCH": PATCH, "OPTIONS": OPTIONS, "HEAD": HEAD, }
RouterMethods declare method key in route table
Functions ¶
func IsParamChar ¶ added in v1.2.13
IsParamChar check the char can used for route params a-z->65:90, A-Z->97:122, 0-9->48->57, _->95
Types ¶
type Baa ¶
type Baa struct {
// contains filtered or unexported fields
}
Baa provlider an application
func (*Baa) Any ¶
func (b *Baa) Any(pattern string, h ...HandlerFunc) RouteNode
Any is a shortcut for b.Router().handle("*", pattern, handlers)
func (*Baa) DefaultNotFoundHandler ¶
DefaultNotFoundHandler invokes the default HTTP error handler.
func (*Baa) Delete ¶
func (b *Baa) Delete(pattern string, h ...HandlerFunc) RouteNode
Delete is a shortcut for b.Route(pattern, "DELETE", handlers)
func (*Baa) Get ¶
func (b *Baa) Get(pattern string, h ...HandlerFunc) RouteNode
Get is a shortcut for b.Route(pattern, "GET", handlers)
func (*Baa) Group ¶
func (b *Baa) Group(pattern string, f func(), h ...HandlerFunc)
Group registers a list of same prefix route
func (*Baa) Head ¶
func (b *Baa) Head(pattern string, h ...HandlerFunc) RouteNode
Head is a shortcut forb.Route(pattern, "Head", handlers)
func (*Baa) Options ¶
func (b *Baa) Options(pattern string, h ...HandlerFunc) RouteNode
Options is a shortcut for b.Route(pattern, "Options", handlers)
func (*Baa) Patch ¶
func (b *Baa) Patch(pattern string, h ...HandlerFunc) RouteNode
Patch is a shortcut for b.Route(pattern, "PATCH", handlers)
func (*Baa) Post ¶
func (b *Baa) Post(pattern string, h ...HandlerFunc) RouteNode
Post is a shortcut for b.Route(pattern, "POST", handlers)
func (*Baa) Put ¶
func (b *Baa) Put(pattern string, h ...HandlerFunc) RouteNode
Put is a shortcut for b.Route(pattern, "Put", handlers)
func (*Baa) Route ¶
func (b *Baa) Route(pattern, methods string, h ...HandlerFunc) RouteNode
Route is a shortcut for same handlers but different HTTP methods.
Example:
baa.Route("/", "GET,POST", h)
func (*Baa) RunTLSServer ¶
RunTLSServer runs a custom server with TLS configuration.
func (*Baa) SetAutoHead ¶
SetAutoHead sets the value who determines whether add HEAD method automatically when GET method is added. Combo router will not be affected by this value.
func (*Baa) SetAutoTrailingSlash ¶ added in v1.2.8
SetAutoTrailingSlash optional trailing slash.
func (*Baa) SetNotFound ¶ added in v1.2.9
func (b *Baa) SetNotFound(h HandlerFunc)
SetNotFound set not found route handler
func (*Baa) Static ¶
func (b *Baa) Static(prefix string, dir string, index bool, h HandlerFunc)
Static set static file route h used for set Expries ...
func (*Baa) StaticFile ¶ added in v1.2.21
StaticFile shortcut for serve file
type Context ¶
Context provlider a HTTP context for baa context contains reqest, response, header, cookie and some content type.
func NewContext ¶ added in v1.2.13
NewContext create a http context
func (*Context) Body ¶
func (c *Context) Body() *RequestBody
Body get raw request body and return RequestBody
func (*Context) Break ¶ added in v1.2.4
func (c *Context) Break()
Break break the handles chain and Immediate return
func (*Context) Deadline ¶ added in v1.3.1
Deadline returns that there is no deadline (ok==false) when c.Req has no Context.
func (*Context) Done ¶ added in v1.3.1
func (c *Context) Done() <-chan struct{}
Done returns nil (chan which will wait forever) when c.Req has no Context.
func (*Context) Fetch ¶
Fetch render data by html template engine use context.store and returns data
func (*Context) GetCookieBool ¶
GetCookieBool returns cookie result in float64 type.
func (*Context) GetCookieFloat64 ¶
GetCookieFloat64 returns cookie result in float64 type.
func (*Context) GetCookieInt ¶
GetCookieInt returns cookie result in int type.
func (*Context) GetCookieInt32 ¶ added in v1.2.12
GetCookieInt32 returns cookie result in int32 type.
func (*Context) GetCookieInt64 ¶
GetCookieInt64 returns cookie result in int64 type.
func (*Context) GetFile ¶
GetFile returns information about user upload file by given form field name.
func (*Context) HTML ¶
HTML write render data by html template engine use context.store it is a alias of c.Render
func (*Context) JSONString ¶
JSONString return string by Marshal interface
func (*Context) Next ¶
func (c *Context) Next()
Next execute next handler handle middleware first, last execute route handler if something wrote to http, break chain and return
func (*Context) NotFound ¶ added in v1.2.9
func (c *Context) NotFound()
NotFound invokes the registered HTTP NotFound handler.
func (*Context) ParamFloat ¶
ParamFloat get route param from context and format to float64
func (*Context) ParamInt32 ¶ added in v1.2.12
ParamInt32 get route param from context and format to int32
func (*Context) ParamInt64 ¶
ParamInt64 get route param from context and format to int64
func (*Context) ParseForm ¶ added in v1.2.20
ParseForm parses a request body as multipart/form-data or parses the raw query from the URL and updates r.Form.
func (*Context) QueryEscape ¶
QueryEscape returns escapred query result.
func (*Context) QueryFloat ¶
QueryFloat get a param from http.Request.Form and format to float64
func (*Context) QueryInt32 ¶ added in v1.2.12
QueryInt32 get a param from http.Request.Form and format to int32
func (*Context) QueryInt64 ¶
QueryInt64 get a param from http.Request.Form and format to int64
func (*Context) QueryStrings ¶
QueryStrings get a group param from http.Request.Form and format to string slice
func (*Context) RemoteAddr ¶
RemoteAddr returns more real IP address.
func (*Context) Reset ¶ added in v1.2.13
func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
Reset ...
func (*Context) SaveToFile ¶
SaveToFile reads a file from request by field name and saves to given path.
func (*Context) SetCookie ¶
SetCookie sets given cookie value to response header. full params example: SetCookie(<name>, <value>, <max age>, <path>, <domain>, <secure>, <http only>)
type DI ¶
type DI struct {
// contains filtered or unexported fields
}
DI provlider a dependency injection service for baa
type ErrorHandleFunc ¶
ErrorHandleFunc HTTP error handleFunc
type HandlerFunc ¶
type HandlerFunc func(*Context)
HandlerFunc context handler func
func WrapHandlerFunc ¶ added in v1.2.13
func WrapHandlerFunc(h HandlerFunc) HandlerFunc
WrapHandlerFunc wrap for context handler chain
type Logger ¶
type Logger interface { Print(v ...interface{}) Printf(format string, v ...interface{}) Println(v ...interface{}) Fatal(v ...interface{}) Fatalf(format string, v ...interface{}) Fatalln(v ...interface{}) Panic(v ...interface{}) Panicf(format string, v ...interface{}) Panicln(v ...interface{}) }
Logger provlider a basic log interface for baa
type Node ¶ added in v1.2.13
type Node struct {
// contains filtered or unexported fields
}
Node is struct for named route
type RequestBody ¶
type RequestBody struct {
// contains filtered or unexported fields
}
RequestBody represents a request body.
func (*RequestBody) Bytes ¶
func (rb *RequestBody) Bytes() ([]byte, error)
Bytes reads and returns content of request body in bytes.
func (*RequestBody) ReadCloser ¶
func (rb *RequestBody) ReadCloser() io.ReadCloser
ReadCloser returns a ReadCloser for request body.
func (*RequestBody) String ¶
func (rb *RequestBody) String() (string, error)
String reads and returns content of request body in string.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response implement ResponseWriter
func (*Response) CloseNotify ¶
CloseNotify implements the http.CloseNotifier interface to allow detecting when the underlying connection has gone away. This mechanism can be used to cancel long operations on the server if the client has disconnected before the response is ready. See http.CloseNotifier(https://golang.org/pkg/net/http/#CloseNotifier)
func (*Response) Flush ¶
func (r *Response) Flush()
Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered data to the client. See http.Flusher(https://golang.org/pkg/net/http/#Flusher)
func (*Response) Header ¶
Header returns the header map that will be sent by WriteHeader. Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader (see example). To suppress implicit response headers, set their value to nil.
func (*Response) Hijack ¶
Hijack implements the http.Hijacker interface to allow an HTTP handler to take over the connection. See http.Hijacker(https://golang.org/pkg/net/http/#Hijacker)
func (*Response) Pusher ¶ added in v1.3.2
Pusher is the interface implemented by ResponseWriters that support HTTP/2 server push. For more background, see https://tools.ietf.org/html/rfc7540#section-8.2.
func (*Response) Write ¶
Write writes the data to the connection as part of an HTTP reply. If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.
func (*Response) WriteHeader ¶
WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.
type RouteNode ¶ added in v1.2.13
type RouteNode interface {
Name(name string)
}
RouteNode is an router node
type Router ¶
type Router interface { // SetAutoHead sets the value who determines whether add HEAD method automatically // when GET method is added. Combo router will not be affected by this value. SetAutoHead(v bool) // SetAutoTrailingSlash optional trailing slash. SetAutoTrailingSlash(v bool) // Match find matched route then returns handlers and name Match(method, uri string, c *Context) ([]HandlerFunc, string) // URLFor use named route return format url URLFor(name string, args ...interface{}) string // Add registers a new handle with the given method, pattern and handlers. Add(method, pattern string, handlers []HandlerFunc) RouteNode // GroupAdd registers a list of same prefix route GroupAdd(pattern string, f func(), handlers []HandlerFunc) // Routes returns registered route uri in a string slice Routes() map[string][]string // NamedRoutes returns named route uri in a string slice NamedRoutes() map[string]string }
Router is an router interface for baa
type Tree ¶ added in v1.2.13
type Tree struct {
// contains filtered or unexported fields
}
Tree provlider router for baa with radix tree
func (*Tree) Add ¶ added in v1.2.13
func (t *Tree) Add(method, pattern string, handlers []HandlerFunc) RouteNode
Add registers a new handle with the given method, pattern and handlers. add check training slash option.
func (*Tree) GroupAdd ¶ added in v1.2.13
func (t *Tree) GroupAdd(pattern string, f func(), handlers []HandlerFunc)
GroupAdd add a group route has same prefix and handle chain
func (*Tree) Match ¶ added in v1.2.13
func (t *Tree) Match(method, pattern string, c *Context) ([]HandlerFunc, string)
Match find matched route then returns handlers and name
func (*Tree) NamedRoutes ¶ added in v1.2.28
NamedRoutes returns named route uri in a string slice
func (*Tree) SetAutoHead ¶ added in v1.2.13
SetAutoHead sets the value who determines whether add HEAD method automatically when GET method is added. Combo router will not be affected by this value.
func (*Tree) SetAutoTrailingSlash ¶ added in v1.2.13
SetAutoTrailingSlash optional trailing slash.