vox

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2022 License: MIT Imports: 12 Imported by: 0

README

VOX

Go Reference Build Status Codecov Go Report Card Maintainability Gitter chat

A golang web framework for humans, inspired by Koa heavily.

VoxLogo

Getting started

Installation

Using the go get power:

$ go get -u github.com/aisk/vox
Basic Web Application
package main

import (
	"fmt"
	"time"

	"github.com/aisk/vox"
)

func main() {
	app := vox.New()

	// custom middleware that add a x-response-time to the response header
	app.Use(func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
		start := time.Now()
		ctx.Next()
		duration := time.Now().Sub(start)
		res.Header.Set("X-Response-Time", fmt.Sprintf("%s", duration))
	})

	// router param
	app.Get("/hello/{name}", func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
		res.Body = "Hello, " + req.Params["name"] + "!"
	})

	app.Run("localhost:3000")
}

More Docs

https://aisk.github.io/vox/

Need Support?

If you need help for using vox, or have other questions, welcome to our gitter chat room.

About the Project

Vox is © 2016-2020 by aisk.

License

Vox is distributed by a MIT license.

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

Constants

This section is empty.

Variables

View Source
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 New

func New() *Application

New returns a new vox Application.

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) Run

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

Run the Vox application.

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.

func (*Application) Use

func (app *Application) Use(handler Handler)

Use a vox middleware.

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 Handler

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

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

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

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

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

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

Redirect request to another url.

func (*Response) SetCookie

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

SetCookie sets cookies on response.

Directories

Path Synopsis
examples
middlewares

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL