Merge branch '10346-rearrange-api-docs' closes #10346
[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         flag.Usage = usage
66         flag.Parse()
67
68         if err := config.LoadFile(cfg, configPath); err != nil {
69                 if h := os.Getenv("ARVADOS_API_HOST"); h != "" && configPath == defaultConfigPath {
70                         log.Printf("DEPRECATED: Using ARVADOS_API_HOST environment variable. Use config file instead.")
71                         cfg.Client.APIHost = h
72                 } else {
73                         log.Fatal(err)
74                 }
75         }
76         if cfg.deprecatedAllowAnonymous {
77                 log.Printf("DEPRECATED: Using -allow-anonymous command line flag with ARVADOS_API_TOKEN environment variable. Use config file instead.")
78                 cfg.AnonymousTokens = []string{os.Getenv("ARVADOS_API_TOKEN")}
79         }
80
81         os.Setenv("ARVADOS_API_HOST", cfg.Client.APIHost)
82         srv := &server{Config: cfg}
83         if err := srv.Start(); err != nil {
84                 log.Fatal(err)
85         }
86         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
87                 log.Printf("Error notifying init daemon: %v", err)
88         }
89         log.Println("Listening at", srv.Addr)
90         if err := srv.Wait(); err != nil {
91                 log.Fatal(err)
92         }
93 }