Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Package som implements HTTP server that handles requests to default module. | 5 // Package som implements HTTP server that handles requests to default module. |
| 6 package som | 6 package som |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 9 "crypto/sha1" | 9 "crypto/sha1" |
| 10 "encoding/json" | 10 "encoding/json" |
| 11 "fmt" | 11 "fmt" |
| 12 "html/template" | 12 "html/template" |
| 13 "infra/monorail" | |
| 13 "io/ioutil" | 14 "io/ioutil" |
| 14 "net/http" | 15 "net/http" |
| 15 "strings" | 16 "strings" |
| 16 | 17 |
| 17 "github.com/julienschmidt/httprouter" | 18 "github.com/julienschmidt/httprouter" |
| 18 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 19 "google.golang.org/appengine" | 20 "google.golang.org/appengine" |
| 21 "google.golang.org/appengine/urlfetch" | |
|
martiniss
2016/06/09 18:36:07
You should use https://godoc.org/github.com/luci/l
seanmccullough1
2016/06/09 19:02:57
Done.
| |
| 20 | 22 |
| 21 "github.com/luci/gae/service/datastore" | 23 "github.com/luci/gae/service/datastore" |
| 22 "github.com/luci/luci-go/appengine/gaeauth/server" | 24 "github.com/luci/luci-go/appengine/gaeauth/server" |
| 23 "github.com/luci/luci-go/appengine/gaemiddleware" | 25 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 24 "github.com/luci/luci-go/common/clock" | 26 "github.com/luci/luci-go/common/clock" |
| 25 "github.com/luci/luci-go/common/logging" | 27 "github.com/luci/luci-go/common/logging" |
| 26 "github.com/luci/luci-go/server/auth" | 28 "github.com/luci/luci-go/server/auth" |
| 27 "github.com/luci/luci-go/server/auth/identity" | 29 "github.com/luci/luci-go/server/auth/identity" |
| 28 "github.com/luci/luci-go/server/middleware" | 30 "github.com/luci/luci-go/server/middleware" |
| 29 "github.com/luci/luci-go/server/settings" | 31 "github.com/luci/luci-go/server/settings" |
| 30 ) | 32 ) |
| 31 | 33 |
| 32 const authGroup = "sheriff-o-matic-access" | 34 const authGroup = "sheriff-o-matic-access" |
| 33 | 35 |
| 34 var ( | 36 var ( |
| 35 mainPage = template.Must(template.ParseFiles("./index.html")) | 37 mainPage = template.Must(template.ParseFiles("./index.html")) |
| 36 accessDeniedPage = template.Must(template.ParseFiles("./access-denied.ht ml")) | 38 accessDeniedPage = template.Must(template.ParseFiles("./access-denied.ht ml")) |
| 39 // Set to "http://localhost:9090/_ah/api/monorail/v1/" to debug locally. | |
| 40 monorailEndpoint = "https://monorail-prod.appspot.com/_ah/api/monorail/v 1/" | |
| 37 ) | 41 ) |
| 38 | 42 |
| 39 var errStatus = func(w http.ResponseWriter, status int, msg string) { | 43 var errStatus = func(w http.ResponseWriter, status int, msg string) { |
| 40 w.WriteHeader(status) | 44 w.WriteHeader(status) |
| 41 w.Write([]byte(msg)) | 45 w.Write([]byte(msg)) |
| 42 } | 46 } |
| 43 | 47 |
| 44 var requireGoogler = func(w http.ResponseWriter, c context.Context) bool { | 48 var requireGoogler = func(w http.ResponseWriter, c context.Context) bool { |
| 45 if appengine.IsDevAppServer() { | 49 if appengine.IsDevAppServer() { |
| 46 return true | 50 return true |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 data, err := json.Marshal(annotation) | 366 data, err := json.Marshal(annotation) |
| 363 if err != nil { | 367 if err != nil { |
| 364 errStatus(w, http.StatusInternalServerError, err.Error()) | 368 errStatus(w, http.StatusInternalServerError, err.Error()) |
| 365 return | 369 return |
| 366 } | 370 } |
| 367 | 371 |
| 368 w.Header().Set("Content-Type", "application/json") | 372 w.Header().Set("Content-Type", "application/json") |
| 369 w.Write(data) | 373 w.Write(data) |
| 370 } | 374 } |
| 371 | 375 |
| 376 func getBugQueueHandler(c context.Context, w http.ResponseWriter, r *http.Reques t, p httprouter.Params) { | |
| 377 c = appengine.NewContext(r) | |
| 378 mr := monorail.NewEndpointsClient(urlfetch.Client(c), monorailEndpoint) | |
| 379 tree := p.ByName("tree") | |
| 380 req := &monorail.IssuesListRequest{ | |
| 381 ProjectId: tree, | |
| 382 Can: monorail.IssuesListRequest_OPEN, | |
| 383 Q: fmt.Sprintf("label:Sheriff-%s", tree), | |
| 384 } | |
| 385 | |
| 386 res, err := mr.IssuesList(c, req) | |
| 387 if err != nil { | |
| 388 errStatus(w, http.StatusInternalServerError, err.Error()) | |
| 389 return | |
| 390 } | |
| 391 | |
| 392 bytes, err := json.Marshal(res) | |
| 393 if err != nil { | |
| 394 errStatus(w, http.StatusInternalServerError, err.Error()) | |
| 395 return | |
| 396 } | |
| 397 | |
| 398 w.Header().Set("Content-Type", "application/json") | |
| 399 w.Write(bytes) | |
| 400 } | |
| 401 | |
| 372 // base is the root of the middleware chain. | 402 // base is the root of the middleware chain. |
| 373 func base(h middleware.Handler) httprouter.Handle { | 403 func base(h middleware.Handler) httprouter.Handle { |
| 374 methods := auth.Authenticator{ | 404 methods := auth.Authenticator{ |
| 375 &server.OAuth2Method{Scopes: []string{server.EmailScope}}, | 405 &server.OAuth2Method{Scopes: []string{server.EmailScope}}, |
| 376 server.CookieAuth, | 406 server.CookieAuth, |
| 377 &server.InboundAppIDAuthMethod{}, | 407 &server.InboundAppIDAuthMethod{}, |
| 378 } | 408 } |
| 379 h = auth.Use(h, methods) | 409 h = auth.Use(h, methods) |
| 380 if !appengine.IsDevAppServer() { | 410 if !appengine.IsDevAppServer() { |
| 381 h = middleware.WithPanicCatcher(h) | 411 h = middleware.WithPanicCatcher(h) |
| 382 } | 412 } |
| 383 return gaemiddleware.BaseProd(h) | 413 return gaemiddleware.BaseProd(h) |
| 384 } | 414 } |
| 385 | 415 |
| 386 //// Routes. | 416 //// Routes. |
| 387 func init() { | 417 func init() { |
| 388 settings.RegisterUIPage(settingsKey, settingsUIPage{}) | 418 settings.RegisterUIPage(settingsKey, settingsUIPage{}) |
| 389 | 419 |
| 390 router := httprouter.New() | 420 router := httprouter.New() |
| 391 gaemiddleware.InstallHandlers(router, base) | 421 gaemiddleware.InstallHandlers(router, base) |
| 392 router.GET("/api/v1/trees/", base(auth.Authenticate(getTreesHandler))) | 422 router.GET("/api/v1/trees/", base(auth.Authenticate(getTreesHandler))) |
| 393 router.GET("/api/v1/alerts/:tree", base(auth.Authenticate(getAlertsHandl er))) | 423 router.GET("/api/v1/alerts/:tree", base(auth.Authenticate(getAlertsHandl er))) |
| 394 router.POST("/api/v1/alerts/:tree", base(auth.Authenticate(postAlertsHan dler))) | 424 router.POST("/api/v1/alerts/:tree", base(auth.Authenticate(postAlertsHan dler))) |
| 395 router.GET("/api/v1/annotations/", base(auth.Authenticate(getAnnotations Handler))) | 425 router.GET("/api/v1/annotations/", base(auth.Authenticate(getAnnotations Handler))) |
| 396 router.POST("/api/v1/annotations/:annKey/:action", base(auth.Authenticat e(postAnnotationsHandler))) | 426 router.POST("/api/v1/annotations/:annKey/:action", base(auth.Authenticat e(postAnnotationsHandler))) |
| 427 router.GET("/api/v1/bugqueue/:tree", base(auth.Authenticate(getBugQueueH andler))) | |
| 397 | 428 |
| 398 rootRouter := httprouter.New() | 429 rootRouter := httprouter.New() |
| 399 rootRouter.GET("/*path", base(auth.Authenticate(indexPage))) | 430 rootRouter.GET("/*path", base(auth.Authenticate(indexPage))) |
| 400 | 431 |
| 401 http.DefaultServeMux.Handle("/api/", router) | 432 http.DefaultServeMux.Handle("/api/", router) |
| 402 http.DefaultServeMux.Handle("/admin/", router) | 433 http.DefaultServeMux.Handle("/admin/", router) |
| 403 http.DefaultServeMux.Handle("/auth/", router) | 434 http.DefaultServeMux.Handle("/auth/", router) |
| 404 http.DefaultServeMux.Handle("/_ah/", router) | 435 http.DefaultServeMux.Handle("/_ah/", router) |
| 405 http.DefaultServeMux.Handle("/", rootRouter) | 436 http.DefaultServeMux.Handle("/", rootRouter) |
| 406 } | 437 } |
| OLD | NEW |