8460: Structured logging.
[arvados.git] / services / ws / main.go
1 package main
2
3 import (
4         "flag"
5         "fmt"
6         "net/http"
7         "time"
8
9         "git.curoverse.com/arvados.git/sdk/go/config"
10         log "github.com/Sirupsen/logrus"
11 )
12
13 func main() {
14         configPath := flag.String("config", "/etc/arvados/ws/ws.yml", "`path` to config file")
15         dumpConfig := flag.Bool("dump-config", false, "show current configuration and exit")
16         cfg := DefaultConfig()
17         flag.Parse()
18
19         err := config.LoadFile(&cfg, *configPath)
20         if err != nil {
21                 log.Fatal(err)
22         }
23
24         lvl, err := log.ParseLevel(cfg.LogLevel)
25         if err != nil {
26                 log.Fatal(err)
27         }
28         log.SetLevel(lvl)
29         switch cfg.LogFormat {
30         case "text":
31                 log.SetFormatter(&log.TextFormatter{
32                         FullTimestamp:   true,
33                         TimestampFormat: time.RFC3339Nano,
34                 })
35         case "json":
36                 log.SetFormatter(&log.JSONFormatter{
37                         TimestampFormat: time.RFC3339Nano,
38                 })
39         default:
40                 log.WithField("LogFormat", cfg.LogFormat).Fatal("unknown log format")
41         }
42
43         if *dumpConfig {
44                 txt, err := config.Dump(&cfg)
45                 if err != nil {
46                         log.Fatal(err)
47                 }
48                 fmt.Print(string(txt))
49                 return
50         }
51
52         eventSource := &pgEventSource{
53                 DataSource: cfg.Postgres.ConnectionString(),
54                 QueueSize:  cfg.ServerEventQueue,
55         }
56         srv := &http.Server{
57                 Addr:           cfg.Listen,
58                 ReadTimeout:    time.Minute,
59                 WriteTimeout:   time.Minute,
60                 MaxHeaderBytes: 1 << 20,
61                 Handler: &router{
62                         Config:      &cfg,
63                         eventSource: eventSource,
64                 },
65         }
66         eventSource.NewSink().Stop()
67
68         log.WithField("Listen", srv.Addr).Info("listening")
69         log.Fatal(srv.ListenAndServe())
70 }