tango

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2016 License: MIT Imports: 29 Imported by: 0

README

Tango Build Status 简体中文

Tango Logo

Package tango is a micro & pluggable web framework for Go.

Current version: v0.4.6 Version History

Getting Started

To install Tango:

go get github.com/lunny/tango

A classic usage of Tango below:

package main

import (
    "errors"
    "github.com/lunny/tango"
)

type Action struct {
    tango.Json
}

func (Action) Get() interface{} {
    if true {
        return map[string]string{
            "say": "Hello tango!",
        }
    }
    return errors.New("something error")
}

func main() {
    t := tango.Classic()
    t.Get("/", new(Action))
    t.Run()
}

Then visit http://localhost:8000 on your browser. You will get

{"say":"Hello tango!"}

If you change true after if to false, then you will get

{"err":"something error"}

This code will automatically convert returned map or error to a json because we has an embedded struct tango.Json.

Features

  • Powerful routing & Flexible routes combinations.
  • Directly integrate with existing services.
  • Easy to plugin features with modular design.
  • High performance dependency injection embedded.

Middlewares

Middlewares allow you easily plugin features for your Tango applications.

There are already many middlewares to simplify your work:

Documentation

Discuss

Cases

License

This project is under BSD License. See the LICENSE file for the full license text.

Documentation

Index

Constants

View Source
const (
	HeaderAcceptEncoding  = "Accept-Encoding"
	HeaderContentEncoding = "Content-Encoding"
	HeaderContentLength   = "Content-Length"
	HeaderContentType     = "Content-Type"
	HeaderVary            = "Vary"
)
View Source
const (
	AutoResponse = iota
	JsonResponse
	XmlResponse
)

Variables

View Source
var (
	SupportMethods = []string{
		"GET",
		"POST",
		"HEAD",
		"DELETE",
		"PUT",
		"OPTIONS",
		"TRACE",
		"PATCH",
	}

	PoolSize = 10
)
View Source
var (
	ClassicHandlers = []Handler{
		Logging(),
		Recovery(false),
		Compresses([]string{}),
		Static(StaticOptions{Prefix: "public"}),
		Return(),
		Param(),
		Contexts(),
	}
)

Functions

func Dir added in v0.4.4

func Dir(dir string) func(ctx *Context)

func File added in v0.4.4

func File(path string) func(ctx *Context)

func GetAddress added in v0.4.6

func GetAddress(args ...interface{}) string

func NewCookie

func NewCookie(name string, value string, age ...int64) *http.Cookie

func NewRouter

func NewRouter() (r *router)

func NewSecureCookie

func NewSecureCookie(secret, name string, val string, age ...int64) *http.Cookie

func Version

func Version() string

Types

type AbortError

type AbortError interface {
	error
	Code() int
}

func Abort

func Abort(code int, content ...string) AbortError

func Forbidden

func Forbidden(content ...string) AbortError

func InternalServerError

func InternalServerError(content ...string) AbortError

func NotFound

func NotFound(content ...string) AbortError

func NotSupported

func NotSupported(content ...string) AbortError

func Unauthorized

func Unauthorized(content ...string) AbortError

type Compress

type Compress struct{}

func (Compress) CompressType

func (Compress) CompressType() string

type Compresser

type Compresser interface {
	CompressType() string
}

type Context

type Context struct {
	Logger

	ResponseWriter

	Result interface{}
	// contains filtered or unexported fields
}

func (*Context) Abort

func (ctx *Context) Abort(status int, body string)

Abort is a helper method that sends an HTTP header and an optional body. It is useful for returning 4xx or 5xx errors. Once it has been called, any return value from the handler will not be written to the response.

func (*Context) Action

func (ctx *Context) Action() interface{}

func (*Context) Body added in v0.5.0

func (ctx *Context) Body() ([]byte, error)

func (*Context) Cookie added in v0.5.0

func (ctx *Context) Cookie(key string, defaults ...string) string

func (*Context) CookieBool added in v0.5.0

