10234: Change default repo root from CWD to recommended path.
[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 }
22
23 var theConfig = defaultConfig()
24
25 func defaultConfig() *Config {
26         return &Config{
27                 Listen:     ":80",
28                 GitCommand: "/usr/bin/git",
29                 RepoRoot:   "/var/lib/arvados/git/repositories",
30         }
31 }
32
33 func init() {
34         const defaultCfgPath = "/etc/arvados/git-httpd/git-httpd.yml"
35         const deprecated = " (DEPRECATED -- use config file instead)"
36         flag.StringVar(&theConfig.Listen, "address", theConfig.Listen,
37                 "Address to listen on, \"host:port\" or \":port\"."+deprecated)
38         flag.StringVar(&theConfig.GitCommand, "git-command", theConfig.GitCommand,
39                 "Path to git or gitolite-shell executable. Each authenticated request will execute this program with a single argument, \"http-backend\"."+deprecated)
40         flag.StringVar(&theConfig.RepoRoot, "repo-root", theConfig.RepoRoot,
41                 "Path to git repositories."+deprecated)
42
43         cfgPath := flag.String("config", defaultCfgPath, "Configuration file `path`.")
44         flag.Usage = usage
45         flag.Parse()
46
47         err := config.LoadFile(theConfig, *cfgPath)
48         if err != nil {
49                 h := os.Getenv("ARVADOS_API_HOST")
50                 if h == "" || !os.IsNotExist(err) || *cfgPath != defaultCfgPath {
51                         log.Fatal(err)
52                 }
53                 log.Print("DEPRECATED: No config file found, but ARVADOS_API_HOST environment variable is set. Please use a config file instead.")
54                 theConfig.Client.APIHost = h
55                 if regexp.MustCompile("^(?i:1|yes|true)$").MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")) {
56                         theConfig.Client.Insecure = true
57                 }
58                 if j, err := json.MarshalIndent(theConfig, "", "    "); err == nil {
59                         log.Print("Current configuration:\n", string(j))
60                 }
61         }
62 }
63
64 func main() {
65         srv := &server{}
66         if err := srv.Start(); err != nil {
67                 log.Fatal(err)
68         }
69         if _, err := daemon.SdNotify("READY=1"); err != nil {
70                 log.Printf("Error notifying init daemon: %v", err)
71         }
72         log.Println("Listening at", srv.Addr)
73         log.Println("Repository root", theConfig.RepoRoot)
74         if err := srv.Wait(); err != nil {
75                 log.Fatal(err)
76         }
77 }