| OLD | NEW |
| 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 } |
| OLD | NEW |