| OLD | NEW |
| 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 returns the root middleware chain. |
| 35 func base(installConfig bool) middleware.Base { | 34 func base(installConfig bool) router.MiddlewareChain { |
| 36 » return func(h middleware.Handler) httprouter.Handle { | 35 » m := append(gaemiddleware.BaseProd(), coordinator.WithProdServices) |
| 37 » » if installConfig { | 36 » if installConfig { |
| 38 » » » h = config.WithConfig(h) | 37 » » m = append(m, config.WithConfig) |
| 39 » » } | |
| 40 » » h = coordinator.WithProdServices(h) | |
| 41 » » return gaemiddleware.BaseProd(h) | |
| 42 } | 38 } |
| 39 return m |
| 43 } | 40 } |
| 44 | 41 |
| 45 // Run installs and executes this site. | 42 // Run installs and executes this site. |
| 46 func main() { | 43 func main() { |
| 47 » router := httprouter.New() | 44 » r := router.New() |
| 48 | 45 |
| 49 // Setup Cloud Endpoints. | 46 // Setup Cloud Endpoints. |
| 50 svr := prpc.Server{ | 47 svr := prpc.Server{ |
| 51 AccessControl: accessControl, | 48 AccessControl: accessControl, |
| 52 } | 49 } |
| 53 adminPb.RegisterAdminServer(&svr, admin.New()) | 50 adminPb.RegisterAdminServer(&svr, admin.New()) |
| 54 servicesPb.RegisterServicesServer(&svr, services.New()) | 51 servicesPb.RegisterServicesServer(&svr, services.New()) |
| 55 registrationPb.RegisterRegistrationServer(&svr, registration.New()) | 52 registrationPb.RegisterRegistrationServer(&svr, registration.New()) |
| 56 logsPb.RegisterLogsServer(&svr, logs.New()) | 53 logsPb.RegisterLogsServer(&svr, logs.New()) |
| 57 discovery.Enable(&svr) | 54 discovery.Enable(&svr) |
| 58 | 55 |
| 59 // Standard HTTP endpoints. | 56 // Standard HTTP endpoints. |
| 60 » gaemiddleware.InstallHandlers(router, base(false)) | 57 » gaemiddleware.InstallHandlers(r, base(false)) |
| 61 » svr.InstallHandlers(router, base(true)) | 58 » svr.InstallHandlers(r, base(true)) |
| 62 | 59 |
| 63 // Redirect "/" to "/app/". | 60 // Redirect "/" to "/app/". |
| 64 » router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprout
er.Params) { | 61 » r.GET("/", nil, func(c *router.Context) { |
| 65 » » http.Redirect(w, r, "/app/", http.StatusFound) | 62 » » http.Redirect(c.Writer, c.Request, "/app/", http.StatusFound) |
| 66 }) | 63 }) |
| 67 | 64 |
| 68 » http.Handle("/", router) | 65 » http.Handle("/", r) |
| 69 appengine.Main() | 66 appengine.Main() |
| 70 } | 67 } |
| 71 | 68 |
| 72 func accessControl(c context.Context, origin string) bool { | 69 func accessControl(c context.Context, origin string) bool { |
| 73 cfg, err := config.Load(c) | 70 cfg, err := config.Load(c) |
| 74 if err != nil { | 71 if err != nil { |
| 75 log.WithError(err).Errorf(c, "Failed to get config for access co
ntrol check.") | 72 log.WithError(err).Errorf(c, "Failed to get config for access co
ntrol check.") |
| 76 return false | 73 return false |
| 77 } | 74 } |
| 78 | 75 |
| 79 ccfg := cfg.GetCoordinator() | 76 ccfg := cfg.GetCoordinator() |
| 80 if ccfg == nil { | 77 if ccfg == nil { |
| 81 return false | 78 return false |
| 82 } | 79 } |
| 83 | 80 |
| 84 for _, o := range ccfg.RpcAllowOrigins { | 81 for _, o := range ccfg.RpcAllowOrigins { |
| 85 if o == origin { | 82 if o == origin { |
| 86 return true | 83 return true |
| 87 } | 84 } |
| 88 } | 85 } |
| 89 return false | 86 return false |
| 90 } | 87 } |
| OLD | NEW |