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

Unified Diff: appengine/middleware/context.go

Issue 1402543003: Add initial middleware package. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: nits Created 5 years, 2 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/middleware/appengine_test.go ('k') | appengine/middleware/doc.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/middleware/context.go
diff --git a/appengine/middleware/context.go b/appengine/middleware/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..c98aae895e93b7429565089e839a8aa4011f6601
--- /dev/null
+++ b/appengine/middleware/context.go
@@ -0,0 +1,48 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/julienschmidt/httprouter"
+ "github.com/luci/gae/impl/memory"
+ "github.com/luci/gae/impl/prod"
+ "github.com/luci/luci-go/appengine/gaelogger"
+ "github.com/luci/luci-go/common/logging/memlogger"
+ "golang.org/x/net/context"
+)
+
+// Handler is the type for all middleware handlers. Of particular note, it's the
+// same as httprouder.Handle, except that it also has a context parameter.
+type Handler func(context.Context, http.ResponseWriter, *http.Request, httprouter.Params)
+
+// Base adapts a middleware-style handler to a httprouter.Handle. It passes
+// a new, empty context to `h`.
+func Base(h Handler) httprouter.Handle {
+ return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
+ h(context.Background(), rw, r, p)
+ }
+}
+
+// BaseProd adapts a middleware-style handler to a httprouter.Handle. It passes
+// a new context to `h` with the following services installed:
+// * github.com/luci/gae/impl/prod (production appengine services)
+// * github.com/luci/luci-go/appengine/gaelogger (appengine logging service)
+func BaseProd(h Handler) httprouter.Handle {
+ return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
+ h(gaelogger.Use(prod.UseRequest(r)), rw, r, p)
+ }
+}
+
+// BaseTest adapts a middleware-style handler to a httprouter.Handle. It passes
+// a new context to `h` with the following services installed:
+// * github.com/luci/gae/impl/memory (in-memory appengine services)
+// * github.com/luci/luci-go/common/logging/memlogger (in-memory logging service)
+func BaseTest(h Handler) httprouter.Handle {
+ return func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
+ h(memlogger.Use(memory.Use(context.Background())), rw, r, p)
+ }
+}
« no previous file with comments | « appengine/middleware/appengine_test.go ('k') | appengine/middleware/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698