-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (68 loc) · 1.88 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
package main
import (
"log"
"net/http"
"os"
"strings"
"sync"
"time"
)
func main() {
// Parse config.
if len(os.Args) != 2 {
log.Fatal("please specify the path to a config file, an example config is available at https://github.com/beefsack/git-mirror/blob/master/example-config.toml")
}
cfg, repos, err := parseConfig(os.Args[1])
if err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(cfg.BasePath, 0755); err != nil {
log.Fatalf("failed to create %s, %s", cfg.BasePath, err)
}
// Run background threads to keep mirrors up to date.
wg := sync.WaitGroup{}
for _, r := range repos {
wg.Add(1)
go func(r repo) {
defer wg.Done()
for {
log.Printf("updating %s", r.Name)
if err := mirror(cfg, r); err != nil {
log.Printf("error updating %s, %s", r.Name, err)
} else if r.Target != "" {
log.Printf("updated %s (pushed to %s)", r.Name, r.Target)
} else {
log.Printf("updated %s", r.Name)
}
time.Sleep(r.Interval.Duration)
}
}(r)
}
// Run HTTP server to serve mirrors.
if cfg.ServeMirror {
handler := http.FileServer(http.Dir(cfg.BasePath))
if cfg.AutoClone {
handler = handleGitClone(cfg, handler)
}
http.Handle("/", handler)
log.Printf("starting web server on %s", cfg.ListenAddr)
if err := http.ListenAndServe(cfg.ListenAddr, nil); err != nil {
log.Fatalf("failed to start server, %s", err)
}
}
wg.Wait()
}
func handleGitClone(cfg config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rpstr := r.URL.String()[1:]
if strings.Index(rpstr, ".git/info/") > -1 || strings.HasSuffix(rpstr, ".git") {
rpstr = rpstr[:strings.Index(rpstr, ".git")+4]
log.Print("Cloning: ", rpstr)
rp := repo{rpstr, "https://" + rpstr, "", duration{}}
if err := mirror(cfg, rp); err != nil {
log.Print("Clone error: ", err, rp, r)
}
}
next.ServeHTTP(w, r)
})
}