Documentation ¶
Overview ¶
Package vox is a golang web framework for humans, inspired by Koa http://koajs.com heavily.
Introduction ¶
Vox is a web framework inspired by Koa, which aims to be a minimal and elegent library for web applications.
Installation
$ go get -u github.com/aisk/vox
Basic Example
package main import ( "fmt" "time" "github.com/aisk/vox" ) func main() { app := vox.New() // x-response-time app.Use(func(req *vox.Request, res *vox.Response) { start := time.Now() req.Next() duration := time.Now().Sub(start) res.Header.Set("X-Response-Time", fmt.Sprintf("%s", duration)) }) // logger app.Use(func(req *vox.Request, res *vox.Response) { req.Next() fmt.Printf("%s %s\n", req.Method, req.URL) }) // router param app.Get("/hello/{name}", func(req *vox.Request, res *vox.Response) { res.Body = "Hello, " + req.Params["name"] + "!" }) // response app.Get("/", func(req *vox.Request, res *vox.Response) { // get the query string name := req.URL.Query().Get("name") if name == "" { name = "World" } res.Body = "Hello, " + name + "!" }) app.Run("localhost:3000") }
Index ¶
- Variables
- type Application
- func (app *Application) Delete(path string, handler Handler)
- func (app *Application) Get(path string, handler Handler)
- func (app *Application) GetConfig(key string) string
- func (app *Application) Head(path string, handler Handler)
- func (app *Application) Options(path string, handler Handler)
- func (app *Application) Patch(path string, handler Handler)
- func (app *Application) Post(path string, handler Handler)
- func (app *Application) Put(path string, handler Handler)
- func (app *Application) Route(method string, path string, handler Handler)
- func (app *Application) Run(addr string) error
- func (app *Application) ServeHTTP(rw http.ResponseWriter, rq *http.Request)
- func (app *Application) SetConfig(key, value string)
- func (app *Application) Trace(path string, handler Handler)
- func (app *Application) Use(handler Handler)
- type Context
- type Handler
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
var ErrNotAcceptable = errors.New("content is not acceptable")
ErrNotAcceptable is the error returns when vox found the reqeust is not acceptable.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
An Application is a container which includes middlewares and config, and implemented the GO's net/http.Handler interface https://golang.org/pkg/net/http/#Handler.
func (*Application) Delete ¶
func (app *Application) Delete(path string, handler Handler)
Delete register a new path handler for DELETE method.
func (*Application) Get ¶
func (app *Application) Get(path string, handler Handler)
Get register a new path handler for GET method.
func (*Application) GetConfig ¶
func (app *Application) GetConfig(key string) string
GetConfig application level variable by key.
func (*Application) Head ¶
func (app *Application) Head(path string, handler Handler)
Head register a new path handler for HEAD method.
func (*Application) Options ¶
func (app *Application) Options(path string, handler Handler)
Options register a new path handler for OPTIONS method.
func (*Application) Patch ¶
func (app *Application) Patch(path string, handler Handler)
Patch register a new path handler for PATCH method.
func (*Application) Post ¶
func (app *Application) Post(path string, handler Handler)
Post register a new path handler for POST method.
func (*Application) Put ¶
func (app *Application) Put(path string, handler Handler)
Put register a new path handler for PUT method.
func (*Application) Route ¶
func (app *Application) Route(method string, path string, handler Handler)
Route will register a new path handler to a given path.
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(rw http.ResponseWriter, rq *http.Request)
func (*Application) SetConfig ¶
func (app *Application) SetConfig(key, value string)
SetConfig sets an application level variable.
func (*Application) Trace ¶
func (app *Application) Trace(path string, handler Handler)
Trace register a new path handler for TRACE method.
type Context ¶ added in v0.2.0
type Context struct { context.Context App *Application // Next will call the next handler / middleware to processing request. // It's the middleware's responsibility to call the Next function (or not). Next func() }
Context is vox's handler context, which implemented the context.Context interface by wrapping the context from current requests'. You should use it when you need the standard context.Context.
type Request ¶
type Request struct { *http.Request // Params the parameters which extracted from the route. // // If the registered route is "/hello/{name}", and the actual path which // visited is "/hello/jim", the Params should be map[string]{"name": "jim"} // // Multiple parameters with same key is invalid and will be ignored. Params map[string]string // contains filtered or unexported fields }
A Request object contains all the information from current HTTP client.
Request embedded the current request's raw *http.Request as it's field, so you can using all the fields and method of http.Request. see http://golang.org/pkg/net/http/#Request.
func (*Request) JSON ¶ added in v0.1.0
JSON is a helper to decode JSON request body to go value, with additional functionality to check the content type header from the request. If the content type header do not starts with "application/json" or decode errors, this function will return an error and set the response status code to 406.
type Response ¶
type Response struct { // Don't write headers, status and body from Response struct to client. In // the case of you're using the go's origin http.Response. DontRespond bool // Writer is the raw http.ResponseWriter for current request. You should // assign the Body / Status / Header value instead of using this field. Writer http.ResponseWriter // Body is the container for HTTP response's body. Body interface{} // The status code which will respond as the HTTP Response's status code. // 200 will be used as the default value if not set. Status int // Headers which will be written to the response. Header http.Header // contains filtered or unexported fields }
A Response object contains all the information which will written to current HTTP client.