func (ctx *Context) CookieBool(key string, defaults ...bool) bool

func (*Context) CookieEscape added in v0.5.0

func (ctx *Context) CookieEscape(key string, defaults ...string) string

func (*Context) CookieFloat32 added in v0.5.0

func (ctx *Context) CookieFloat32(key string, defaults ...float32) float32

func (*Context) CookieFloat64 added in v0.5.0

func (ctx *Context) CookieFloat64(key string, defaults ...float64) float64

func (*Context) CookieInt added in v0.5.0

func (ctx *Context) CookieInt(key string, defaults ...int) int

func (*Context) CookieInt32 added in v0.5.0

func (ctx *Context) CookieInt32(key string, defaults ...int32) int32

func (*Context) CookieInt64 added in v0.5.0

func (ctx *Context) CookieInt64(key string, defaults ...int64) int64

func (*Context) CookieUint added in v0.5.0

func (ctx *Context) CookieUint(key string, defaults ...uint) uint

func (*Context) CookieUint32 added in v0.5.0

func (ctx *Context) CookieUint32(key string, defaults ...uint32) uint32

func (*Context) CookieUint64 added in v0.5.0

func (ctx *Context) CookieUint64(key string, defaults ...uint64) uint64

func (*Context) Cookies

func (ctx *Context) Cookies() Cookies

func (*Context) DecodeJson added in v0.5.0

func (ctx *Context) DecodeJson(obj interface{}) error

func (*Context) DecodeXml added in v0.5.0

func (ctx *Context) DecodeXml(obj interface{}) error

func (*Context) Download

func (ctx *Context) Download(fpath string) error

func (*Context) Form added in v0.5.0

func (ctx *Context) Form(key string, defaults ...string) string

func (*Context) FormBool added in v0.5.0

func (ctx *Context) FormBool(key string, defaults ...bool) bool

func (*Context) FormEscape added in v0.5.0

func (ctx *Context) FormEscape(key string, defaults ...string) string

func (*Context) FormFloat32 added in v0.5.0

func (ctx *Context) FormFloat32(key string, defaults ...float32) float32

func (*Context) FormFloat64 added in v0.5.0

func (ctx *Context) FormFloat64(key string, defaults ...float64) float64

func (*Context) FormInt added in v0.5.0

func (ctx *Context) FormInt(key string, defaults ...int) int

func (*Context) FormInt32 added in v0.5.0

func (ctx *Context) FormInt32(key string, defaults ...int32) int32

func (*Context) FormInt64 added in v0.5.0

func (ctx *Context) FormInt64(key string, defaults ...int64) int64

func (*Context) FormStrings added in v0.5.0

func (ctx *Context) FormStrings(key string, defaults ...[]string) []string

func (*Context) FormUint added in v0.5.0

func (ctx *Context) FormUint(key string, defaults ...uint) uint

func (*Context) FormUint32 added in v0.5.0

func (ctx *Context) FormUint32(key string, defaults ...uint32) uint32

func (*Context) FormUint64 added in v0.5.0

func (ctx *Context) FormUint64(key string, defaults ...uint64) uint64

func (*Context) Forms added in v0.4.6

func (ctx *Context) Forms() *Forms

func (*Context) HandleError

func (ctx *Context) HandleError()

func (*Context) IP added in v0.5.0

func (ctx *Context) IP() string

func (*Context) IsAjax added in v0.5.0

func (ctx *Context) IsAjax() bool

func (*Context) Next

func (ctx *Context) Next()

WARNING: don't invoke this method on action

func (*Context) NotFound

func (ctx *Context) NotFound(message ...string)

NotFound writes a 404 HTTP response

func (*Context) NotModified

func (ctx *Context) NotModified()

Notmodified writes a 304 HTTP response

func (*Context) Param added in v0.5.0

func (ctx *Context) Param(key string, defaults ...string) string

func (*Context) ParamBool added in v0.5.0

func (ctx *Context) ParamBool(key string, defaults ...bool) bool

