github.com/GoogleCloudPlatform/testgrid@v0.0.174/cmd/api/main.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  // The api utility hosts a web server that serves TestGrid data
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"net"
    24  	"net/http"
    25  	"time"
    26  
    27  	"github.com/sirupsen/logrus"
    28  
    29  	"github.com/GoogleCloudPlatform/testgrid/pkg/api"
    30  )
    31  
    32  type options struct {
    33  	httpPort string
    34  	grpcPort string
    35  	router   api.RouterOptions
    36  }
    37  
    38  func gatherOptions() (options, error) {
    39  	var o options
    40  	flag.StringVar(&o.router.HomeBucket, "scope", "", "Local or cloud TestGrid context to read from")
    41  	flag.StringVar(&o.router.GcsCredentials, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
    42  	flag.StringVar(&o.httpPort, "port", "8080", "Alias for http-port")
    43  	flag.StringVar(&o.httpPort, "http-port", "8080", "Port to deploy REST server to")
    44  	flag.StringVar(&o.grpcPort, "grpc-port", "50051", "Port to deploy gRPC server to")
    45  	flag.StringVar(&o.router.AccessControlAllowOrigin, "allowed-origin", "", "Allowed 'Access-Control-Allow-Origin' for HTTP calls, if any")
    46  	flag.StringVar(&o.router.TabPathPrefix, "tab", "tabs", "Read tab states under this path")
    47  	flag.StringVar(&o.router.SummaryPathPrefix, "summary", "summary", "Read summaries under this path.")
    48  	flag.DurationVar(&o.router.Timeout, "timeout", 10*time.Minute, "Maximum time allocated to complete one request")
    49  	flag.Parse()
    50  
    51  	return o, nil
    52  }
    53  
    54  func main() {
    55  	log := logrus.WithField("component", "api")
    56  	opt, err := gatherOptions()
    57  	if err != nil {
    58  		log.WithError(err).Fatal("Can't parse options")
    59  	}
    60  
    61  	httpMux, grpcMux, err := api.GetRouters(opt.router, nil)
    62  	if err != nil {
    63  		log.WithError(err).WithField("router-options", opt.router).Fatal("Can't create router")
    64  	}
    65  
    66  	terminate := make(chan interface{})
    67  	go func() {
    68  		lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%s", opt.grpcPort))
    69  		if err != nil {
    70  			log.Fatalf("failed to listen: %v", err)
    71  		}
    72  
    73  		log.WithField("port", opt.grpcPort).Info("Listening via gRPC...")
    74  		if err := grpcMux.Serve(lis); err != nil {
    75  			log.WithError(err).Error("gRPC Server Error")
    76  		} else {
    77  			log.Info("gRPC listener stopped listening (with no error)")
    78  		}
    79  		terminate <- 0
    80  	}()
    81  
    82  	log.WithField("port", opt.httpPort).Info("Listening via http...")
    83  	if err := http.ListenAndServe(fmt.Sprintf(":%s", opt.httpPort), httpMux); err != nil {
    84  		log.WithError(err).Error("HTTP Server Error")
    85  	} else {
    86  		log.Info("HTTP listener stopped listening (with no error)")
    87  	}
    88  	<-terminate
    89  }