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

Side by Side Diff: appengine/cmd/helloworld_mvm/frontend/main.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: gaemiddleware: add middleware func for WithProd 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
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 // Package main implements HTTP server that handles requests to default 5 // Package main implements HTTP server that handles requests to default
6 // module. 6 // module.
7 package main 7 package main
8 8
9 import ( 9 import (
10 "net/http" 10 "net/http"
11 "strings" 11 "strings"
12 12
13 "github.com/julienschmidt/httprouter"
14 "golang.org/x/net/context" 13 "golang.org/x/net/context"
15 "google.golang.org/appengine" 14 "google.golang.org/appengine"
16 15
17 "github.com/luci/gae/service/info" 16 "github.com/luci/gae/service/info"
18 "github.com/luci/luci-go/appengine/gaeauth/server" 17 "github.com/luci/luci-go/appengine/gaeauth/server"
19 "github.com/luci/luci-go/appengine/gaemiddleware" 18 "github.com/luci/luci-go/appengine/gaemiddleware"
20 "github.com/luci/luci-go/server/auth" 19 "github.com/luci/luci-go/server/auth"
21 » "github.com/luci/luci-go/server/middleware" 20 » "github.com/luci/luci-go/server/router"
22 "github.com/luci/luci-go/server/templates" 21 "github.com/luci/luci-go/server/templates"
23 ) 22 )
24 23
25 // templateBundle is used to render HTML templates. It provides a base args 24 // templateBundle is used to render HTML templates. It provides a base args
26 // passed to all templates. 25 // passed to all templates.
27 var templateBundle = &templates.Bundle{ 26 var templateBundle = &templates.Bundle{
28 Loader: templates.FileSystemLoader("templates"), 27 Loader: templates.FileSystemLoader("templates"),
29 DebugMode: appengine.IsDevAppServer(), 28 DebugMode: appengine.IsDevAppServer(),
30 DefaultArgs: func(c context.Context) (templates.Args, error) { 29 DefaultArgs: func(c context.Context) (templates.Args, error) {
31 loginURL, err := auth.LoginURL(c, "/") 30 loginURL, err := auth.LoginURL(c, "/")
(...skipping 12 matching lines...) Expand all
44 "AppVersion": strings.Split(info.Get(c).VersionID(), ". ")[0], 43 "AppVersion": strings.Split(info.Get(c).VersionID(), ". ")[0],
45 "IsAnonymous": auth.CurrentIdentity(c) == "anonymous:ano nymous", 44 "IsAnonymous": auth.CurrentIdentity(c) == "anonymous:ano nymous",
46 "IsAdmin": isAdmin, 45 "IsAdmin": isAdmin,
47 "User": auth.CurrentUser(c), 46 "User": auth.CurrentUser(c),
48 "LoginURL": loginURL, 47 "LoginURL": loginURL,
49 "LogoutURL": logoutURL, 48 "LogoutURL": logoutURL,
50 }, nil 49 }, nil
51 }, 50 },
52 } 51 }
53 52
54 // base is the root of the middleware chain. 53 // base returns the root middleware chain.
55 func base(h middleware.Handler) httprouter.Handle { 54 func base() router.MiddlewareChain {
56 methods := auth.Authenticator{ 55 methods := auth.Authenticator{
57 &server.OAuth2Method{Scopes: []string{server.EmailScope}}, 56 &server.OAuth2Method{Scopes: []string{server.EmailScope}},
58 server.CookieAuth, 57 server.CookieAuth,
59 &server.InboundAppIDAuthMethod{}, 58 &server.InboundAppIDAuthMethod{},
60 } 59 }
61 » h = auth.Use(h, methods) 60 » return append(
62 » h = templates.WithTemplates(h, templateBundle) 61 » » gaemiddleware.BaseProd(),
63 » return gaemiddleware.BaseProd(h) 62 » » templates.WithTemplates(templateBundle),
63 » » auth.Use(methods),
64 » )
64 } 65 }
65 66
66 //// Routes. 67 //// Routes.
67 68
68 func main() { 69 func main() {
69 » router := httprouter.New() 70 » r := router.New()
70 » gaemiddleware.InstallHandlers(router, base) 71 » basemw := base()
71 » router.GET("/", base(auth.Authenticate(indexPage))) 72 » gaemiddleware.InstallHandlers(r, basemw)
72 » http.DefaultServeMux.Handle("/", router) 73 » r.GET("/", append(basemw, auth.Authenticate), indexPage)
74 » http.DefaultServeMux.Handle("/", r)
73 75
74 appengine.Main() 76 appengine.Main()
75 } 77 }
76 78
77 //// Handlers. 79 //// Handlers.
78 80
79 func indexPage(c context.Context, w http.ResponseWriter, r *http.Request, p http router.Params) { 81 func indexPage(c *router.Context) {
80 » templates.MustRender(c, w, "pages/index.html", nil) 82 » templates.MustRender(c.Context, c.Writer, "pages/index.html", nil)
81 } 83 }
OLDNEW
« no previous file with comments | « appengine/cmd/helloworld_mvm/backend/main.go ('k') | appengine/cmd/logdog_coordinator/backend/main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698