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

Unified Diff: appengine/cmd/helloworld/frontend/handler.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 side-by-side diff with in-line comments
Download patch
Index: appengine/cmd/helloworld/frontend/handler.go
diff --git a/appengine/cmd/helloworld/frontend/handler.go b/appengine/cmd/helloworld/frontend/handler.go
index 72c18679594e6871be3af931f1b8483c70c8d0b3..b696b2fdda13e4e2b9bd80569e5dc4a3b5becf87 100644
--- a/appengine/cmd/helloworld/frontend/handler.go
+++ b/appengine/cmd/helloworld/frontend/handler.go
@@ -11,7 +11,6 @@ import (
"strings"
"github.com/golang/protobuf/proto"
- "github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
"google.golang.org/appengine"
@@ -21,8 +20,8 @@ import (
"github.com/luci/luci-go/appengine/gaemiddleware"
"github.com/luci/luci-go/server/auth"
"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"
"github.com/luci/luci-go/server/templates"
)
@@ -56,21 +55,23 @@ var templateBundle = &templates.Bundle{
}
// pageBase is middleware for page handlers.
-func pageBase(h middleware.Handler) httprouter.Handle {
- h = auth.Use(h, auth.Authenticator{server.CookieAuth})
- h = templates.WithTemplates(h, templateBundle)
- return gaemiddleware.BaseProd(h)
+func pageBase() []router.Handler {
+ return append(
+ gaemiddleware.BaseProd(),
+ templates.WithTemplates(templateBundle),
+ auth.Use(auth.Authenticator{server.CookieAuth}),
+ )
}
// prpcBase is middleware for pRPC API handlers.
-func prpcBase(h middleware.Handler) httprouter.Handle {
+func prpcBase() []router.Handler {
// OAuth 2.0 with email scope is registered as a default authenticator
// by importing "github.com/luci/luci-go/appengine/gaeauth/server".
// No need to setup an authenticator here.
//
// For authorization checks, we use per-service decorators; see
// service registration code.
- return gaemiddleware.BaseProd(h)
+ return gaemiddleware.BaseProd()
}
//// Routes.
@@ -94,9 +95,9 @@ func checkAPIAccess(c context.Context, methodName string, req proto.Message) (co
}
func init() {
- router := httprouter.New()
- gaemiddleware.InstallHandlers(router, pageBase)
- router.GET("/", pageBase(auth.Authenticate(indexPage)))
+ router := router.New()
+ gaemiddleware.InstallHandlers(router, pageBase())
+ router.GET("/", append(pageBase(), auth.Authenticate(), indexPage)...)
var api prpc.Server
helloworld.RegisterGreeterServer(&api, &helloworld.DecoratedGreeter{
@@ -104,13 +105,13 @@ func init() {
Prelude: checkAPIAccess,
})
discovery.Enable(&api)
- api.InstallHandlers(router, prpcBase)
+ api.InstallHandlers(router, prpcBase())
http.DefaultServeMux.Handle("/", router)
}
//// Handlers.
-func indexPage(c context.Context, w http.ResponseWriter, r *http.Request, p httprouter.Params) {
- templates.MustRender(c, w, "pages/index.html", nil)
+func indexPage(c *router.Context) {
+ templates.MustRender(c.Context, c.Writer, "pages/index.html", nil)
}

Powered by Google App Engine
This is Rietveld 408576698