| 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 frontend | 5 package frontend |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 "os" | 10 "os" |
| 11 | 11 |
| 12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 13 | 13 |
| 14 "google.golang.org/appengine" | 14 "google.golang.org/appengine" |
| 15 | 15 |
| 16 "github.com/julienschmidt/httprouter" | |
| 17 "github.com/luci/luci-go/appengine/cmd/dm/deps" | 16 "github.com/luci/luci-go/appengine/cmd/dm/deps" |
| 18 "github.com/luci/luci-go/appengine/cmd/dm/distributor" | 17 "github.com/luci/luci-go/appengine/cmd/dm/distributor" |
| 19 "github.com/luci/luci-go/appengine/cmd/dm/mutate" | 18 "github.com/luci/luci-go/appengine/cmd/dm/mutate" |
| 20 "github.com/luci/luci-go/appengine/gaeconfig" | 19 "github.com/luci/luci-go/appengine/gaeconfig" |
| 21 "github.com/luci/luci-go/appengine/gaemiddleware" | 20 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 22 "github.com/luci/luci-go/appengine/tumble" | 21 "github.com/luci/luci-go/appengine/tumble" |
| 23 "github.com/luci/luci-go/common/config" | 22 "github.com/luci/luci-go/common/config" |
| 24 "github.com/luci/luci-go/common/config/impl/filesystem" | 23 "github.com/luci/luci-go/common/config/impl/filesystem" |
| 25 "github.com/luci/luci-go/common/logging" | 24 "github.com/luci/luci-go/common/logging" |
| 26 "github.com/luci/luci-go/server/discovery" | 25 "github.com/luci/luci-go/server/discovery" |
| 27 "github.com/luci/luci-go/server/middleware" | |
| 28 "github.com/luci/luci-go/server/prpc" | 26 "github.com/luci/luci-go/server/prpc" |
| 27 "github.com/luci/luci-go/server/router" |
| 29 ) | 28 ) |
| 30 | 29 |
| 31 func addConfigProd(c context.Context) context.Context { | 30 func addConfigProd(c context.Context) context.Context { |
| 32 cfg, err := gaeconfig.New(c) | 31 cfg, err := gaeconfig.New(c) |
| 33 switch err { | 32 switch err { |
| 34 case nil: | 33 case nil: |
| 35 c = config.Set(c, cfg) | 34 c = config.Set(c, cfg) |
| 36 case gaeconfig.ErrNotConfigured: | 35 case gaeconfig.ErrNotConfigured: |
| 37 logging.Warningf(c, "luci-config service url not configured. Con
figure this at /admin/settings/gaeconfig.") | 36 logging.Warningf(c, "luci-config service url not configured. Con
figure this at /admin/settings/gaeconfig.") |
| 38 fallthrough | 37 fallthrough |
| 39 default: | 38 default: |
| 40 panic(err) | 39 panic(err) |
| 41 } | 40 } |
| 42 return c | 41 return c |
| 43 } | 42 } |
| 44 | 43 |
| 45 func baseProd(h middleware.Handler) httprouter.Handle { | 44 func baseProd() router.MiddlewareChain { |
| 46 » newH := func(c context.Context, rw http.ResponseWriter, r *http.Request,
p httprouter.Params) { | 45 » return append( |
| 47 » » h(addConfigProd(c), rw, r, p) | 46 » » gaemiddleware.BaseProd(), |
| 48 » } | 47 » » func(c *router.Context, next router.Handler) { |
| 49 » return gaemiddleware.BaseProd(newH) | 48 » » » c.Context = addConfigProd(c.Context) |
| 49 » » » next(c) |
| 50 » » }, |
| 51 » ) |
| 50 } | 52 } |
| 51 | 53 |
| 52 func addConfigDev(c context.Context) context.Context { | 54 func addConfigDev(c context.Context) context.Context { |
| 53 fpath := os.Getenv("LUCI_DM_CONFIG_BASE_PATH") | 55 fpath := os.Getenv("LUCI_DM_CONFIG_BASE_PATH") |
| 54 if fpath == "" { | 56 if fpath == "" { |
| 55 panic(fmt.Errorf("LUCI_DM_CONFIG_BASE_PATH must be set in the en
vironment")) | 57 panic(fmt.Errorf("LUCI_DM_CONFIG_BASE_PATH must be set in the en
vironment")) |
| 56 } | 58 } |
| 57 fs, err := filesystem.New(fpath) | 59 fs, err := filesystem.New(fpath) |
| 58 if err != nil { | 60 if err != nil { |
| 59 panic(fmt.Errorf("while setting up LUCI_DM_CONFIG_BASE_PATH: %s"
, err)) | 61 panic(fmt.Errorf("while setting up LUCI_DM_CONFIG_BASE_PATH: %s"
, err)) |
| 60 } | 62 } |
| 61 return config.Set(c, fs) | 63 return config.Set(c, fs) |
| 62 } | 64 } |
| 63 | 65 |
| 64 func baseDev(h middleware.Handler) httprouter.Handle { | 66 func baseDev() router.MiddlewareChain { |
| 65 » return gaemiddleware.BaseProd(func(c context.Context, rw http.ResponseWr
iter, r *http.Request, p httprouter.Params) { | 67 » return append( |
| 66 » » h(addConfigDev(c), rw, r, p) | 68 » » gaemiddleware.BaseProd(), |
| 67 » }) | 69 » » func(c *router.Context, next router.Handler) { |
| 70 » » » c.Context = addConfigDev(c.Context) |
| 71 » » » next(c) |
| 72 » » }, |
| 73 » ) |
| 68 } | 74 } |
| 69 | 75 |
| 70 func init() { | 76 func init() { |
| 71 » router := httprouter.New() | 77 » r := router.New() |
| 72 tmb := tumble.Service{} | 78 tmb := tumble.Service{} |
| 73 | 79 |
| 74 reg := distributor.NewRegistry(nil, mutate.FinishExecutionFn) | 80 reg := distributor.NewRegistry(nil, mutate.FinishExecutionFn) |
| 75 | 81 |
| 76 » base := baseProd | 82 » basemw := baseProd() |
| 77 tmb.Middleware = func(c context.Context) context.Context { | 83 tmb.Middleware = func(c context.Context) context.Context { |
| 78 return distributor.WithRegistry(addConfigProd(c), reg) | 84 return distributor.WithRegistry(addConfigProd(c), reg) |
| 79 } | 85 } |
| 80 if appengine.IsDevAppServer() { | 86 if appengine.IsDevAppServer() { |
| 81 » » base = baseDev | 87 » » basemw = baseDev() |
| 82 tmb.Middleware = func(c context.Context) context.Context { | 88 tmb.Middleware = func(c context.Context) context.Context { |
| 83 return distributor.WithRegistry(addConfigDev(c), reg) | 89 return distributor.WithRegistry(addConfigDev(c), reg) |
| 84 } | 90 } |
| 85 } | 91 } |
| 86 | 92 |
| 87 » distributor.InstallHandlers(reg, router, base) | 93 » distributor.InstallHandlers(reg, r, basemw) |
| 88 | 94 |
| 89 svr := prpc.Server{} | 95 svr := prpc.Server{} |
| 90 deps.RegisterDepsServer(&svr, reg) | 96 deps.RegisterDepsServer(&svr, reg) |
| 91 discovery.Enable(&svr) | 97 discovery.Enable(&svr) |
| 92 » svr.InstallHandlers(router, base) | 98 » svr.InstallHandlers(r, basemw) |
| 93 » tmb.InstallHandlers(router) | 99 » tmb.InstallHandlers(r) |
| 94 » gaemiddleware.InstallHandlers(router, base) | 100 » gaemiddleware.InstallHandlers(r, basemw) |
| 95 | 101 |
| 96 » http.Handle("/", router) | 102 » http.Handle("/", r) |
| 97 } | 103 } |
| OLD | NEW |