10690: De-duplicate dumpConfig.
[arvados.git] / services / keep-web / main.go
1 package main
2
3 import (
4         "flag"
5         "log"
6         "os"
7
8         "git.curoverse.com/arvados.git/sdk/go/arvados"
9         "git.curoverse.com/arvados.git/sdk/go/config"
10         "github.com/coreos/go-systemd/daemon"
11 )
12
13 var (
14         defaultConfigPath = "/etc/arvados/keep-web/keep-web.yml"
15 )
16
17 // Config specifies server configuration.
18 type Config struct {
19         Client arvados.Client
20
21         Listen string
22
23         AnonymousTokens    []string
24         AttachmentOnlyHost string
25         TrustAllContent    bool
26
27         // Hack to support old command line flag, which is a bool
28         // meaning "get actual token from environment".
29         deprecatedAllowAnonymous bool
30 }
31
32 // DefaultConfig returns the default configuration.
33 func DefaultConfig() *Config {
34         return &Config{
35                 Listen: ":80",
36         }
37 }
38
39 func init() {
40         // MakeArvadosClient returns an error if this env var isn't
41         // available as a default token (even if we explicitly set a
42         // different token before doing anything with the client). We
43         // set this dummy value during init so it doesn't clobber the
44         // one used by "run test servers".
45         if os.Getenv("ARVADOS_API_TOKEN") == "" {
46                 os.Setenv("ARVADOS_API_TOKEN", "xxx")
47         }
48 }
49
50 func main() {
51         cfg := DefaultConfig()
52
53         var configPath string
54         deprecated := " (DEPRECATED -- use config file instead)"
55         flag.StringVar(&configPath, "config", defaultConfigPath,
56                 "`path` to JSON or YAML configuration file")
57         flag.StringVar(&cfg.Listen, "listen", "",
58                 "address:port or :port to listen on"+deprecated)
59         flag.BoolVar(&cfg.deprecatedAllowAnonymous, "allow-anonymous", false,
60                 "Load an anonymous token from the ARVADOS_API_TOKEN environment variable"+deprecated)
61         flag.StringVar(&cfg.AttachmentOnlyHost, "attachment-only-host", "",
62                 "Only serve attachments at the given `host:port`"+deprecated)
63         flag.BoolVar(&cfg.TrustAllContent, "trust-all-content", false,
64                 "Serve non-public content from a single origin. Dangerous: read docs before using!"+deprecated)
65         dumpConfig := flag.Bool("dump-config", false,
66                 "write current configuration to stdout and exit")
67         flag.Usage = usage
68         flag.Parse()
69
70         if err := config.LoadFile(cfg, configPath); err != nil {
71                 if h := os.Getenv("ARVADOS_API_HOST"); h != "" && configPath == defaultConfigPath {
72                         log.Printf("DEPRECATED: Using ARVADOS_API_HOST environment variable. Use config file instead.")
73                         cfg.Client.APIHost = h
74                 } else {
75                         log.Fatal(err)
76                 }
77         }
78         if cfg.deprecatedAllowAnonymous {
79                 log.Printf("DEPRECATED: Using -allow-anonymous command line flag with ARVADOS_API_TOKEN environment variable. Use config file instead.")
80                 cfg.AnonymousTokens = []string{os.Getenv("ARVADOS_API_TOKEN")}
81         }
82
83         if *dumpConfig {
84                 log.Fatal(config.DumpAndExit(cfg))
85         }
86
87         os.Setenv("ARVADOS_API_HOST", cfg.Client.APIHost)
88         srv := &server{Config: cfg}
89         if err := srv.Start(); err != nil {
90                 log.Fatal(err)
91         }
92         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
93                 log.Printf("Error notifying init daemon: %v", err)
94         }
95         log.Println("Listening at", srv.Addr)
96         if err := srv.Wait(); err != nil {
97                 log.Fatal(err)
98         }
99 }