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

Unified Diff: milo/appengine/common/middleware.go

Issue 2748073006: Milo Refactor: Remove theme support (Closed)
Patch Set: Review Created 3 years, 9 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: milo/appengine/common/middleware.go
diff --git a/milo/appengine/common/middleware.go b/milo/appengine/common/middleware.go
new file mode 100644
index 0000000000000000000000000000000000000000..fac2f4e18e2b9d4bb5d43116d796442ffadb4821
--- /dev/null
+++ b/milo/appengine/common/middleware.go
@@ -0,0 +1,93 @@
+// Copyright 2015 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 common
+
+import (
+ "golang.org/x/net/context"
nodir 2017/03/17 20:47:58 move it after stdlib imports
hinoka 2017/03/17 22:04:55 gofmt just did it for me.
+ "net/http"
+ "strings"
+
+ "github.com/luci/gae/service/info"
+ "github.com/luci/luci-go/appengine/gaeauth/server"
+ "github.com/luci/luci-go/appengine/gaemiddleware"
+ "github.com/luci/luci-go/common/clock"
+ "github.com/luci/luci-go/server/analytics"
+ "github.com/luci/luci-go/server/auth"
+ "github.com/luci/luci-go/server/router"
+ "github.com/luci/luci-go/server/templates"
+)
+
+// getTemplateBundles is used to render HTML templates. It provides a base args
nodir 2017/03/17 20:47:58 "GetTemplateBundle"
nodir 2017/03/17 20:47:58 "base args" without "a"
hinoka 2017/03/17 22:04:55 Done.
+// passed to all templates.
+func GetTemplateBundle() *templates.Bundle {
+ return &templates.Bundle{
+ Loader: templates.FileSystemLoader("templates"),
+ DebugMode: info.IsDevAppServer,
+ DefaultTemplate: "base",
+ DefaultArgs: func(c context.Context) (templates.Args, error) {
+ r := getRequest(c)
+ path := r.URL.Path
+ loginURL, err := auth.LoginURL(c, path)
+ if err != nil {
+ return nil, err
+ }
+ logoutURL, err := auth.LogoutURL(c, path)
+ if err != nil {
+ return nil, err
+ }
+ if err != nil {
nodir 2017/03/17 20:47:58 remove
hinoka 2017/03/17 22:04:55 Done.
+ return nil, err
+ }
+ return templates.Args{
+ "AppVersion": strings.Split(info.VersionID(c), ".")[0],
+ "IsAnonymous": auth.CurrentIdentity(c) == "anonymous:anonymous",
nodir 2017/03/17 20:47:58 use https://godoc.org/github.com/luci/luci-go/serv
hinoka 2017/03/17 22:04:55 Done.
+ "User": auth.CurrentUser(c),
+ "LoginURL": loginURL,
+ "LogoutURL": logoutURL,
+ "CurrentTime": clock.Now(c),
+ "Analytics": analytics.Snippet(c),
+ "RequestID": info.RequestID(c),
+ }, nil
+ },
+ FuncMap: funcMap,
+ }
+}
+
+// Base returns the basic luci appengine middlewares.
nodir 2017/03/17 20:47:58 "LUCI" because it is Go
hinoka 2017/03/17 22:04:55 Done.
+func Base() router.MiddlewareChain {
+ methods := auth.Authenticator{
+ &server.OAuth2Method{Scopes: []string{server.EmailScope}},
+ server.CookieAuth,
+ &server.InboundAppIDAuthMethod{},
+ }
+ m := gaemiddleware.BaseProd().Extend(auth.Use(methods), auth.Authenticate)
+ m = m.Extend(withRequestMiddleware)
+ m = m.Extend(templates.WithTemplates(GetTemplateBundle()))
+ return m
+}
+
+// The context key, so that we can embed the http.Request object into
+// the context.
+var requestKey = "http.request"
+
+// WithRequest returns a context with the http.Request object
+// in it.
+func WithRequest(c context.Context, r *http.Request) context.Context {
+ return context.WithValue(c, &requestKey, r)
+}
+
+// withRequestMiddleware is a middleware that installs a request into the context.
+// This is used for various things in the default template.
+func withRequestMiddleware(c *router.Context, next router.Handler) {
+ c.Context = WithRequest(c.Context, c.Request)
+ next(c)
+}
+
+func getRequest(c context.Context) *http.Request {
+ if req, ok := c.Value(&requestKey).(*http.Request); ok {
+ return req
+ }
+ panic("No http.request found in context")
+}

Powered by Google App Engine
This is Rietveld 408576698