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