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

Unified Diff: server/router/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: server/router/handler.go
diff --git a/server/router/handler.go b/server/router/handler.go
new file mode 100644
index 0000000000000000000000000000000000000000..84d85a05e36414b5a6c5ef559cb5ac10be3179ff
--- /dev/null
+++ b/server/router/handler.go
@@ -0,0 +1,64 @@
+// Copyright 2016 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 router
+
+import "golang.org/x/net/context"
+
+// TODO(nishanths): Base and TestingBase need better names now that they are in
+// a package named router. Previously, it was fine because they were in the
+// middleware package.
+
+type (
+ // Handler is the type for all request handlers.
+ Handler func(*Context)
+
+ // Middleware is a function that returns a Handler.
+ Middleware func() Handler
nodir 2016/06/13 20:20:52 I think this type is unnecessary
+
+ // Base returns a list of handlers. It usually is
+ // the start of the middleware chain.
+ // TODO(nishanths): It may not be necessary to have a specific type for
nodir 2016/06/13 20:20:52 "//\n" before TODO
+ // this after issue 617774.
+ Base func() []Handler
nodir 2016/06/13 20:20:52 why Base is a function that returns []Handler rath
+)
+
+// TestingBase is Base that passes given context to the handler. Useful in
+// tests.
+func TestingBase(c context.Context) Base {
+ return func() (h []Handler) {
+ return []Handler{func(ctx *Context) {
+ ctx.Context = c
+ }}
+ }
+}
+
+// HandlerWithContext creates a handler that assigns the given context to
+// the Context field.
+func HandlerWithContext(c context.Context) Handler {
+ return func(ctx *Context) {
+ ctx.Context = c
+ }
+}
+
+// ChainHandlers combines handlers into a single executable function.
+// Useful to create a single executable function for tests.
+// The Writer, Request, and Params fields in the Context available to the
+// handlers are not set and should be set before use.
+func ChainHandlers(handlers ...Handler) func() {
+ return func() {
+ if len(handlers) > 0 {
+ c := &Context{handlers: handlers, index: -1}
+ c.Next()
+ }
+ }
+}
+
+// HandlerWithContextValue is a middleware that adds a value to the context before
+// calling the handler.
+func HandlerWithContextValue(key, val interface{}) Handler {
+ return func(c *Context) {
+ c.Context = context.WithValue(c.Context, key, val)
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698