Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: server/router/example_test.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: router: Update documentation and improve code style Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | server/router/handler.go » ('j') | server/router/handler.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_test
6
7 import (
8 "fmt"
9 "log"
10 "math/rand"
11 "net/http"
12 "time"
13
14 "golang.org/x/net/context"
15
16 "github.com/luci/luci-go/server/router"
17 )
18
19 var durations []time.Duration
20
21 func Logger(c *router.Context, next router.Handler) {
22 log.Println(c.Request.URL.String())
23 next(c)
24 }
25
26 func Monitor(c *router.Context, next router.Handler) {
27 start := time.Now()
28 next(c)
29 durations = append(durations, time.Since(start))
30 }
31
32 func AuthCheck(c *router.Context, next router.Handler) {
33 var authenticated bool
34 if !authenticated {
35 c.WriteHeader(http.StatusUnauthorized)
36 return
37 }
38 next(c)
39 }
40
41 func GenerateRandom(c *router.Context, next router.Handler) {
42 secret := rand.Int()
43 c.Context = context.WithValue(c.Context, "secret", secret)
44 next(c)
45 }
46
47 func Root(c *router.Context) {
48 c.WriteHeader(http.StatusOK)
49 }
50
51 func Hello(c *router.Context) {
52 name := c.Params[0].Value
53 fmt.Fprintf(c, "Hello, %s", name)
54 }
55
56 func Random(c *router.Context) {
57 s := c.Value("secret").(int)
58 fmt.Fprintf(c, "secret: %d", s)
59 }
60
61 // Example_createServer demonstrates creating an HTTP server using the imported
62 // router package.
63 func Example_createServer() {
64 r := router.New()
65 r.Use(router.MiddlewareChain{Logger, Monitor})
66 r.GET("/", nil, Root)
67 r.GET("/hello/:name", nil, Hello)
68
69 auth := r.Subrouter("authenticated")
70 auth.Use(router.MiddlewareChain{AuthCheck})
71 auth.GET("/random", router.MiddlewareChain{GenerateRandom}, Random)
72
73 http.Handle("/", r)
74 log.Fatal(http.ListenAndServe(":8080", nil))
iannucci 2016/06/16 00:54:24 I think you need // Output: // something //
nishanths 2016/06/16 03:43:27 Cool, done. :) If you have time, please check to
75 }
OLDNEW
« no previous file with comments | « no previous file | server/router/handler.go » ('j') | server/router/handler.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698