Merge branch '10346-rearrange-api-docs' closes #10346
[arvados.git] / services / arv-git-httpd / main.go
1 package main
2
3 import (
4         "encoding/json"
5         "flag"
6         "log"
7         "os"
8         "regexp"
9
10         "git.curoverse.com/arvados.git/sdk/go/arvados"
11         "git.curoverse.com/arvados.git/sdk/go/config"
12         "github.com/coreos/go-systemd/daemon"
13 )
14
15 // Server configuration
16 type Config struct {
17         Client       arvados.Client
18         Listen       string
19         GitCommand   string
20         RepoRoot     string
21         GitoliteHome string
22 }
23
24 var theConfig = defaultConfig()
25
26 func defaultConfig() *Config {
27         return &Config{
28                 Listen:     ":80",
29                 GitCommand: "/usr/bin/git",
30                 RepoRoot:   "/var/lib/arvados/git/repositories",
31         }
32 }
33
34 func main() {
35         const defaultCfgPath = "/etc/arvados/git-httpd/git-httpd.yml"
36         const deprecated = " (DEPRECATED -- use config file instead)"
37         flag.StringVar(&theConfig.Listen, "address", theConfig.Listen,
38                 "Address to listen on, \"host:port\" or \":port\"."+deprecated)
39         flag.StringVar(&theConfig.GitCommand, "git-command", theConfig.GitCommand,
40                 "Path to git or gitolite-shell executable. Each authenticated request will execute this program with a single argument, \"http-backend\"."+deprecated)
41         flag.StringVar(&theConfig.RepoRoot, "repo-root", theConfig.RepoRoot,
42                 "Path to git repositories."+deprecated)
43         flag.StringVar(&theConfig.GitoliteHome, "gitolite-home", theConfig.GitoliteHome,
44                 "Value for GITOLITE_HTTP_HOME environment variable. If not empty, GL_BYPASS_ACCESS_CHECKS=1 will also be set."+deprecated)
45
46         cfgPath := flag.String("config", defaultCfgPath, "Configuration file `path`.")
47         flag.Usage = usage
48         flag.Parse()
49
50         err := config.LoadFile(theConfig, *cfgPath)
51         if err != nil {
52                 h := os.Getenv("ARVADOS_API_HOST")
53                 if h == "" || !os.IsNotExist(err) || *cfgPath != defaultCfgPath {
54                         log.Fatal(err)
55                 }
56                 log.Print("DEPRECATED: No config file found, but ARVADOS_API_HOST environment variable is set. Please use a config file instead.")
57                 theConfig.Client.APIHost = h
58                 if regexp.MustCompile("^(?i:1|yes|true)$").MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")) {
59                         theConfig.Client.Insecure = true
60                 }
61                 if j, err := json.MarshalIndent(theConfig, "", "    "); err == nil {
62                         log.Print("Current configuration:\n", string(j))
63                 }
64         }
65
66         srv := &server{}
67         if err := srv.Start(); err != nil {
68                 log.Fatal(err)
69         }
70         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
71                 log.Printf("Error notifying init daemon: %v", err)
72         }
73         log.Println("Listening at", srv.Addr)
74         log.Println("Repository root", theConfig.RepoRoot)
75         if err := srv.Wait(); err != nil {
76                 log.Fatal(err)
77         }
78 }