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

Unified Diff: appengine/cmd/dm/frontend/init.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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « appengine/cmd/dm/distributor/tq_handler.go ('k') | appengine/cmd/helloworld/frontend/handler.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/cmd/dm/frontend/init.go
diff --git a/appengine/cmd/dm/frontend/init.go b/appengine/cmd/dm/frontend/init.go
index 8075b6155a0c83783d038eaa4b491113012544fa..2ad63c77119dc72f12f82987fdeeab614127518c 100644
--- a/appengine/cmd/dm/frontend/init.go
+++ b/appengine/cmd/dm/frontend/init.go
@@ -6,92 +6,98 @@ package frontend
import (
"fmt"
"net/http"
"os"
"golang.org/x/net/context"
"google.golang.org/appengine"
- "github.com/julienschmidt/httprouter"
"github.com/luci/luci-go/appengine/cmd/dm/deps"
"github.com/luci/luci-go/appengine/cmd/dm/distributor"
"github.com/luci/luci-go/appengine/cmd/dm/mutate"
"github.com/luci/luci-go/appengine/gaeconfig"
"github.com/luci/luci-go/appengine/gaemiddleware"
"github.com/luci/luci-go/appengine/tumble"
"github.com/luci/luci-go/common/config"
"github.com/luci/luci-go/common/config/impl/filesystem"
"github.com/luci/luci-go/common/logging"
"github.com/luci/luci-go/server/discovery"
- "github.com/luci/luci-go/server/middleware"
"github.com/luci/luci-go/server/prpc"
+ "github.com/luci/luci-go/server/router"
)
func addConfigProd(c context.Context) context.Context {
cfg, err := gaeconfig.New(c)
switch err {
case nil:
c = config.Set(c, cfg)
case gaeconfig.ErrNotConfigured:
logging.Warningf(c, "luci-config service url not configured. Configure this at /admin/settings/gaeconfig.")
fallthrough
default:
panic(err)
}
return c
}
-func baseProd(h middleware.Handler) httprouter.Handle {
- newH := func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- h(addConfigProd(c), rw, r, p)
- }
- return gaemiddleware.BaseProd(newH)
+func baseProd() router.MiddlewareChain {
+ return append(
+ gaemiddleware.BaseProd(),
+ func(c *router.Context, next router.Handler) {
+ c.Context = addConfigProd(c.Context)
+ next(c)
+ },
+ )
}
func addConfigDev(c context.Context) context.Context {
fpath := os.Getenv("LUCI_DM_CONFIG_BASE_PATH")
if fpath == "" {
panic(fmt.Errorf("LUCI_DM_CONFIG_BASE_PATH must be set in the environment"))
}
fs, err := filesystem.New(fpath)
if err != nil {
panic(fmt.Errorf("while setting up LUCI_DM_CONFIG_BASE_PATH: %s", err))
}
return config.Set(c, fs)
}
-func baseDev(h middleware.Handler) httprouter.Handle {
- return gaemiddleware.BaseProd(func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- h(addConfigDev(c), rw, r, p)
- })
+func baseDev() router.MiddlewareChain {
+ return append(
+ gaemiddleware.BaseProd(),
+ func(c *router.Context, next router.Handler) {
+ c.Context = addConfigDev(c.Context)
+ next(c)
+ },
+ )
}
func init() {
- router := httprouter.New()
+ r := router.New()
tmb := tumble.Service{}
reg := distributor.NewRegistry(nil, mutate.FinishExecutionFn)
- base := baseProd
+ basemw := baseProd()
tmb.Middleware = func(c context.Context) context.Context {
return distributor.WithRegistry(addConfigProd(c), reg)
}
if appengine.IsDevAppServer() {
- base = baseDev
+ basemw = baseDev()
tmb.Middleware = func(c context.Context) context.Context {
return distributor.WithRegistry(addConfigDev(c), reg)
}
}
- distributor.InstallHandlers(reg, router, base)
+ distributor.InstallHandlers(reg, r, basemw)
svr := prpc.Server{}
deps.RegisterDepsServer(&svr, reg)
discovery.Enable(&svr)
- svr.InstallHandlers(router, base)
- tmb.InstallHandlers(router)
- gaemiddleware.InstallHandlers(router, base)
+ svr.InstallHandlers(r, basemw)
+ tmb.InstallHandlers(r)
+ gaemiddleware.InstallHandlers(r, basemw)
- http.Handle("/", router)
+ http.Handle("/", r)
}
« no previous file with comments | « appengine/cmd/dm/distributor/tq_handler.go ('k') | appengine/cmd/helloworld/frontend/handler.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698