vox: github.com/aisk/vox Index | Files | Directories

package vox

import "github.com/aisk/vox"

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

Package Files

application.go context.go doc.go handler.go logging.go request.go respond.go response.go route.go

Variables

var ErrNotAcceptable = errors.New("content is not acceptable")

ErrNotAcceptable is the error returns when vox found the reqeust is not acceptable.

type Application Uses

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 New Uses

func New() *Application

New returns a new vox Application.

func (*Application) Delete Uses

func (app *Application) Delete(path string, handler Handler)

Delete register a new path handler for DELETE method.

func (*Application) Get Uses

func (app *Application) Get(path string, handler Handler)

Get register a new path handler for GET method.

func (*Application) GetConfig Uses

func (app *Application) GetConfig(key string) string

GetConfig application level variable by key.

func (*Application) Head Uses

func (app *Application) Head(path string, handler Handler)

Head register a new path handler for HEAD method.

func (*Application) Options Uses

func (app *Application) Options(path string, handler Handler)

Options register a new path handler for OPTIONS method.

func (*Application) Patch Uses

func (app *Application) Patch(path string, handler Handler)

Patch register a new path handler for PATCH method.

func (*Application) Post Uses

func (app *Application) Post(path string, handler Handler)

Post register a new path handler for POST method.

func (*Application) Put Uses

func (app *Application) Put(path string, handler Handler)

Put register a new path handler for PUT method.

func (*Application) Route Uses

func (app *Application) Route(method string, path string, handler Handler)

Route will register a new path handler to a given path.

func (*Application) Run Uses

func (app *Application) Run(addr string) error

Run the Vox application.

func (*Application) ServeHTTP Uses

func (app *Application) ServeHTTP(rw http.ResponseWriter, rq *http.Request)

func (*Application) SetConfig Uses

func (app *Application) SetConfig(key, value string)

SetConfig sets an application level variable.

func (*Application) Trace Uses

func (app *Application) Trace(path string, handler Handler)

Trace register a new path handler for TRACE method.

func (*Application) Use Uses

func (app *Application) Use(handler Handler)

Use a vox middleware.

type Context Uses

type Context struct {
    context.Context
    // 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 Handler Uses

type Handler func(*Context, *Request, *Response)

Handler is the type for middlewares and route handlers to register.

type Request Uses

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 Uses

func (request *Request) JSON(v interface{}) error

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 Uses

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.

func (*Response) Redirect Uses

func (response *Response) Redirect(url string, code int)

Redirect request to another url.

func (*Response) SetCookie Uses

func (response *Response) SetCookie(cookie *http.Cookie)

SetCookie sets cookies on response.

Directories

PathSynopsis
examples/hello
examples/pprof
middlewares/pprof

Package vox imports 14 packages (graph) and is imported by 3 packages. Updated 2020-07-01. Refresh now. Tools for package owners.