| Index: server/router/router.go
|
| diff --git a/server/router/router.go b/server/router/router.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7bad4ec2dc26db41c8cf5f2830dc0eec477fc5b6
|
| --- /dev/null
|
| +++ b/server/router/router.go
|
| @@ -0,0 +1,87 @@
|
| +// Copyright 2016 The LUCI Authors. All rights reserved.
|
| +// Use of this source code is governed under the Apache License, Version 2.0
|
| +// that can be found in the LICENSE file.
|
| +
|
| +// Package router provides a HTTP router with support for middleware and groups.
|
| +//
|
| +// Usage:
|
| +//
|
| +// r := router.New()
|
| +// r.UseHandlers(Logger())
|
| +// r.GET("/", rootHandler) // Type of rootHandler TBD
|
| +//
|
| +// authorized := r.Group("authorized")
|
| +// authorized.UseHandler(Authenticator()) // Authenticator has signature similar to Logger
|
| +// authorized.DELETE("/comment/:id", commentDeleteHandler) // Executes Logger and Authenticator middlewares in order before commentDeleteHandler
|
| +// // Full path for above: /authorized/comment/:id
|
| +//
|
| +// func Logger() middleware.ChainedHandler {
|
| +// return func(ctx context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params, next middleware.Handler) {
|
| +// log.Println("before")
|
| +// next(ctx, rw, r, p)
|
| +// log.Println("after")
|
| +// }
|
| +// }
|
| +//
|
| +package router
|
| +
|
| +import (
|
| + "github.com/julienschmidt/httprouter"
|
| + "github.com/luci/luci-go/server/middleware"
|
| +)
|
| +
|
| +// Router represents the main router type.
|
| +type Router struct {
|
| + Group
|
| +}
|
| +
|
| +// Group represents a router configuration. It has a base path
|
| +// and a list of middleware for the path.
|
| +type Group struct {
|
| + hrouter *httprouter.Router
|
| + handlers middleware.Chain
|
| + basePath string
|
| + router *Router
|
| + root bool
|
| +}
|
| +
|
| +// New creates a Router.
|
| +func New() *Router {
|
| + r := &Router{
|
| + Group: Group{
|
| + hrouter: httprouter.New(),
|
| + basePath: "/",
|
| + root: true,
|
| + },
|
| + }
|
| + r.Group.router = r
|
| + return r
|
| +}
|
| +
|
| +// UseHandlers adds handlers as middleware to the group. The added middleware applies to
|
| +// all routes in the current group and in groups derived from the current group.
|
| +func (g *Group) UseHandlers(handlers ...middleware.ChainedHandler) {
|
| + g.handlers.Add(middleware.NewChain(handlers...))
|
| +}
|
| +
|
| +// Use adds middleware chains to the group. The added middleware applies to
|
| +// all routes in the current group and in groups derived from the current group.
|
| +func (g *Group) Use(chains ...middleware.Chain) {
|
| + g.handlers.Add(chains...)
|
| +}
|
| +
|
| +// Group creates a new router group, extended by the provided relativePath.
|
| +// The new group has configuration such as handlers from the current group.
|
| +func (g *Group) Group(relativePath string) *Group {
|
| + return &Group{
|
| + hrouter: g.hrouter,
|
| + handlers: g.handlers,
|
| + basePath: g.basePath + relativePath,
|
| + router: g.router,
|
| + root: false,
|
| + }
|
| +}
|
| +
|
| +// TODO(nishanths): Write wrappers for GET, POST, Handle and other httrouter.Router's public
|
| +// methods. These will need to execute middleware before delegating to
|
| +// hrouter.GET(..), etc.
|
|
|