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