zebra

package module
v0.0.0-...-6eeaf3e Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2015 License: MIT Imports: 13 Imported by: 0

README

zebra - 一个黑白分明的 Go Web 框架

zebra

zebra 是一个追求简单实用,但同时功能特性完整、易扩展、编码灵活自由的 Golang Web 框架。 它不依赖任何第三方包。

如何使用

使用 zebra 只需要创建一个 zebra 服务并启动它:

z := zebra.New()
z.Run()

zebra默认在 3000 端口运行,你也可以通过 z.RunOnAddr(":8888") 方法指定端口。另外, 还可以通过http.Server来配置完整的服务信息,以便让zebra按照你自定义的方式运行:

s := &http.Server{
    Addr:           ":8080",
    ReadTimeout:    10 * time.Second,
    WriteTimeout:   10 * time.Second,
    MaxHeaderBytes: 1 << 20,
}
z := zebra.NewWithServer(s)
z.Run()

路由

zebra的路由是满足 RESTful 规则的路由,但需要注意的是,它并不支持完整的HTTP Method, 我们只保留了一些常用的方法,如下:

"GET"
"POST"
"PUT"
"OPTIONS"
"DELETE"
"HEAD"

我们相信这些方法足够您实现一个完整的 RESTful Web 服务。但是如果它们真的不足以满足您的需求, 不要担心,zebra的路由中间件非常容易扩展。

创建 Router

使用 zebra.NewRouter() 方法即可创建一个Router.

r := zebra.NewRouter()
r.Get("/foo", func(c *zebra.Captain) {
    // do some thing..
})
app := zebra.New()
app.Use(r)
app.Run()
路由规则

zebra的路由规则非常简单,使用它时你只需要记住一个能满足 80% 场景的规则 :path 即可,例如我们为用户信息创建如下Router:

// http GET:  localhost:8888/user/zhangsan
r.Get("/user/:name", func(c *zebra.Captain) {
    // 使用 c.Path("name") 得到请求中的路径参数 'zhangsan'
})

我们在实践中发现,这种方式能解决绝大多数路由规则需求,但并不是万能的。 你总会遇到一些特殊的匹配规则,此时,只需要编写自己的正则即可。 zebra路由直接支持正则表达式,只需要将它放在一对花括号中:

// http GET:  localhost:8888/bar/badman123
r.Get("/bar/:userName{badman[\\d]+}", func(c *Captain) {
    // c.Path("userName")  --->  badman123
})
// 请求 localhost:8888/bar/goodboy1314 则不会被匹配

日志

创建日志模块并注册,即可记录 zebra 的访问日志:

logger := NewLogger()
app.Use(r)

zebra 提供两种内置的日志格式:commoncombined. 默认使用 combined 格式,它会记录类似 apache http combined 格式的日志。

使用 SetFormat(string) 方法设置日志格式,如 logger.SetFormat("common")。 也可以使用自定义格式,如:

logger := zebra.NewLogger()
logger.SetFormat("[:date] :method :url HTTP/:http-version (:response-time)")

favicon

静态服务

如何编写自己的中间件

整合其它应用

zebra灵活的功能特性,允许你轻松整合 http.ServeMux 甚至其它第三方应用,如 martini .

mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello zebra"))
})

z := zebra.New()
z.SetName("Zebra")
z.Use(mux)
z.Run()

另外,一个已完成的zebra应用,也可以被轻松地整合到一个全新的zebra应用,构成它的一部分。

subApp := zebra.New()
subApp.SetName("bar")
// ...

app := zebra.New()
app.Use(subApp)
app.Run()

联系我们

有问题可以直接创建一个issue

PS: zebra目前处于开发阶段,欢迎有同样想法的朋友加入。

Documentation

Index

Constants

View Source
const (
	MethodAny int = iota
	MethodGet
	MethodPost
	MethodPut
	MethodDelete
	MethodHead
	MethodOptions
)

一组表示Http Method的常量。其中Any表示任意一种Http Method.

Variables

This section is empty.

Functions

func HttpMethodCode

func HttpMethodCode(method string) int

通过请求的 HTTP Method 获取对应的 code.

Types

type AccessLogger

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

服务访问日志

func NewLogger

func NewLogger() *AccessLogger

创建服务访问日志中间件

