Chromium Code Reviews| Index: appengine/cmd/milo/logdog/http.go |
| diff --git a/appengine/cmd/milo/logdog/http.go b/appengine/cmd/milo/logdog/http.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..80a51482e5c3d02a5c4e94d8c12dac322eeaaac4 |
| --- /dev/null |
| +++ b/appengine/cmd/milo/logdog/http.go |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2016 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 logdog |
| + |
| +import ( |
| + "net/http" |
| + "strings" |
| + "sync" |
| + |
| + "github.com/julienschmidt/httprouter" |
| + "github.com/luci/luci-go/appengine/cmd/milo/miloerror" |
| + "github.com/luci/luci-go/appengine/cmd/milo/settings" |
| + authClient "github.com/luci/luci-go/appengine/gaeauth/client" |
| + "github.com/luci/luci-go/common/config" |
| + log "github.com/luci/luci-go/common/logging" |
| + "github.com/luci/luci-go/logdog/common/types" |
| + "github.com/luci/luci-go/server/templates" |
| + |
| + "golang.org/x/net/context" |
| +) |
| + |
| +// AnnotationStream is a ThemedHandler that renders a LogDog Milo annotation |
| +// protobuf stream. |
| +// |
| +// The protobuf stream is fetched live from LogDog and cached locally, either |
| +// temporarily (if incomplete) or indefinitely (if complete). |
| +type AnnotationStream struct { |
| + // logDogClient is a reusable HTTP client to use for LogDog. |
| + logDogClient *http.Client |
| + |
| + initMu sync.Mutex |
| +} |
| + |
| +// init initializes the process-global state of the AnnotationStream. |
| +func (s *AnnotationStream) init(c context.Context) *miloerror.Error { |
| + s.initMu.Lock() |
|
nodir
2016/07/29 23:00:32
insert a s.logdogClient!=nil check with read-lock
dnj
2016/07/29 23:24:43
Done.
|
| + defer s.initMu.Unlock() |
| + |
| + if s.logDogClient != nil { |
| + return nil |
| + } |
| + |
| + // Initialize the LogDog client. |
| + a, err := authClient.Transport(c, nil, nil) |
| + if err != nil { |
| + log.WithError(err).Errorf(c, "Failed to get transport for LogDog server.") |
| + return &miloerror.Error{ |
| + Code: http.StatusInternalServerError, |
| + } |
| + } |
| + |
| + s.logDogClient = &http.Client{ |
| + Transport: a, |
| + } |
| + return nil |
| +} |
| + |
| +// GetTemplateName implements settings.ThemedHandler. |
| +func (s *AnnotationStream) GetTemplateName(t settings.Theme) string { |
| + return "build.html" |
| +} |
| + |
| +// Render implements settings.ThemedHandler. |
| +func (s *AnnotationStream) Render(c context.Context, req *http.Request, p httprouter.Params) (*templates.Args, error) { |
| + if err := s.init(c); err != nil { |
| + return nil, err |
| + } |
| + |
| + as := annotationStreamRequest{ |
| + AnnotationStream: s, |
| + |
| + project: config.ProjectName(p.ByName("project")), |
| + path: types.StreamPath(strings.Trim(p.ByName("path"), "/")), |
| + host: req.FormValue("host"), |
| + } |
| + if err := as.normalize(); err != nil { |
| + return nil, err |
| + } |
| + |
| + // Load the Milo annotation protobuf from the annotation stream. |
| + if err := as.load(c); err != nil { |
| + return nil, err |
| + } |
| + |
| + // Convert the Milo Annotation protobuf to Milo objects. |
| + return &templates.Args{ |
| + "Build": as.toMiloBuild(c), |
| + }, nil |
| +} |