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