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

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: 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/frontend/init.go ('k') | appengine/cmd/helloworld_mvm/backend/main.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..52bf8bab17ce19de1a7ccd810231e3341e511b9c 100644
--- a/appengine/cmd/helloworld/frontend/handler.go
+++ b/appengine/cmd/helloworld/frontend/handler.go
@@ -4,32 +4,31 @@
// Package frontend implements HTTP server that handles requests to default
// module.
package frontend
import (
"net/http"
"strings"
"github.com/golang/protobuf/proto"
- "github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
"google.golang.org/appengine"
"github.com/luci/gae/service/info"
"github.com/luci/luci-go/appengine/cmd/helloworld/proto"
"github.com/luci/luci-go/appengine/gaeauth/server"
"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"
)
// templateBundle is used to render HTML templates. It provides a base args
// passed to all templates.
var templateBundle = &templates.Bundle{
Loader: templates.FileSystemLoader("templates"),
DebugMode: appengine.IsDevAppServer(),
DefaultArgs: func(c context.Context) (templates.Args, error) {
loginURL, err := auth.LoginURL(c, "/")
@@ -48,36 +47,38 @@ var templateBundle = &templates.Bundle{
"AppVersion": strings.Split(info.Get(c).VersionID(), ".")[0],
"IsAnonymous": auth.CurrentIdentity(c) == "anonymous:anonymous",
"IsAdmin": isAdmin,
"User": auth.CurrentUser(c),
"LoginURL": loginURL,
"LogoutURL": logoutURL,
}, nil
},
}
-// 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)
+// pageBase returns the middleware chain for page handlers.
+func pageBase() router.MiddlewareChain {
+ 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 {
+// prpcBase returns the middleware chain for pRPC API handlers.
+func prpcBase() router.MiddlewareChain {
// 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.
func checkAPIAccess(c context.Context, methodName string, req proto.Message) (context.Context, error) {
// Implement authorization check here, for example:
//
// import "github.com/golang/protobuf/proto"
// import "google.golang.org/grpc/codes"
// import "github.com/luci/luci-go/common/grpcutil"
@@ -87,30 +88,30 @@ func checkAPIAccess(c context.Context, methodName string, req proto.Message) (co
// return nil, grpcutil.Errf(codes.Internal, "%s", err)
// }
// if !hasAccess {
// return nil, grpcutil.Errf(codes.PermissionDenied, "%s is not allowed to call APIs", auth.CurrentIdentity(c))
// }
return c, nil
}
func init() {
- router := httprouter.New()
- gaemiddleware.InstallHandlers(router, pageBase)
- router.GET("/", pageBase(auth.Authenticate(indexPage)))
+ r := router.New()
+ gaemiddleware.InstallHandlers(r, pageBase())
+ r.GET("/", append(pageBase(), auth.Authenticate), indexPage)
var api prpc.Server
helloworld.RegisterGreeterServer(&api, &helloworld.DecoratedGreeter{
Service: &greeterService{},
Prelude: checkAPIAccess,
})
discovery.Enable(&api)
- api.InstallHandlers(router, prpcBase)
+ api.InstallHandlers(r, prpcBase())
- http.DefaultServeMux.Handle("/", router)
+ http.DefaultServeMux.Handle("/", r)
}
//// 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)
}
« no previous file with comments | « appengine/cmd/dm/frontend/init.go ('k') | appengine/cmd/helloworld_mvm/backend/main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698