-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
130 lines (120 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2013 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"net/http"
"os"
"flag"
"repo_server/opgp"
)
var (
repoPath = "repos"
filesPath = "files"
tmpPath = "tmp"
)
var cwd = flag.String("dir", ".", "Change to this directory before doing anything.")
var cfg *Config
var names = make(chan string)
var manageOnly = false
func loadConfig() string {
cfgPath := "config.yml"
if flag.NArg() > 0 {
cfgPath = flag.Arg(0)
}
cfg = LoadConfig(cfgPath)
listen, err := cfg.Get("listen", ":8080")
if err != nil {
log.Printf("Error loading config 'listen': %s\n", err)
os.Exit(1)
}
manageOnly, err = cfg.GetBool("manage-only", manageOnly)
if err != nil {
log.Printf("Error loading config 'listen': %s\n", err)
os.Exit(1)
}
opgp.KeyringFile, err = cfg.Get("keyring", opgp.KeyringFile)
if err != nil {
log.Printf("Error loading config 'keyring': %s\n", err)
os.Exit(1)
}
cwd, err := cfg.Get("path.cwd", ".")
if err != nil {
log.Printf("Error loading config 'path.cwd': %s\n", err)
os.Exit(1)
}
err = os.Chdir(cwd)
if err != nil {
log.Printf("Failed to set cwd to '%s': %s\n", cwd, err)
os.Exit(1)
}
repoPath, err = cfg.Get("path.repos", repoPath)
if err != nil {
log.Printf("Error loading config 'path.repos': %s\n", err)
os.Exit(1)
}
filesPath, err = cfg.Get("path.files", filesPath)
if err != nil {
log.Printf("Error loading config 'path.files': %s\n", err)
os.Exit(1)
}
tmpPath, err = cfg.Get("path.tmp", tmpPath)
if err != nil {
log.Printf("Error loading config 'path.tmp': %s\n", err)
os.Exit(1)
}
return listen
}
func prepPaths() {
for _, dir := range []string{repoPath, filesPath, tmpPath} {
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Printf("Failed to create '%s': %s\n", dir, err)
os.Exit(1)
}
}
}
func prepRepos() {
repos, err := cfg.GetMapList("repos")
if err != nil {
log.Printf("Failed to read shared repo config: %s\n", err)
os.Exit(1)
}
for i, entry := range repos {
name, ok := entry["name"]
if !ok {
log.Printf("Repo entry %d, missing name!\n", i+1)
os.Exit(1)
}
_, ok = entry["codename"]
if !ok {
log.Printf("Repo entry %d, missing codename!\n", i+1)
os.Exit(1)
}
err = UpdateSharedRepo(name, entry)
if err != nil {
log.Printf("Failed to update Repo %s: %s\n", name, err)
os.Exit(1)
}
}
}
func main() {
flag.Parse()
err := os.Chdir(*cwd)
if err != nil {
log.Printf("Failed to set cwd to '%s': %s\n", *cwd, err)
os.Exit(1)
}
listen := loadConfig()
prepPaths()
prepRepos()
go randNameGen(names)
if !manageOnly {
http.Handle("/", http.FileServer(http.Dir(filesPath)))
http.Handle("/r/", http.StripPrefix("/r/", http.FileServer(http.Dir(repoPath))))
}
http.HandleFunc("/c/", handleControlRequest)
log.Printf("-- start web server --\n")
log.Fatal(http.ListenAndServe(listen, nil))
}