func (*Context) ParamEscape added in v0.5.0

func (ctx *Context) ParamEscape(key string, defaults ...string) string

func (*Context) ParamFloat32 added in v0.5.0

func (ctx *Context) ParamFloat32(key string, defaults ...float32) float32

func (*Context) ParamFloat64 added in v0.5.0

func (ctx *Context) ParamFloat64(key string, defaults ...float64) float64

func (*Context) ParamInt added in v0.5.0

func (ctx *Context) ParamInt(key string, defaults ...int) int

func (*Context) ParamInt32 added in v0.5.0

func (ctx *Context) ParamInt32(key string, defaults ...int32) int32

func (*Context) ParamInt64 added in v0.5.0

func (ctx *Context) ParamInt64(key string, defaults ...int64) int64

func (*Context) ParamStrings added in v0.5.0

func (ctx *Context) ParamStrings(key string, defaults ...[]string) []string

func (*Context) ParamUint added in v0.5.0

func (ctx *Context) ParamUint(key string, defaults ...uint) uint

func (*Context) ParamUint32 added in v0.5.0

func (ctx *Context) ParamUint32(key string, defaults ...uint32) uint32

func (*Context) ParamUint64 added in v0.5.0

func (ctx *Context) ParamUint64(key string, defaults ...uint64) uint64

func (*Context) Params

func (ctx *Context) Params() *Params

func (*Context) Redirect

func (ctx *Context) Redirect(url string, status ...int)

func (*Context) Req

func (ctx *Context) Req() *http.Request

func (*Context) Route

func (ctx *Context) Route() *Route

func (*Context) SaveToFile added in v0.4.3

func (ctx *Context) SaveToFile(formName, savePath string) error

func (*Context) SecureCookies

func (ctx *Context) SecureCookies(secret string) Cookies

func (*Context) ServeFile

func (ctx *Context) ServeFile(path string) error

func (*Context) ServeJson

func (ctx *Context) ServeJson(obj interface{}) error

func (*Context) ServeXml

func (ctx *Context) ServeXml(obj interface{}) error

func (*Context) Unauthorized

func (ctx *Context) Unauthorized()

type Contexter

type Contexter interface {
	SetContext(*Context)
}

type Cookies

type Cookies interface {
	Set
	Get(string) *http.Cookie
	Set(*http.Cookie)
	Expire(string, time.Time)
	Del(string)
}

type Ctx

type Ctx struct {
	*Context
}

func (*Ctx) SetContext

func (c *Ctx) SetContext(ctx *Context)

type Deflate

type Deflate struct{}

func (Deflate) CompressType

func (Deflate) CompressType() string

type Forms added in v0.4.6

type Forms http.Request

Forms a new enhancement of http.Request

func (*Forms) Bool added in v0.4.6

func (f *Forms) Bool(key string) (bool, error)

func (*Forms) Escape added in v0.5.0

func (f *Forms) Escape(key string) (string, error)

func (*Forms) Float32 added in v0.4.6

func (f *Forms) Float32(key string) (float32, error)

func (*Forms) Float64 added in v0.4.6

func (f *Forms) Float64(key string) (float64, error)

func (*Forms) Int added in v0.4.6

func (f *Forms) Int(key string) (int, error)

func (*Forms) Int32 added in v0.4.6

func (f *Forms) Int32(key string) (int32, error)

func (*Forms) Int64 added in v0.4.6

func (f *Forms) Int64(key string) (int64, error)

func (*Forms) MustBool added in v0.4.6

func (f *Forms) MustBool(key string, defaults ...bool) bool

func (*Forms) MustEscape added in v0.5.0

func (f *Forms) MustEscape(key string, defaults ...string) string

func (*Forms) MustFloat32 added in v0.4.6

func (f *Forms) MustFloat32(key string, defaults ...float32) float32

func (*Forms) MustFloat64 added in v0.4.6

func (f *Forms) MustFloat64(key string, defaults ...float64) float64

