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

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: swap args to prevent ugly chains 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
Index: appengine/middleware/context.go
diff --git a/appengine/middleware/context.go b/appengine/middleware/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..f017733c6ba57d0b9beb61bd018c544441c1ceae
--- /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 has httprouder.Handle, except that it also has a context parameter.
Vadim Sh. 2015/10/13 01:29:14 typo: same as?
iannucci 2015/10/13 01:37:17 yup
+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 {
dnj 2015/10/13 01:44:49 Make this take context.Context. context.Background
iannucci 2015/10/13 01:56:34 I think you misunderstand the point of this. This
dnj 2015/10/13 04:39:10 Context generation shouldn't have to start at the
iannucci 2015/10/13 04:52:29 Ah, ah, ok... I misunderstood you. The amount of
+ 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)
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698