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

Side by Side Diff: appengine/cmd/logdog_coordinator/vmuser/main.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests 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 5 package main
6 6
7 import ( 7 import (
8 "net/http" 8 "net/http"
9 9
10 "github.com/julienschmidt/httprouter"
11 "github.com/luci/luci-go/appengine/gaemiddleware" 10 "github.com/luci/luci-go/appengine/gaemiddleware"
12 "github.com/luci/luci-go/appengine/logdog/coordinator" 11 "github.com/luci/luci-go/appengine/logdog/coordinator"
13 "github.com/luci/luci-go/appengine/logdog/coordinator/config" 12 "github.com/luci/luci-go/appengine/logdog/coordinator/config"
14 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/admin" 13 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/admin"
15 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/logs" 14 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/logs"
16 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/registra tion" 15 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/registra tion"
17 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/services " 16 "github.com/luci/luci-go/appengine/logdog/coordinator/endpoints/services "
18 adminPb "github.com/luci/luci-go/common/api/logdog_coordinator/admin/v1" 17 adminPb "github.com/luci/luci-go/common/api/logdog_coordinator/admin/v1"
19 logsPb "github.com/luci/luci-go/common/api/logdog_coordinator/logs/v1" 18 logsPb "github.com/luci/luci-go/common/api/logdog_coordinator/logs/v1"
20 registrationPb "github.com/luci/luci-go/common/api/logdog_coordinator/re gistration/v1" 19 registrationPb "github.com/luci/luci-go/common/api/logdog_coordinator/re gistration/v1"
21 servicesPb "github.com/luci/luci-go/common/api/logdog_coordinator/servic es/v1" 20 servicesPb "github.com/luci/luci-go/common/api/logdog_coordinator/servic es/v1"
22 log "github.com/luci/luci-go/common/logging" 21 log "github.com/luci/luci-go/common/logging"
23 "github.com/luci/luci-go/server/discovery" 22 "github.com/luci/luci-go/server/discovery"
24 "github.com/luci/luci-go/server/middleware"
25 "github.com/luci/luci-go/server/prpc" 23 "github.com/luci/luci-go/server/prpc"
24 "github.com/luci/luci-go/server/router"
26 "golang.org/x/net/context" 25 "golang.org/x/net/context"
27 "google.golang.org/appengine" 26 "google.golang.org/appengine"
28 27
29 // Include mutations package so its Mutations will register with tumble via 28 // Include mutations package so its Mutations will register with tumble via
30 // init(). 29 // init().
31 _ "github.com/luci/luci-go/appengine/logdog/coordinator/mutations" 30 _ "github.com/luci/luci-go/appengine/logdog/coordinator/mutations"
32 ) 31 )
33 32
34 // base is the root of the middleware chain. 33 // base is the root of the middleware chain.
35 func base(installConfig bool) middleware.Base { 34 func base(installConfig bool) []router.Handler {
36 » return func(h middleware.Handler) httprouter.Handle { 35 » h := append(
37 » » if installConfig { 36 » » gaemiddleware.BaseProd(),
38 » » » h = config.WithConfig(h) 37 » » coordinator.WithProdServices(),
39 » » } 38 » )
40 » » h = coordinator.WithProdServices(h) 39 » if installConfig {
41 » » return gaemiddleware.BaseProd(h) 40 » » h = append(h, config.WithConfig())
42 } 41 }
42 return h
43 } 43 }
44 44
45 // Run installs and executes this site. 45 // Run installs and executes this site.
46 func main() { 46 func main() {
47 » router := httprouter.New() 47 » r := router.New()
48 48
49 // Setup Cloud Endpoints. 49 // Setup Cloud Endpoints.
50 svr := prpc.Server{ 50 svr := prpc.Server{
51 AccessControl: accessControl, 51 AccessControl: accessControl,
52 } 52 }
53 adminPb.RegisterAdminServer(&svr, admin.New()) 53 adminPb.RegisterAdminServer(&svr, admin.New())
54 servicesPb.RegisterServicesServer(&svr, services.New()) 54 servicesPb.RegisterServicesServer(&svr, services.New())
55 registrationPb.RegisterRegistrationServer(&svr, registration.New()) 55 registrationPb.RegisterRegistrationServer(&svr, registration.New())
56 logsPb.RegisterLogsServer(&svr, logs.New()) 56 logsPb.RegisterLogsServer(&svr, logs.New())
57 discovery.Enable(&svr) 57 discovery.Enable(&svr)
58 58
59 // Standard HTTP endpoints. 59 // Standard HTTP endpoints.
60 » gaemiddleware.InstallHandlers(router, base(false)) 60 » gaemiddleware.InstallHandlers(r, base(false))
61 » svr.InstallHandlers(router, base(true)) 61 » svr.InstallHandlers(r, base(true))
62 62
63 // Redirect "/" to "/app/". 63 // Redirect "/" to "/app/".
64 » router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprout er.Params) { 64 » r.GET("/", func(c *router.Context) {
65 » » http.Redirect(w, r, "/app/", http.StatusFound) 65 » » http.Redirect(c.Writer, c.Request, "/app/", http.StatusFound)
66 }) 66 })
67 67
68 » http.Handle("/", router) 68 » http.Handle("/", r)
69 appengine.Main() 69 appengine.Main()
70 } 70 }
71 71
72 func accessControl(c context.Context, origin string) bool { 72 func accessControl(c context.Context, origin string) bool {
73 cfg, err := config.Load(c) 73 cfg, err := config.Load(c)
74 if err != nil { 74 if err != nil {
75 log.WithError(err).Errorf(c, "Failed to get config for access co ntrol check.") 75 log.WithError(err).Errorf(c, "Failed to get config for access co ntrol check.")
76 return false 76 return false
77 } 77 }
78 78
79 ccfg := cfg.GetCoordinator() 79 ccfg := cfg.GetCoordinator()
80 if ccfg == nil { 80 if ccfg == nil {
81 return false 81 return false
82 } 82 }
83 83
84 for _, o := range ccfg.RpcAllowOrigins { 84 for _, o := range ccfg.RpcAllowOrigins {
85 if o == origin { 85 if o == origin {
86 return true 86 return true
87 } 87 }
88 } 88 }
89 return false 89 return false
90 } 90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698