func (*Forms) MustInt added in v0.4.6

func (f *Forms) MustInt(key string, defaults ...int) int

func (*Forms) MustInt32 added in v0.4.6

func (f *Forms) MustInt32(key string, defaults ...int32) int32

func (*Forms) MustInt64 added in v0.4.6

func (f *Forms) MustInt64(key string, defaults ...int64) int64

func (*Forms) MustString added in v0.4.6

func (f *Forms) MustString(key string, defaults ...string) string

func (*Forms) MustStrings added in v0.5.0

func (f *Forms) MustStrings(key string, defaults ...[]string) []string

func (*Forms) MustUint added in v0.4.6

func (f *Forms) MustUint(key string, defaults ...uint) uint

func (*Forms) MustUint32 added in v0.4.6

func (f *Forms) MustUint32(key string, defaults ...uint32) uint32

func (*Forms) MustUint64 added in v0.4.6

func (f *Forms) MustUint64(key string, defaults ...uint64) uint64

func (*Forms) String added in v0.4.6

func (f *Forms) String(key string) (string, error)

func (*Forms) Strings added in v0.5.0

func (f *Forms) Strings(key string) ([]string, error)

func (*Forms) Uint added in v0.4.6

func (f *Forms) Uint(key string) (uint, error)

func (*Forms) Uint32 added in v0.4.6

func (f *Forms) Uint32(key string) (uint32, error)

func (*Forms) Uint64 added in v0.4.6

func (f *Forms) Uint64(key string) (uint64, error)

func (*Forms) Values added in v0.5.0

func (f *Forms) Values() url.Values

type GZip

type GZip struct{}

func (GZip) CompressType

func (GZip) CompressType() string

type Group

