| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 logdog | 5 package logdog |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 "strings" | 9 "strings" |
| 10 | 10 |
| 11 "github.com/luci/luci-go/common/errors" |
| 11 log "github.com/luci/luci-go/common/logging" | 12 log "github.com/luci/luci-go/common/logging" |
| 12 "github.com/luci/luci-go/grpc/prpc" | 13 "github.com/luci/luci-go/grpc/prpc" |
| 13 "github.com/luci/luci-go/logdog/client/coordinator" | 14 "github.com/luci/luci-go/logdog/client/coordinator" |
| 14 "github.com/luci/luci-go/logdog/common/types" | 15 "github.com/luci/luci-go/logdog/common/types" |
| 15 "github.com/luci/luci-go/luci_config/common/cfgtypes" | 16 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 16 "github.com/luci/luci-go/milo/appengine/settings" | 17 "github.com/luci/luci-go/milo/appengine/settings" |
| 17 "github.com/luci/luci-go/milo/common/miloerror" | 18 "github.com/luci/luci-go/milo/common/miloerror" |
| 18 "github.com/luci/luci-go/server/auth" | 19 "github.com/luci/luci-go/server/auth" |
| 19 "github.com/luci/luci-go/server/templates" | 20 "github.com/luci/luci-go/server/templates" |
| 20 | 21 |
| 21 "github.com/julienschmidt/httprouter" | 22 "github.com/julienschmidt/httprouter" |
| 22 "golang.org/x/net/context" | 23 "golang.org/x/net/context" |
| 23 ) | 24 ) |
| 24 | 25 |
| 25 // AnnotationStream is a ThemedHandler that renders a LogDog Milo annotation | 26 // AnnotationStreamHandler is a ThemedHandler that renders a LogDog Milo |
| 26 // protobuf stream. | 27 // annotation protobuf stream. |
| 27 // | 28 // |
| 28 // The protobuf stream is fetched live from LogDog and cached locally, either | 29 // The protobuf stream is fetched live from LogDog and cached locally, either |
| 29 // temporarily (if incomplete) or indefinitely (if complete). | 30 // temporarily (if incomplete) or indefinitely (if complete). |
| 30 type AnnotationStream struct { | 31 type AnnotationStreamHandler struct{} |
| 31 » // logDogClient is a reusable HTTP client to use for LogDog. | |
| 32 » logDogClient *http.Client | |
| 33 } | |
| 34 | 32 |
| 35 // GetTemplateName implements settings.ThemedHandler. | 33 // GetTemplateName implements settings.ThemedHandler. |
| 36 func (s *AnnotationStream) GetTemplateName(t settings.Theme) string { | 34 func (s *AnnotationStreamHandler) GetTemplateName(t settings.Theme) string { |
| 37 return "build.html" | 35 return "build.html" |
| 38 } | 36 } |
| 39 | 37 |
| 40 // Render implements settings.ThemedHandler. | 38 // Render implements settings.ThemedHandler. |
| 41 func (s *AnnotationStream) Render(c context.Context, req *http.Request, p httpro
uter.Params) (*templates.Args, error) { | 39 func (s *AnnotationStreamHandler) Render(c context.Context, req *http.Request, p
httprouter.Params) ( |
| 42 » // Initialize the LogDog client authentication. | 40 » *templates.Args, error) { |
| 43 » t, err := auth.GetRPCTransport(c, auth.AsUser) | 41 |
| 44 » if err != nil { | 42 » as := AnnotationStream{ |
| 45 » » log.WithError(err).Errorf(c, "Failed to get transport for LogDog
server.") | 43 » » Project: cfgtypes.ProjectName(p.ByName("project")), |
| 44 » » Path: types.StreamPath(strings.Trim(p.ByName("path"), "/")), |
| 45 » } |
| 46 » if err := as.Normalize(); err != nil { |
| 47 » » return nil, &miloerror.Error{ |
| 48 » » » Message: err.Error(), |
| 49 » » » Code: http.StatusBadRequest, |
| 50 » » } |
| 51 » } |
| 52 |
| 53 » // Setup our LogDog client. |
| 54 » var err error |
| 55 » if as.Client, err = NewClient(c, ""); err != nil { |
| 56 » » log.WithError(err).Errorf(c, "Failed to generate LogDog client."
) |
| 46 return nil, &miloerror.Error{ | 57 return nil, &miloerror.Error{ |
| 47 Code: http.StatusInternalServerError, | 58 Code: http.StatusInternalServerError, |
| 48 } | 59 } |
| 49 } | 60 } |
| 50 | 61 |
| 51 » as := annotationStreamRequest{ | 62 » // Load the Milo annotation protobuf from the annotation stream. |
| 52 » » AnnotationStream: s, | 63 » if _, err := as.Load(c); err != nil { |
| 53 | |
| 54 » » project: cfgtypes.ProjectName(p.ByName("project")), | |
| 55 » » path: types.StreamPath(strings.Trim(p.ByName("path"), "/")), | |
| 56 » » host: req.FormValue("host"), | |
| 57 » } | |
| 58 » if err := as.normalize(); err != nil { | |
| 59 return nil, err | 64 return nil, err |
| 60 } | 65 } |
| 61 | 66 |
| 62 // Setup our LogDog client. | |
| 63 as.logDogClient = coordinator.NewClient(&prpc.Client{ | |
| 64 C: &http.Client{ | |
| 65 Transport: t, | |
| 66 }, | |
| 67 Host: as.host, | |
| 68 }) | |
| 69 | |
| 70 // Load the Milo annotation protobuf from the annotation stream. | |
| 71 if err := as.load(c); err != nil { | |
| 72 return nil, err | |
| 73 } | |
| 74 | |
| 75 // Convert the Milo Annotation protobuf to Milo objects. | 67 // Convert the Milo Annotation protobuf to Milo objects. |
| 76 return &templates.Args{ | 68 return &templates.Args{ |
| 77 "Build": as.toMiloBuild(c), | 69 "Build": as.toMiloBuild(c), |
| 78 }, nil | 70 }, nil |
| 79 } | 71 } |
| 72 |
| 73 // NewClient generates a new LogDog client that issues requests on behalf of the |
| 74 // current user. |
| 75 func NewClient(c context.Context, host string) (*coordinator.Client, error) { |
| 76 if host == "" { |
| 77 host = defaultLogDogHost |
| 78 } |
| 79 |
| 80 // Initialize the LogDog client authentication. |
| 81 t, err := auth.GetRPCTransport(c, auth.AsUser) |
| 82 if err != nil { |
| 83 return nil, errors.New("failed to get transport for LogDog serve
r") |
| 84 } |
| 85 |
| 86 // Setup our LogDog client. |
| 87 return coordinator.NewClient(&prpc.Client{ |
| 88 C: &http.Client{ |
| 89 Transport: t, |
| 90 }, |
| 91 Host: host, |
| 92 }), nil |
| 93 } |
| OLD | NEW |