Merge branch '11070-ws-listener-problem'
[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         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
11         "github.com/coreos/go-systemd/daemon"
12 )
13
14 var logger = ctxlog.FromContext
15
16 func main() {
17         log := logger(nil)
18
19         configPath := flag.String("config", "/etc/arvados/ws/ws.yml", "`path` to config file")
20         dumpConfig := flag.Bool("dump-config", false, "show current configuration and exit")
21         cfg := defaultConfig()
22         flag.Parse()
23
24         err := config.LoadFile(&cfg, *configPath)
25         if err != nil {
26                 log.Fatal(err)
27         }
28
29         ctxlog.SetLevel(cfg.LogLevel)
30         ctxlog.SetFormat(cfg.LogFormat)
31
32         if *dumpConfig {
33                 txt, err := config.Dump(&cfg)
34                 if err != nil {
35                         log.Fatal(err)
36                 }
37                 fmt.Print(string(txt))
38                 return
39         }
40
41         log.Info("started")
42         eventSource := &pgEventSource{
43                 DataSource: cfg.Postgres.ConnectionString(),
44                 QueueSize:  cfg.ServerEventQueue,
45         }
46         srv := &http.Server{
47                 Addr:           cfg.Listen,
48                 ReadTimeout:    time.Minute,
49                 WriteTimeout:   time.Minute,
50                 MaxHeaderBytes: 1 << 20,
51                 Handler: &router{
52                         Config:         &cfg,
53                         eventSource:    eventSource,
54                         newPermChecker: func() permChecker { return newPermChecker(cfg.Client) },
55                 },
56         }
57
58         go func() {
59                 eventSource.Run()
60                 log.Fatal("event source stopped")
61         }()
62
63         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
64                 log.WithError(err).Warn("error notifying init daemon")
65         }
66
67         log.WithField("Listen", srv.Addr).Info("listening")
68         log.Fatal(srv.ListenAndServe())
69 }