func (*AccessLogger) Callback

func (logger *AccessLogger) Callback(cxt *Context)

func (*AccessLogger) EnableImmediate

func (logger *AccessLogger) EnableImmediate()

开启即时模式:请求时输出日志而不是响应时输出

func (*AccessLogger) Excute

func (logger *AccessLogger) Excute(cxt *Context) bool

TODO 不应该阻塞主程序,日志压入队列中异步执行

func (*AccessLogger) SetFormat

func (logger *AccessLogger) SetFormat(format string)

设置日志格式,默认为 Combined 格式

func (*AccessLogger) SetLogger

func (logger *AccessLogger) SetLogger(l *log.Logger)

使用给定的 Logger 记录服务访问日志

type Captain

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

Router 上下文对象

func (*Captain) Path

func (c *Captain) Path(pathname string) string

func (*Captain) Status

func (e *Captain) Status(code int)

func (*Captain) WriteJSON

func (e *Captain) WriteJSON(data interface{})

type Context

type Context struct {
	Method  int    // Http Method
	Urlpath string // *http.Request 中的 URL Path

	Transfer map[string]interface{}
	// contains filtered or unexported fields
}

Context 是 Middleware 的上下文对象

func (*Context) Logger

func (c *Context) Logger() *log.Logger

func (*Context) OriginalRequest

func (c *Context) OriginalRequest() *http.Request

获取原始的 *http.Request

func (*Context) OriginalResponseWriter

func (c *Context) OriginalResponseWriter() http.ResponseWriter

获取原始的 http.ResponseWriter

func (*Context) Request

func (c *Context) Request() *http.Request

TODO

func (*Context) ResponseWriter

func (c *Context) ResponseWriter() http.ResponseWriter

func (*Context) Zebra

func (c *Context) Zebra() *Zebra

type Middleware

type Middleware interface {
	// Excute 用来执行当前 Middleware,
	// 返回 true 表示继续执行后续 Middleware,
	// 返回 false 阻止后续 Middleware 的执行。
	Excute(*Context) bool
}

zebra中的一切组件均为一个 Middleware.

func Mount

func Mount(prefix string, m Middleware) Middleware

Mount 函数用于将一个 Middleware 挂载到一个路径前缀上。 该 Middleware 只会在指定 prefix 的URL子段下执行。

func ServeFavicon

func ServeFavicon(path string) Middleware

func Wrap

func Wrap(handler http.Handler) Middleware

type MiddlewareCallback

type MiddlewareCallback interface {
	Middleware
	Callback(*Context)
}

type Router

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

路由中间件 Router .

func NewRouter

func NewRouter() *Router

创建 Router 中间件

func (*Router) Any

func (r *Router) Any(path string, h ...func(c *Captain))

func (*Router) Delete

func (r *Router) Delete(path string, h ...func(c *Captain))

func (*Router) Excute

func (r *Router) Excute(c *Context) bool

func (*Router) Get

func (r *Router) Get(path string, h ...func(c *Captain))

func (*Router) Options

func (r *Router) Options(path string, h ...func(c *Captain))

func (*Router) Post

func (r *Router) Post(path string, h ...func(c *Captain))

func (*Router) Put

func (r *Router) Put(path string, h ...func(c *Captain))

type TestHelper

type TestHelper interface {
	AssertTrue(bool)
	AssertFalse(bool)
	AssertEqual(string, string)

	NewRequest(string, string) *http.Request
}

func NewTestHelper

func NewTestHelper(t *testing.T) TestHelper

type Zebra

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

func New

func New() *Zebra

func NewWithServer

func NewWithServer(server *http.Server) *Zebra

func (*Zebra) Name

func (z *Zebra) Name() string

func (*Zebra) Run

func (z *Zebra) Run()

func (*Zebra) RunOnAddr

func (z *Zebra) RunOnAddr(addr string)

func (*Zebra) ServeHTTP

func (z *Zebra) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Zebra) SetLogger

func (z *Zebra) SetLogger(logger *log.Logger)

func (*Zebra) SetName

func (z *Zebra) SetName(name string)

func (*Zebra) Use

func (z *Zebra) Use(m interface{})

func (*Zebra) UseFullFeatures

func (z *Zebra) UseFullFeatures()

Jump to

Keyboard shortcuts

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