type Group struct {
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup() *Group

func (*Group) Any

func (g *Group) Any(url string, c interface{})

func (*Group) Delete

func (g *Group) Delete(url string, c interface{})

func (*Group) Get

func (g *Group) Get(url string, c interface{})

func (*Group) Group

func (g *Group) Group(p string, o interface{})

func (*Group) Head

func (g *Group) Head(url string, c interface{})

func (*Group) Options

func (g *Group) Options(url string, c interface{})

func (*Group) Patch

func (g *Group) Patch(url string, c interface{})

func (*Group) Post

func (g *Group) Post(url string, c interface{})

func (*Group) Put

func (g *Group) Put(url string, c interface{})

func (*Group) Route

func (g *Group) Route(methods interface{}, url string, c interface{}, handlers ...Handler)

func (*Group) Trace

func (g *Group) Trace(url string, c interface{})

func (*Group) Use added in v0.2.9

func (g *Group) Use(handlers ...Handler)

type Handler

type Handler interface {
	Handle(*Context)
}

type HandlerFunc

type HandlerFunc func(ctx *Context)

func Compresses

func Compresses(exts []string) HandlerFunc

func Contexts

func Contexts() HandlerFunc

func Errors

func Errors() HandlerFunc

default errorhandler, you can use your self handler

func Logging

func Logging() HandlerFunc

func Param

func Param() HandlerFunc

func Prefix added in v0.2.9

func Prefix(prefix string, handler Handler) HandlerFunc

TODO: regex prefix

func Recovery

func Recovery(debug bool) HandlerFunc

func Return

func Return() HandlerFunc

func Static

func Static(opts ...StaticOptions) HandlerFunc

func WrapAfter

func WrapAfter(handler http.Handler) HandlerFunc

func WrapBefore

func WrapBefore(handler http.Handler) HandlerFunc

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(ctx *Context)

type Json

type Json struct{}

func (Json) ResponseType

func (Json) ResponseType() int

type Log

type Log struct {
	Logger
}

func (*Log) SetLogger

func (l *Log) SetLogger(log Logger)

type LogInterface

type LogInterface interface {
	SetLogger(Logger)
}

type Logger

type Logger interface {
	Debugf(format string, v ...interface{})
	Debug(v ...interface{})
	Infof(format string, v ...interface{})
	Info(v ...interface{})
	Warnf(format string, v ...interface{})
	Warn(v ...interface{})
	Errorf(format string, v ...interface{})
	Error(v ...interface{})
}

func NewLogger

func NewLogger(out io.Writer) Logger

type Paramer

type Paramer interface {
	SetParams([]param)
}

type Params

type Params []param

func (*Params) Bool added in v0.4.6

func (p *Params) Bool(key string) (bool, error)

func (*Params) Escape added in v0.5.0

func (p *Params) Escape(key string) (string, error)

func (*Params) Float32 added in v0.4.6

func (p *Params) Float32(key string) (float32, error)

func (*Params) Float64 added in v0.4.6

func (p *Params) Float64(key string) (float64, error)

func (*Params) Get added in v0.4.0

func (p *Params) Get(key string) string

func (*Params) Int added in v0.4.6

func (p *Params) Int(key string) (int, error)

func (*Params) Int32 added in v0.4.6

func (p *Params) Int32(key string) (int32, error)

func (*Params) Int64 added in v0.4.6

func (p *Params) Int64(key string) (int64, error)

func (*Params) MustBool added in v0.4.6

func (p *Params) MustBool(key string, defaults ...bool) bool

func (*Params) MustEscape added in v0.5.0

func (p *Params) MustEscape(key string, defaults ...string) string

func (*Params) MustFloat32 added in v0.4.6

func (p *Params) MustFloat32(key string, defaults ...float32) float32

func (*Params) MustFloat64 added in v0.4.6

func (p *Params) MustFloat64(key string, defaults ...float64) float64

func (*Params) MustInt added in v0.4.6

func (p *Params) MustInt(key string, defaults ...int) int

func (*Params) MustInt32 added in v0.4.6

func (p *Params) MustInt32(key string, defaults ...int32) int32

func (*Params) MustInt64 added in v0.4.6

func (p *Params) MustInt64(key string, defaults ...int64) int64

func (*Params) MustString added in v0.4.6

func (p *Params) MustString(key string, defaults ...string) string

func (*Params) MustStrings added in v0.5.0

func (p *Params) MustStrings(key string, defaults ...[]string) []string

func (*Params) MustUint added in v0.4.6

func (p *Params) MustUint(key string, defaults ...uint) uint

func (*Params) MustUint32 added in v0.4.6

func (p *Params) MustUint32(key string, defaults ...uint32) uint32

func (*Params) MustUint64 added in v0.4.6

func (p *Params) MustUint64(key string, defaults ...uint64) uint64

func (*Params) Set added in v0.4.2

func (p *Params) Set(key, value string)

func (*Params) SetParams

func (p *Params) SetParams(params []param)

func (*Params) String added in v0.4.6

func (p *Params) String(key string) (string, error)

func (*Params) Strings added in v0.5.0

func (p *Params) Strings(key string) ([]string, error)

func (*Params) Uint added in v0.4.6

func (p *Params) Uint(key string) (uint, error)

func (*Params) Uint32 added in v0.4.6

func (p *Params) Uint32(key string) (uint32, error)

func (*Params) Uint64 added in v0.4.6

func (p *Params) Uint64(key string) (uint64, error)

type ResponseTyper

type ResponseTyper interface {
	ResponseType() int
}

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	http.Hijacker
	// Status returns the status code of the response or 0 if the response has not been written.
	Status() int
	// Written returns whether or not the ResponseWriter has been written.
	Written() bool
	// Size returns the size of the response body.
	Size() int
}

ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route

func NewRoute

func NewRoute(v interface{}, t reflect.Type,
	method reflect.Value, tp RouteType, handlers []Handler) *Route

func (*Route) IsStruct

func (r *Route) IsStruct() bool

func (*Route) Method

func (r *Route) Method() reflect.Value

func (*Route) Raw added in v0.4.6

func (r *Route) Raw() interface{}

func (*Route) RouteType

