github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/api/router.go (about) 1 /* 2 Copyright 2021 The TestGrid Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package api provides code to host an API displaying TestGrid data 18 package api 19 20 import ( 21 "context" 22 "fmt" 23 "net/http" 24 "time" 25 26 "cloud.google.com/go/storage" 27 "github.com/go-chi/chi" 28 "google.golang.org/grpc" 29 "google.golang.org/grpc/reflection" 30 31 v1pb "github.com/GoogleCloudPlatform/testgrid/pb/api/v1" 32 v1 "github.com/GoogleCloudPlatform/testgrid/pkg/api/v1" 33 "github.com/GoogleCloudPlatform/testgrid/util/gcs" 34 ) 35 36 // RouterOptions are the options needed to GetRouter 37 type RouterOptions struct { 38 GcsCredentials string 39 HomeBucket string 40 TabPathPrefix string 41 SummaryPathPrefix string 42 AccessControlAllowOrigin string 43 Timeout time.Duration 44 } 45 46 const v1InfixRef = "/api/v1" 47 48 var healthCheckFile = "pkg/api/README.md" // a relative path 49 50 // GetRouters returns an http router and gRPC server that both serve TestGrid's API 51 // It also instantiates necessary caching and i/o objects 52 func GetRouters(options RouterOptions, storageClient *storage.Client) (http.Handler, *grpc.Server, error) { 53 server, err := GetServer(options, storageClient) 54 if err != nil { 55 return nil, nil, err 56 } 57 58 router := chi.NewRouter() 59 router.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { 60 http.ServeFile(w, req, healthCheckFile) 61 }) 62 router.Mount(v1InfixRef, v1.Route(nil, *server)) 63 64 grpcOptions := []grpc.ServerOption{} 65 grpcServer := grpc.NewServer(grpcOptions...) 66 v1pb.RegisterTestGridDataServer(grpcServer, server) 67 reflection.Register(grpcServer) 68 69 return router, grpcServer, nil 70 } 71 72 // GetServer returns a server that serves TestGrid's API 73 // It also instantiates necessary caching and i/o objects 74 func GetServer(options RouterOptions, storageClient *storage.Client) (*v1.Server, error) { 75 if storageClient == nil { 76 sc, err := gcs.ClientWithCreds(context.Background(), options.GcsCredentials) 77 if err != nil { 78 return nil, fmt.Errorf("clientWithCreds(): %w", err) 79 } 80 storageClient = sc 81 } 82 83 return &v1.Server{ 84 Client: gcs.NewClient(storageClient), 85 DefaultBucket: options.HomeBucket, 86 TabPathPrefix: options.TabPathPrefix, 87 SummaryPathPrefix: options.SummaryPathPrefix, 88 AccessControlAllowOrigin: options.AccessControlAllowOrigin, 89 Timeout: options.Timeout, 90 }, nil 91 }