Chromium Code Reviews| Index: milo/appengine/swarming/html.go |
| diff --git a/milo/appengine/swarming/html.go b/milo/appengine/swarming/html.go |
| index fa17ac78956229551da1ee66b9b1b11d197c5375..272575c421128f39c69429a7bcf791b1155051b4 100644 |
| --- a/milo/appengine/swarming/html.go |
| +++ b/milo/appengine/swarming/html.go |
| @@ -8,12 +8,12 @@ import ( |
| "net/http" |
| "os" |
| - "github.com/julienschmidt/httprouter" |
| "golang.org/x/net/context" |
| "google.golang.org/api/googleapi" |
| - "github.com/luci/luci-go/milo/appengine/settings" |
| + "github.com/luci/luci-go/milo/appengine/common" |
| "github.com/luci/luci-go/milo/common/miloerror" |
| + "github.com/luci/luci-go/server/router" |
| "github.com/luci/luci-go/server/templates" |
| ) |
| @@ -50,9 +50,6 @@ func getSwarmingService(c context.Context, host string) (swarmingService, error) |
| } |
| } |
| -// Log is for fetching logs from swarming. |
| -type Log struct{} |
| - |
| // Build is for deciphering recipe builds from swarming based off of logs. |
| type Build struct { |
|
nodir
2017/03/17 20:47:58
why do we need this type?
hinoka
2017/03/17 22:04:55
It's used as a delivery mechanism to pass the buil
|
| // bl is the buildLoader to use. A zero value is suitable for production, but |
| @@ -60,86 +57,71 @@ type Build struct { |
| bl buildLoader |
| } |
| -// GetTemplateName for Log returns the template name for log pages. |
| -func (l Log) GetTemplateName(t settings.Theme) string { |
| - return "log.html" |
| -} |
| - |
| -// Render writes the build log to the given response writer. |
| -func (l Log) Render(c context.Context, r *http.Request, p httprouter.Params) (*templates.Args, error) { |
| - id := p.ByName("id") |
| +// LogHandler writes the build log to the given response writer. |
| +func LogHandler(c *router.Context) { |
| + id := c.Params.ByName("id") |
| if id == "" { |
| - return nil, &miloerror.Error{ |
| - Message: "No id", |
| - Code: http.StatusBadRequest, |
| - } |
| + common.ErrorPage(c, http.StatusBadRequest, "No id") |
| + return |
| } |
| - logname := p.ByName("logname") |
| + logname := c.Params.ByName("logname") |
| if logname == "" { |
| - return nil, &miloerror.Error{ |
| - Message: "No log name", |
| - Code: http.StatusBadRequest, |
| - } |
| + common.ErrorPage(c, http.StatusBadRequest, "No log name") |
| } |
| - sf, err := getSwarmingService(c, getSwarmingHost(r)) |
| + sf, err := getSwarmingService(c.Context, getSwarmingHost(c.Request)) |
| if err != nil { |
| - return nil, convertErr(err) |
| + common.ErrorPage(c, errCode(err), "No log name") |
| + return |
| } |
| - log, closed, err := swarmingBuildLogImpl(c, sf, id, logname) |
| + log, closed, err := swarmingBuildLogImpl(c.Context, sf, id, logname) |
| if err != nil { |
| - return nil, convertErr(err) |
| + common.ErrorPage(c, errCode(err), err.Error()) |
| + return |
| } |
| - args := &templates.Args{ |
| + templates.MustRender(c.Context, c.Writer, "pages/log.html", templates.Args{ |
| "Log": log, |
| "Closed": closed, |
| - } |
| - return args, nil |
| + }) |
| } |
| -// GetTemplateName for Build returns the template name for build pages. |
| -func (b Build) GetTemplateName(t settings.Theme) string { |
| - return "build.html" |
| +func BuildHandler(c *router.Context) { |
| + (Build{}).handler(c) |
| } |
| // Render renders both the build page and the log. |
|
nodir
2017/03/17 20:47:58
s/Render/handler
or rename func to Render
hinoka
2017/03/17 22:04:55
Done.
|
| -func (b Build) Render(c context.Context, r *http.Request, p httprouter.Params) (*templates.Args, error) { |
| +func (b Build) handler(c *router.Context) { |
| // Get the swarming ID |
| - id := p.ByName("id") |
| + id := c.Params.ByName("id") |
| if id == "" { |
| - return nil, &miloerror.Error{ |
| - Message: "No id", |
| - Code: http.StatusBadRequest, |
| - } |
| + common.ErrorPage(c, http.StatusBadRequest, "No id") |
| + return |
| } |
| - sf, err := getSwarmingService(c, getSwarmingHost(r)) |
| + sf, err := getSwarmingService(c.Context, getSwarmingHost(c.Request)) |
| if err != nil { |
| - return nil, convertErr(err) |
| + common.ErrorPage(c, errCode(err), err.Error()) |
| + return |
| } |
| - result, err := b.bl.swarmingBuildImpl(c, sf, r.URL.String(), id) |
| + result, err := b.bl.swarmingBuildImpl(c.Context, sf, c.Request.URL.String(), id) |
| if err != nil { |
| - return nil, convertErr(err) |
| + common.ErrorPage(c, errCode(err), err.Error()) |
| + return |
| } |
| - // Render into the template |
| - args := &templates.Args{ |
| + templates.MustRender(c.Context, c.Writer, "pages/build.html", templates.Args{ |
| "Build": result, |
| - } |
| - return args, nil |
| + }) |
| } |
| -func convertErr(err error) error { |
| +func errCode(err error) int { |
| if isAPINotFound(err) || os.IsNotExist(err) { |
| - return &miloerror.Error{ |
| - Message: err.Error(), |
| - Code: http.StatusNotFound, |
| - } |
| + return http.StatusNotFound |
| } |
| - return err |
| + return http.StatusInternalServerError |
| } |
| // isAPINotFound returns true if err is a HTTP 404 API response. |
| @@ -147,6 +129,5 @@ func isAPINotFound(err error) bool { |
| if apiErr, ok := err.(*googleapi.Error); ok && apiErr.Code == http.StatusNotFound { |
| return true |
| } |
| - |
| return false |
| } |