10666: Formatting fixes.
[arvados.git] / services / arv-git-httpd / 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         "encoding/json"
9         "flag"
10         "fmt"
11         "log"
12         "os"
13         "regexp"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16         "git.curoverse.com/arvados.git/sdk/go/config"
17         "github.com/coreos/go-systemd/daemon"
18 )
19
20 var version = "dev"
21
22 // Server configuration
23 type Config struct {
24         Client          arvados.Client
25         Listen          string
26         GitCommand      string
27         RepoRoot        string
28         GitoliteHome    string
29         ManagementToken string
30 }
31
32 var theConfig = defaultConfig()
33
34 func defaultConfig() *Config {
35         return &Config{
36                 Listen:     ":80",
37                 GitCommand: "/usr/bin/git",
38                 RepoRoot:   "/var/lib/arvados/git/repositories",
39         }
40 }
41
42 func main() {
43         const defaultCfgPath = "/etc/arvados/git-httpd/git-httpd.yml"
44         const deprecated = " (DEPRECATED -- use config file instead)"
45         flag.StringVar(&theConfig.Listen, "address", theConfig.Listen,
46                 "Address to listen on, \"host:port\" or \":port\"."+deprecated)
47         flag.StringVar(&theConfig.GitCommand, "git-command", theConfig.GitCommand,
48                 "Path to git or gitolite-shell executable. Each authenticated request will execute this program with a single argument, \"http-backend\"."+deprecated)
49         flag.StringVar(&theConfig.RepoRoot, "repo-root", theConfig.RepoRoot,
50                 "Path to git repositories."+deprecated)
51         flag.StringVar(&theConfig.GitoliteHome, "gitolite-home", theConfig.GitoliteHome,
52                 "Value for GITOLITE_HTTP_HOME environment variable. If not empty, GL_BYPASS_ACCESS_CHECKS=1 will also be set."+deprecated)
53
54         cfgPath := flag.String("config", defaultCfgPath, "Configuration file `path`.")
55         dumpConfig := flag.Bool("dump-config", false, "write current configuration to stdout and exit (useful for migrating from command line flags to config file)")
56         getVersion := flag.Bool("version", false, "print version information and exit.")
57
58         flag.StringVar(&theConfig.ManagementToken, "management-token", theConfig.ManagementToken,
59                 "Authorization token to be included in all health check requests.")
60
61         flag.Usage = usage
62         flag.Parse()
63
64         // Print version information if requested
65         if *getVersion {
66                 fmt.Printf("arv-git-httpd %s\n", version)
67                 return
68         }
69
70         err := config.LoadFile(theConfig, *cfgPath)
71         if err != nil {
72                 h := os.Getenv("ARVADOS_API_HOST")
73                 if h == "" || !os.IsNotExist(err) || *cfgPath != defaultCfgPath {
74                         log.Fatal(err)
75                 }
76                 log.Print("DEPRECATED: No config file found, but ARVADOS_API_HOST environment variable is set. Please use a config file instead.")
77                 theConfig.Client.APIHost = h
78                 if regexp.MustCompile("^(?i:1|yes|true)$").MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")) {
79                         theConfig.Client.Insecure = true
80                 }
81                 if j, err := json.MarshalIndent(theConfig, "", "    "); err == nil {
82                         log.Print("Current configuration:\n", string(j))
83                 }
84         }
85
86         if *dumpConfig {
87                 log.Fatal(config.DumpAndExit(theConfig))
88         }
89
90         srv := &server{}
91         if err := srv.Start(); err != nil {
92                 log.Fatal(err)
93         }
94         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
95                 log.Printf("Error notifying init daemon: %v", err)
96         }
97         log.Printf("arv-git-httpd %s started", version)
98         log.Println("Listening at", srv.Addr)
99         log.Println("Repository root", theConfig.RepoRoot)
100         if err := srv.Wait(); err != nil {
101                 log.Fatal(err)
102         }
103 }