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