zebra is a simple but convenient framework for quickly developing RESTful APIs web applications/services in Go.
go get github.com/raythorn/zebra
package main
import "github.com/raythorn/zebra"
func main() {
zebra.Run()
}
go build test.go
./test
######Congratulations! You just built your first zebra app.
zebra provides RESTful APIs, you can easily writing RESTful hanlder with Get/Put/Post/Delete/Patch/Options/Head/Any. Take Get for example:
import (
"github.com/raythorn/zebra"
"github.com/raythorn/zebra/context"
)
func user_get(ctx *context.Context) {
ctx.WriteString("zebra")
}
zebra.Get("/user", user_get)
zebra provides a context which contains http.RespondWriter and http.Request for http, and a simple cache to store temporary data, such as http request header, request parametr along with the url and form, named regexps and, of course, custom variables. And it has several convenient APIs to handle http related jobs.
zebra supports fixed route and regular expression route.
zebra.Get("/user", handler) //fixed route
zebra.Get("/user/:id", handler) //regexp route, match /user/123 ..., and id will be set in context
zebra.Get("/user/(?P<name>exp)", handler) //named regexp route, name will be set in context
zebra supports group api with same function.
zebra.Group("/user"
zebra.GGet("", handler), //Match "/user"
zebra.GPut("", handler),
zebra.GSub("/friends",
zebra.GGet("", handler), //Match "/user/friends"
zebra.GPut("", handler),
),
)
GGet/GGPut/... is same as Get/Put... APIs, which add related route to group, and GSub can add a sub-group to current group.
Logger for zebra, it can print log to both console and file. It default logs to console, and you can user log.Add("file", "/tmp/log") to add log to file. Log file will be named with current date, and rotate to new file in 12:00pm, also cleanup older log files, system cached the latest files in a month.
zebra source code is licensed under the Apache Licence, Version 2.0.