Merge branch '10666-report-version'
[arvados.git] / services / health / main.go
1 package main
2
3 import (
4         "flag"
5         "fmt"
6         "net/http"
7
8         "git.curoverse.com/arvados.git/sdk/go/arvados"
9         "git.curoverse.com/arvados.git/sdk/go/health"
10         "git.curoverse.com/arvados.git/sdk/go/httpserver"
11         log "github.com/Sirupsen/logrus"
12 )
13
14 var version = "dev"
15
16 func main() {
17         configFile := flag.String("config", arvados.DefaultConfigFile, "`path` to arvados configuration file")
18         getVersion := flag.Bool("version", false, "Print version information and exit.")
19         flag.Parse()
20
21         // Print version information if requested
22         if *getVersion {
23                 fmt.Printf("arvados-health %s\n", version)
24                 return
25         }
26
27         log.SetFormatter(&log.JSONFormatter{
28                 TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
29         })
30         log.Printf("arvados-health %s started", version)
31
32         cfg, err := arvados.GetConfig(*configFile)
33         if err != nil {
34                 log.Fatal(err)
35         }
36         clusterCfg, err := cfg.GetCluster("")
37         if err != nil {
38                 log.Fatal(err)
39         }
40         nodeCfg, err := clusterCfg.GetThisSystemNode()
41         if err != nil {
42                 log.Fatal(err)
43         }
44
45         log := log.WithField("Service", "Health")
46         srv := &httpserver.Server{
47                 Addr: nodeCfg.Health.Listen,
48                 Server: http.Server{
49                         Handler: &health.Aggregator{
50                                 Config: cfg,
51                                 Log: func(req *http.Request, err error) {
52                                         log.WithField("RemoteAddr", req.RemoteAddr).
53                                                 WithField("Path", req.URL.Path).
54                                                 WithError(err).
55                                                 Info("HTTP request")
56                                 },
57                         },
58                 },
59         }
60         if err := srv.Start(); err != nil {
61                 log.Fatal(err)
62         }
63         log.WithField("Listen", srv.Addr).Info("listening")
64         if err := srv.Wait(); err != nil {
65                 log.Fatal(err)
66         }
67 }