Documentation
¶
Overview ¶
Package group provides to define grouped routing with httprouter.
Package group は httprouter にルーティングをグループ化して定義する機能を追加する.
Example ¶
package main import ( "fmt" "log" "net/http" "github.com/julienschmidt/httprouter" group "github.com/mythrnr/httprouter-group" ) func main() { // first, define routes, handlers, and middlewares. g := group.New("/").GET( func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { w.Write([]byte("GET /\n")) }, ).Middleware( func(h httprouter.Handle) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Write([]byte("Middleware 1: before\n")) h(w, r, p) w.Write([]byte("Middleware 1: after\n")) } }, ).Children( group.New("/users").GET( func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { w.Write([]byte("GET /users\n")) }, ).Middleware( func(h httprouter.Handle) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Write([]byte("Middleware 2: before\n")) h(w, r, p) w.Write([]byte("Middleware 2: after\n")) } }, ).Children( group.New("/:id").GET( func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { w.Write([]byte("GET /users/:id\n")) }, ), ), group.New("/users/:id").PUT( func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { w.Write([]byte("PUT /users/:id\n")) }, ).DELETE( func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) { w.Write([]byte("DELETE /users/:id\n")) }, ).Middleware( func(h httprouter.Handle) httprouter.Handle { return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Write([]byte("Middleware 3: before\n")) h(w, r, p) w.Write([]byte("Middleware 3: after\n")) } }, ), ) // next, set up and configure router. router := httprouter.New() router.PanicHandler = func(_ http.ResponseWriter, _ *http.Request, rec interface{}) { log.Fatal(rec) } // logging. // // GET / // GET /users // DELETE /users/:id // GET /users/:id // PUT /users/:id fmt.Println(g.Routes().String()) // finally, register routes to httprouter instance. // for _, r := range g.Routes() { // router.Handle(r.Method(), r.Path(), r.Handler()) // } // serve. // log.Fatal(http.ListenAndServe(":8080", router)) }
Output:
Index ¶
- type Middleware
- type Route
- type RouteGroup
- func (r *RouteGroup) Any(handler httprouter.Handle) *RouteGroup
- func (r *RouteGroup) Children(children ...*RouteGroup) *RouteGroup
- func (r *RouteGroup) DELETE(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) GET(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) HEAD(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) Handle(method string, handler httprouter.Handle) *RouteGroup
- func (r *RouteGroup) Match(handler httprouter.Handle, methods ...string) *RouteGroup
- func (r *RouteGroup) Middleware(middlewares ...Middleware) *RouteGroup
- func (r *RouteGroup) OPTIONS(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) PATCH(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) POST(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) PUT(handle httprouter.Handle) *RouteGroup
- func (r *RouteGroup) Routes() Routes
- type Routes
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
type Middleware func(httprouter.Handle) httprouter.Handle
Middleware is just type alias of middleware function.
Middleware は単にミドルウェア関数の型の別名.
type Route ¶ added in v0.3.0
type Route struct {
// contains filtered or unexported fields
}
Route is struct to retain routing information. Route does not keep the information of the parent hierarchy (path, middleware).
Route はルーティングの情報を保持する構造体. 親階層の情報(パス, ミドルウェア)は保持しない.
func (*Route) Handler ¶ added in v0.3.0
func (r *Route) Handler() httprouter.Handle
Handler just returns handler.
Handler は単にハンドラを返す.
type RouteGroup ¶
type RouteGroup struct {
// contains filtered or unexported fields
}
RouteGroup is struct to retain group information.
RouteGroup はグループ情報を保持する構造体.
func New ¶
func New(path string) *RouteGroup
New returns a new initialized RouteGroup. Define only base path first, other info define by method chaining.
New は初期化された RouteGroup を返す. 初めはパスのみ定義し, 他の情報はメソッドチェーンで定義していく.
func (*RouteGroup) Any ¶ added in v0.5.0
func (r *RouteGroup) Any(handler httprouter.Handle) *RouteGroup
Any relates its handler to all HTTP methods defined in `net/http` and returns itself. Handler will be ignored if HTTP method is already registered.
Any はハンドラを `net/http` で定義された HTTP メソッド全てに紐付けて自身を返す. 登録済みの HTTP メソッドが指定された場合は無視される.
func (*RouteGroup) Children ¶
func (r *RouteGroup) Children(children ...*RouteGroup) *RouteGroup
Children returns self includes specified groups.
Children は指定されたグループを含む自身を返す.
func (*RouteGroup) DELETE ¶
func (r *RouteGroup) DELETE(handle httprouter.Handle) *RouteGroup
DELETE is a shortcut for (*RouteGroup).Handle(http.MethodDelete, handle).
func (*RouteGroup) GET ¶
func (r *RouteGroup) GET(handle httprouter.Handle) *RouteGroup
GET is a shortcut for (*RouteGroup).Handle(http.MethodGet, handle).
func (*RouteGroup) HEAD ¶
func (r *RouteGroup) HEAD(handle httprouter.Handle) *RouteGroup
HEAD is a shortcut for (*RouteGroup).Handle(http.MethodHead, handle).
func (*RouteGroup) Handle ¶
func (r *RouteGroup) Handle(method string, handler httprouter.Handle) *RouteGroup
Handle returns self includes specified pair of HTTP method and handler. Handler will be overwritten if a registered HTTP method is specified.
Handle は指定された HTTP メソッドとハンドラのペアを含む自身を返す. 登録済みの HTTP メソッドが指定された場合, ハンドラは上書きされる.
func (*RouteGroup) Match ¶ added in v0.5.0
func (r *RouteGroup) Match( handler httprouter.Handle, methods ...string, ) *RouteGroup
Match relates the handler to all specified HTTP methods and returns itself. Handler will be ignored if HTTP method is already registered.
Match はハンドラを指定された HTTP メソッド全てに紐付けて自身を返す. 登録済みの HTTP メソッドが指定された場合は無視される.
func (*RouteGroup) Middleware ¶
func (r *RouteGroup) Middleware(middlewares ...Middleware) *RouteGroup
Middleware returns self includes specified middlewares.
Middleware は指定されたミドルウェアを含む自身を返す.
func (*RouteGroup) OPTIONS ¶
func (r *RouteGroup) OPTIONS(handle httprouter.Handle) *RouteGroup
OPTIONS is a shortcut for (*RouteGroup).Handle(http.MethodOptions, handle).
func (*RouteGroup) PATCH ¶
func (r *RouteGroup) PATCH(handle httprouter.Handle) *RouteGroup
PATCH is a shortcut for (*RouteGroup).Handle(http.MethodPatch, handle).
func (*RouteGroup) POST ¶
func (r *RouteGroup) POST(handle httprouter.Handle) *RouteGroup
POST is a shortcut for (*RouteGroup).Handle(http.MethodPost, handle).
func (*RouteGroup) PUT ¶
func (r *RouteGroup) PUT(handle httprouter.Handle) *RouteGroup
PUT is a shortcut for (*RouteGroup).Handle(http.MethodPut, handle).
func (*RouteGroup) Routes ¶ added in v0.3.0
func (r *RouteGroup) Routes() Routes
Routes returns a one-dimensional representation of the handlers registered in the parent and child hierarchies. The path and middleware are inherited, and the middleware is registered in the order of registration of the parent hierarchy, followed by the order of registration of the child hierarchies.
Routes は自身と子階層に登録済みのハンドラを一次元化して返す. パスとミドルウェアは引き継がれ, ミドルウェアは親階層の登録順, 続いて子階層の登録順で実行されるように登録される.
type Routes ¶ added in v0.3.0
type Routes []*Route
Routes is aggregation of `Route`.
Routes は `Route` の集合体.
func (Routes) String ¶ added in v0.3.0
String sorts the registered HTTP method and URI pairs by string and returns them as a newline-separated string. This includes the information of children.
String は登録済みの HTTP メソッドと URI のペアを 文字列でソートし, 改行区切りの文字列で返す. 自身を起点とした子階層の情報も取得して返す.
Example.
GET / GET /users DELETE /users/:id GET /users/:id PUT /users/:id