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

Unified Diff: appengine/gaemiddleware/appengine.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
Index: appengine/gaemiddleware/appengine.go
diff --git a/appengine/gaemiddleware/appengine.go b/appengine/gaemiddleware/appengine.go
index d39b00091d31359552d1e600679d992135053907..3e77453135b7df241b0d268dad53afec0d3ed8d7 100644
--- a/appengine/gaemiddleware/appengine.go
+++ b/appengine/gaemiddleware/appengine.go
@@ -1,63 +1,60 @@
// Copyright 2015 The LUCI Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package gaemiddleware
import (
"fmt"
"net/http"
- "github.com/julienschmidt/httprouter"
"github.com/luci/gae/service/info"
"github.com/luci/luci-go/common/logging"
- "github.com/luci/luci-go/server/middleware"
+ "github.com/luci/luci-go/server/router"
"golang.org/x/net/context"
)
var devAppserverBypassFn = func(c context.Context) bool {
return info.Get(c).IsDevAppServer()
}
-// RequireCron ensures that this handler was run from the appengine 'cron'
+// RequireCron ensures that the request is from the appengine 'cron'
// service. Otherwise it aborts the request with a StatusForbidden.
//
// This middleware has no effect when using 'BaseTest' or when running under
// dev_appserver.py
-func RequireCron(h middleware.Handler) middleware.Handler {
- return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- if !devAppserverBypassFn(c) {
- if r.Header.Get("X-Appengine-Cron") != "true" {
- rw.WriteHeader(http.StatusForbidden)
- logging.Errorf(c, "request not made from cron")
- fmt.Fprint(rw, "error: must be run from cron")
- return
- }
+func RequireCron(c *router.Context, next router.Handler) {
+ if !devAppserverBypassFn(c.Context) {
+ if c.Request.Header.Get("X-Appengine-Cron") != "true" {
+ c.Writer.WriteHeader(http.StatusForbidden)
+ logging.Errorf(c.Context, "request not made from cron")
+ fmt.Fprint(c.Writer, "error: must be run from cron")
+ return
}
- h(c, rw, r, p)
}
+ next(c)
}
-// RequireTaskQueue ensures that this handler was run from the specified
+// RequireTaskQueue ensures that the request is from the specified
// appengine 'taskqueue' queue. Otherwise it aborts the request with
// a StatusForbidden.
//
// if `queue` is the empty string, than this simply checks that this handler was
// run from ANY appengine taskqueue.
//
// This middleware has no effect when using 'BaseTest' or when running under
// dev_appserver.py
-func RequireTaskQueue(queue string, h middleware.Handler) middleware.Handler {
- return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- if !devAppserverBypassFn(c) {
- qName := r.Header.Get("X-AppEngine-QueueName")
+func RequireTaskQueue(queue string) router.Middleware {
+ return func(c *router.Context, next router.Handler) {
+ if !devAppserverBypassFn(c.Context) {
+ qName := c.Request.Header.Get("X-AppEngine-QueueName")
if qName == "" || (queue != "" && queue != qName) {
- rw.WriteHeader(http.StatusForbidden)
- logging.Errorf(c, "request made from wrong taskqueue: %q v %q", qName, queue)
- fmt.Fprintf(rw, "error: must be run from the correct taskqueue")
+ c.Writer.WriteHeader(http.StatusForbidden)
+ logging.Errorf(c.Context, "request made from wrong taskqueue: %q v %q", qName, queue)
+ fmt.Fprintf(c.Writer, "error: must be run from the correct taskqueue")
return
}
}
- h(c, rw, r, p)
+ next(c)
}
}
« no previous file with comments | « appengine/gaeauth/server/internal/authdb/handlers_test.go ('k') | appengine/gaemiddleware/appengine_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698