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

Side by Side Diff: server/auth/info/info.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/auth/handlers.go ('k') | server/auth/info/info_test.go » ('j') | 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 info exposes /auth/api/v1/server/info URL and allows to query it. 5 // Package info exposes /auth/api/v1/server/info URL and allows to query it.
6 // 6 //
7 // This endpoint is used by luci services to advertise service account names 7 // This endpoint is used by luci services to advertise service account names
8 // they use (among other things). 8 // they use (among other things).
9 package info 9 package info
10 10
11 import ( 11 import (
12 "encoding/json" 12 "encoding/json"
13 "net/http" 13 "net/http"
14 "time" 14 "time"
15 15
16 "github.com/julienschmidt/httprouter"
17 "golang.org/x/net/context" 16 "golang.org/x/net/context"
18 17
19 "github.com/luci/luci-go/server/auth/internal" 18 "github.com/luci/luci-go/server/auth/internal"
20 "github.com/luci/luci-go/server/middleware"
21 "github.com/luci/luci-go/server/proccache" 19 "github.com/luci/luci-go/server/proccache"
20 "github.com/luci/luci-go/server/router"
22 ) 21 )
23 22
24 // ServiceInfo describes JSON format of /auth/api/v1/server/info response. 23 // ServiceInfo describes JSON format of /auth/api/v1/server/info response.
25 type ServiceInfo struct { 24 type ServiceInfo struct {
26 AppID string `json:"app_id,omitempty"` 25 AppID string `json:"app_id,omitempty"`
27 AppRuntime string `json:"app_runtime,omitempty"` 26 AppRuntime string `json:"app_runtime,omitempty"`
28 AppRuntimeVersion string `json:"app_runtime_version,omitempty"` 27 AppRuntimeVersion string `json:"app_runtime_version,omitempty"`
29 AppVersion string `json:"app_version,omitempty"` 28 AppVersion string `json:"app_version,omitempty"`
30 ServiceAccountName string `json:"service_account_name,omitempty"` 29 ServiceAccountName string `json:"service_account_name,omitempty"`
31 } 30 }
(...skipping 17 matching lines...) Expand all
49 if err != nil { 48 if err != nil {
50 return nil, err 49 return nil, err
51 } 50 }
52 return info.(*ServiceInfo), nil 51 return info.(*ServiceInfo), nil
53 } 52 }
54 53
55 // ServiceInfoCallback is called by /auth/api/v1/server/info handler. 54 // ServiceInfoCallback is called by /auth/api/v1/server/info handler.
56 type ServiceInfoCallback func(context.Context) (ServiceInfo, error) 55 type ServiceInfoCallback func(context.Context) (ServiceInfo, error)
57 56
58 // InstallHandlers installs handler that serves info provided by the callback. 57 // InstallHandlers installs handler that serves info provided by the callback.
59 func InstallHandlers(r *httprouter.Router, base middleware.Base, cb ServiceInfoC allback) { 58 func InstallHandlers(r *router.Router, base router.MiddlewareChain, cb ServiceIn foCallback) {
60 » handler := func(c context.Context, w http.ResponseWriter, r *http.Reques t, p httprouter.Params) { 59 » handler := func(c *router.Context) {
61 var response struct { 60 var response struct {
62 ServiceInfo 61 ServiceInfo
63 Error string `json:"error,omitempty"` 62 Error string `json:"error,omitempty"`
64 } 63 }
65 » » info, err := cb(c) 64 » » info, err := cb(c.Context)
66 » » w.Header().Set("Content-Type", "application/json; charset=utf-8" ) 65 » » c.Writer.Header().Set("Content-Type", "application/json; charset =utf-8")
67 if err != nil { 66 if err != nil {
68 » » » w.WriteHeader(http.StatusInternalServerError) 67 » » » c.Writer.WriteHeader(http.StatusInternalServerError)
69 response.Error = err.Error() 68 response.Error = err.Error()
70 } else { 69 } else {
71 » » » w.WriteHeader(http.StatusOK) 70 » » » c.Writer.WriteHeader(http.StatusOK)
72 response.ServiceInfo = info 71 response.ServiceInfo = info
73 } 72 }
74 » » json.NewEncoder(w).Encode(response) 73 » » json.NewEncoder(c.Writer).Encode(response)
75 } 74 }
76 » r.GET("/auth/api/v1/server/info", base(handler)) 75 » r.GET("/auth/api/v1/server/info", base, handler)
77 } 76 }
OLDNEW
« no previous file with comments | « server/auth/handlers.go ('k') | server/auth/info/info_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698