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