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

Side by Side Diff: server/templates/middleware.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 unified diff | Download patch
« no previous file with comments | « server/settings/admin/settings.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package templates 5 package templates
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "net/http" 9 "net/http"
10 10
11 » "github.com/julienschmidt/httprouter" 11 » "github.com/luci/luci-go/server/router"
12 » "golang.org/x/net/context"
13
14 » "github.com/luci/luci-go/server/middleware"
15 ) 12 )
16 13
17 // WithTemplates is middleware that lazily loads template bundle and injects it 14 // WithTemplates is middleware that lazily loads template bundle and injects it
18 // into the context. 15 // into the context.
19 // 16 //
20 // Wrapper reply with HTTP 500 if templates can not be loaded. Inner handler 17 // Wrapper reply with HTTP 500 if templates can not be loaded. Inner handler
21 // receives context with all templates successfully loaded. 18 // receives context with all templates successfully loaded.
22 func WithTemplates(h middleware.Handler, b *Bundle) middleware.Handler { 19 func WithTemplates(b *Bundle) router.Middleware {
23 » return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { 20 » return func(c *router.Context, next router.Handler) {
24 » » c = Use(c, b) // calls EnsureLoaded and initializes b.err inside 21 » » c.Context = Use(c.Context, b) // calls EnsureLoaded and initiali zes b.err inside
25 if b.err != nil { 22 if b.err != nil {
26 » » » http.Error(rw, fmt.Sprintf("Can't load HTML templates.\n %s", b.err), http.StatusInternalServerError) 23 » » » http.Error(c.Writer, fmt.Sprintf("Can't load HTML templa tes.\n%s", b.err), http.StatusInternalServerError)
27 return 24 return
28 } 25 }
29 » » h(c, rw, r, p) 26 » » next(c)
30 } 27 }
31 } 28 }
OLDNEW
« no previous file with comments | « server/settings/admin/settings.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698