Merge branch '12024-zero-content-length'
[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                         MaxCollectionEntries: 1000,
49                         MaxCollectionBytes:   100000000,
50                         MaxPermissionEntries: 1000,
51                         MaxUUIDEntries:       1000,
52                 },
53         }
54 }
55
56 func init() {
57         // MakeArvadosClient returns an error if this env var isn't
58         // available as a default token (even if we explicitly set a
59         // different token before doing anything with the client). We
60         // set this dummy value during init so it doesn't clobber the
61         // one used by "run test servers".
62         if os.Getenv("ARVADOS_API_TOKEN") == "" {
63                 os.Setenv("ARVADOS_API_TOKEN", "xxx")
64         }
65 }
66
67 func main() {
68         cfg := DefaultConfig()
69
70         var configPath string
71         deprecated := " (DEPRECATED -- use config file instead)"
72         flag.StringVar(&configPath, "config", defaultConfigPath,
73                 "`path` to JSON or YAML configuration file")
74         flag.StringVar(&cfg.Listen, "listen", "",
75                 "address:port or :port to listen on"+deprecated)
76         flag.BoolVar(&cfg.deprecatedAllowAnonymous, "allow-anonymous", false,
77                 "Load an anonymous token from the ARVADOS_API_TOKEN environment variable"+deprecated)
78         flag.StringVar(&cfg.AttachmentOnlyHost, "attachment-only-host", "",
79                 "Only serve attachments at the given `host:port`"+deprecated)
80         flag.BoolVar(&cfg.TrustAllContent, "trust-all-content", false,
81                 "Serve non-public content from a single origin. Dangerous: read docs before using!"+deprecated)
82         flag.StringVar(&cfg.ManagementToken, "management-token", "",
83                 "Authorization token to be included in all health check requests.")
84
85         dumpConfig := flag.Bool("dump-config", false,
86                 "write current configuration to stdout and exit")
87         flag.Usage = usage
88         flag.Parse()
89
90         if err := config.LoadFile(cfg, configPath); err != nil {
91                 if h := os.Getenv("ARVADOS_API_HOST"); h != "" && configPath == defaultConfigPath {
92                         log.Printf("DEPRECATED: Using ARVADOS_API_HOST environment variable. Use config file instead.")
93                         cfg.Client.APIHost = h
94                 } else {
95                         log.Fatal(err)
96                 }
97         }
98         if cfg.deprecatedAllowAnonymous {
99                 log.Printf("DEPRECATED: Using -allow-anonymous command line flag with ARVADOS_API_TOKEN environment variable. Use config file instead.")
100                 cfg.AnonymousTokens = []string{os.Getenv("ARVADOS_API_TOKEN")}
101         }
102
103         if *dumpConfig {
104                 log.Fatal(config.DumpAndExit(cfg))
105         }
106
107         os.Setenv("ARVADOS_API_HOST", cfg.Client.APIHost)
108         srv := &server{Config: cfg}
109         if err := srv.Start(); err != nil {
110                 log.Fatal(err)
111         }
112         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
113                 log.Printf("Error notifying init daemon: %v", err)
114         }
115         log.Println("Listening at", srv.Addr)
116         if err := srv.Wait(); err != nil {
117                 log.Fatal(err)
118         }
119 }