tango

package module
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2015 License: MIT Imports: 26 Imported by: 0

README

Tango Build Status 简体中文

Tango Logo

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

Current version: v0.2.8 Version History

Getting Started

To install Tango:

go get github.com/lunny/tango

The very basic usage of Tango:

package main

import "github.com/lunny/tango"

func main() {
    t := tango.Classic()
    t.Get("/", func() string {
        return "Hello tango!"
    })
    t.Run()
}

Then visit http://localhost:8000 on your browser. Of course, tango support struct form also.

package main

import "github.com/lunny/tango"

type Action struct {
    tango.Json
}

func (Action) Get() map[string]string {
    return map[string]string{
        "say": "Hello tango!",
    }
}

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

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

More document, please see godoc and Wiki

Features

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

Middlewares

Middlewares allow you easily plugin/unplugin features for your Tango applications.

There are already many middlewares to simplify your work:

  • recovery - recover after panic
  • compress - Gzip & Deflate compression
  • static - Serves static files
  • logger - Log the request & inject Logger to action struct
  • param - get the router parameters
  • return - Handle the returned value smartlly
  • request - Inject request to action struct
  • response - Inject response to action struct
  • session - Build Status Session manager
  • xsrf - Build Status Generates and validates csrf tokens
  • binding - Build Status Bind and validates forms
  • renders - Build Status Go template engine
  • dispatch - Build Status Multiple Application support on one server
  • tpongo2 - Build Status Pongo2 teamplte engine support
  • captcha - Build Status Captcha
  • events - Build Status Before and After
  • flash - Build Status Share data between requests
  • debug - Build Status show detail debug infomaton on log

Getting Help

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
)
View Source
const (
	Dev = iota
	Prod
)

Variables

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

	PoolSize = 800
)
View Source
var (
	ClassicHandlers = []Handler{
		Logging(),
		Recovery(true),
		Compresses([]string{".js", ".css", ".html", ".htm"}),
		Static(StaticOptions{Prefix: "public"}),
		Return(),
		Responses(),
		Requests(),
		Param(),
		Contexts(),
	}
)
View Source
var (
	Env = Dev
)

Functions

func NewCookie

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

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 NewContext

func NewContext(
	tan *Tango,
	req *http.Request,
	resp ResponseWriter,
	logger Logger) *Context

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

func (ctx *Context) Cookies() Cookies

func (*Context) Download

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

func (*Context) HandleError

func (ctx *Context) HandleError()

func (*Context) Invoke

func (ctx *Context) Invoke()

func (*Context) Next

func (ctx *Context) Next()

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

func (ctx *Context) Params() url.Values

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) 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 {
	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 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 []string, url string, c interface{})

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

func Recovery

func Recovery(debug bool) HandlerFunc

func Requests

func Requests() HandlerFunc

func Responses

func Responses() 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 HttpResponser

type HttpResponser interface {
	SetResponse(http.ResponseWriter)
}

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(url.Values)
}

type Params

type Params struct {
	url.Values
}

func (*Params) SetParams

func (p *Params) SetParams(u url.Values)

type PathType

type PathType int
const (
	StaticPath PathType = iota + 1
	NamedPath
	RegexpPath
)

type Req

type Req struct {
	*http.Request
}

func (*Req) SetRequest

func (r *Req) SetRequest(req *http.Request)

type Requester

type Requester interface {
	SetRequest(*http.Request)
}

type Resp

type Resp struct {
	ResponseWriter
}

func (*Resp) SetResponse

func (resp *Resp) SetResponse(r ResponseWriter)

type ResponseTyper

type ResponseTyper interface {
	ResponseType() int
}

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Flusher
	// 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.

func NewResponseWriter

func NewResponseWriter(rw http.ResponseWriter) ResponseWriter

NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter

type Responser

type Responser interface {
	SetResponse(ResponseWriter)
}

type Route

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

Route

func NewRoute

func NewRoute(r string, t reflect.Type,
	method reflect.Value, tp RouteType) *Route

func (*Route) IsStruct

func (r *Route) IsStruct() bool

func (*Route) Method

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

func (*Route) PathType

func (r *Route) PathType() PathType

func (*Route) RouteType

func (r *Route) RouteType() RouteType

type RouteType

type RouteType int
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{})
	Match(requestPath, method string) (*Route, url.Values)
}

func NewRouter

func NewRouter() Router

type StaticOptions

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

type Tango

type Tango struct {
	Router
	Mode int

	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(addrs ...string)

func (*Tango) RunTLS

func (t *Tango) RunTLS(certFile, keyFile string, addrs ...string)

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