func (r *Route) RouteType() RouteType

type RouteType

type RouteType byte
const (
	FuncRoute         RouteType = iota + 1 // func ()
	FuncHttpRoute                          // func (http.ResponseWriter, *http.Request)
	FuncReqRoute                           // func (*http.Request)
	FuncResponseRoute                      // func (http.ResponseWriter)
	FuncCtxRoute                           // func (*tango.Context)
	StructRoute                            // func (st) <Get>()
	StructPtrRoute                         // func (*struct) <Get>()
)

type Router

type Router interface {
	Route(methods interface{}, path string, handler interface{}, middlewares ...Handler)
	Match(requestPath, method string) (*Route, Params)
}

type Set added in v0.4.6

type Set interface {
	String(key string) (string, error)
	Int(key string) (int, error)
	Int32(key string) (int32, error)
	Int64(key string) (int64, error)
	Uint(key string) (uint, error)
	Uint32(key string) (uint32, error)
	Uint64(key string) (uint64, error)
	Float32(key string) (float32, error)
	Float64(key string) (float64, error)
	Bool(key string) (bool, error)

	MustString(key string, defaults ...string) string
	MustEscape(key string, defaults ...string) string
	MustInt(key string, defaults ...int) int
	MustInt32(key string, defaults ...int32) int32
	MustInt64(key string, defaults ...int64) int64
	MustUint(key string, defaults ...uint) uint
	MustUint32(key string, defaults ...uint32) uint32
	MustUint64(key string, defaults ...uint64) uint64
	MustFloat32(key string, defaults ...float32) float32
	MustFloat64(key string, defaults ...float64) float64
	MustBool(key string, defaults ...bool) bool
}

type StaticOptions

type StaticOptions struct {
	RootPath   string
	Prefix     string
	IndexFiles []string
	ListDir    bool
	FilterExts []string
}

type StatusResult added in v0.5.0

type StatusResult struct {
	Code   int
	Result interface{}
}

type Tango

type Tango struct {
	Router

	ErrHandler Handler
	// contains filtered or unexported fields
}

func Classic

func Classic(l ...Logger) *Tango

func New

func New(handlers ...Handler) *Tango

func NewWithLog

func NewWithLog(logger Logger, handlers ...Handler) *Tango

func (*Tango) Any

func (t *Tango) Any(url string, c interface{})

func (*Tango) Delete

func (t *Tango) Delete(url string, c interface{})

func (*Tango) Get

func (t *Tango) Get(url string, c interface{})

func (*Tango) Group

func (t *Tango) Group(p string, o interface{})

func (*Tango) Head

func (t *Tango) Head(url string, c interface{})

func (*Tango) Logger

func (t *Tango) Logger() Logger

func (*Tango) Options

func (t *Tango) Options(url string, c interface{})

func (*Tango) Patch

func (t *Tango) Patch(url string, c interface{})

func (*Tango) Post

func (t *Tango) Post(url string, c interface{})

func (*Tango) Put

func (t *Tango) Put(url string, c interface{})

func (*Tango) Run

func (t *Tango) Run(args ...interface{})

Run the http server. Listening on os.GetEnv("PORT") or 8000 by default.

func (*Tango) RunTLS

func (t *Tango) RunTLS(certFile, keyFile string, args ...interface{})

func (*Tango) ServeHTTP

func (t *Tango) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Tango) Trace

func (t *Tango) Trace(url string, c interface{})

func (*Tango) Use

func (t *Tango) Use(handlers ...Handler)

func (*Tango) UseHandler

func (t *Tango) UseHandler(handler http.Handler)

type Xml

type Xml struct{}

func (Xml) ResponseType

func (Xml) ResponseType() int

type XmlError

type XmlError struct {
	XMLName xml.Name `xml:"err"`
	Content string   `xml:"content"`
}

type XmlString

type XmlString struct {
	XMLName xml.Name `xml:"string"`
	Content string   `xml:"content"`
}

Jump to

Keyboard shortcuts

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