| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 // Package router provides a HTTP router with support for middleware and groups. |
| 6 // |
| 7 // Usage: |
| 8 // |
| 9 // r := router.New() |
| 10 // r.UseHandlers(Logger()) |
| 11 // r.GET("/", rootHandler) // Type of rootHandler TBD |
| 12 // |
| 13 // authorized := r.Group("authorized") |
| 14 // authorized.UseHandler(Authenticator()) // Authenticator has signature
similar to Logger |
| 15 // authorized.DELETE("/comment/:id", commentDeleteHandler) // Executes L
ogger and Authenticator middlewares in order before commentDeleteHandler |
| 16 // // Full path for
above: /authorized/comment/:id |
| 17 // |
| 18 // func Logger() middleware.ChainedHandler { |
| 19 // return func(ctx context.Context, rw http.ResponseWriter, r *ht
tp.Request, p httprouter.Params, next middleware.Handler) { |
| 20 // log.Println("before") |
| 21 // next(ctx, rw, r, p) |
| 22 // log.Println("after") |
| 23 // } |
| 24 // } |
| 25 // |
| 26 package router |
| 27 |
| 28 import ( |
| 29 "github.com/julienschmidt/httprouter" |
| 30 "github.com/luci/luci-go/server/middleware" |
| 31 ) |
| 32 |
| 33 // Router represents the main router type. |
| 34 type Router struct { |
| 35 Group |
| 36 } |
| 37 |
| 38 // Group represents a router configuration. It has a base path |
| 39 // and a list of middleware for the path. |
| 40 type Group struct { |
| 41 hrouter *httprouter.Router |
| 42 handlers middleware.Chain |
| 43 basePath string |
| 44 router *Router |
| 45 root bool |
| 46 } |
| 47 |
| 48 // New creates a Router. |
| 49 func New() *Router { |
| 50 r := &Router{ |
| 51 Group: Group{ |
| 52 hrouter: httprouter.New(), |
| 53 basePath: "/", |
| 54 root: true, |
| 55 }, |
| 56 } |
| 57 r.Group.router = r |
| 58 return r |
| 59 } |
| 60 |
| 61 // UseHandlers adds handlers as middleware to the group. The added middleware ap
plies to |
| 62 // all routes in the current group and in groups derived from the current group. |
| 63 func (g *Group) UseHandlers(handlers ...middleware.ChainedHandler) { |
| 64 g.handlers.Add(middleware.NewChain(handlers...)) |
| 65 } |
| 66 |
| 67 // Use adds middleware chains to the group. The added middleware applies to |
| 68 // all routes in the current group and in groups derived from the current group. |
| 69 func (g *Group) Use(chains ...middleware.Chain) { |
| 70 g.handlers.Add(chains...) |
| 71 } |
| 72 |
| 73 // Group creates a new router group, extended by the provided relativePath. |
| 74 // The new group has configuration such as handlers from the current group. |
| 75 func (g *Group) Group(relativePath string) *Group { |
| 76 return &Group{ |
| 77 hrouter: g.hrouter, |
| 78 handlers: g.handlers, |
| 79 basePath: g.basePath + relativePath, |
| 80 router: g.router, |
| 81 root: false, |
| 82 } |
| 83 } |
| 84 |
| 85 // TODO(nishanths): Write wrappers for GET, POST, Handle and other httrouter.Rou
ter's public |
| 86 // methods. These will need to execute middleware before delegating to |
| 87 // hrouter.GET(..), etc. |
| OLD | NEW |