forked from antmak/git-mirror
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (119 loc) · 3.01 KB
/
main.go
File metadata and controls
135 lines (119 loc) · 3.01 KB
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
131
132
133
134
135
package main
import (
"bufio"
"context"
"log"
"net/http"
"net/http/cgi"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
func main() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGCHLD)
go func() {
for range sigChan {
for {
var status syscall.WaitStatus
pid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, nil)
if err != nil || pid <= 0 {
break
}
}
}
}()
if len(os.Args) != 2 {
log.Fatal("please specify the path to a config file, an example config is available at https://github.com/espressif/git-mirror-server/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)
}
healthCheck(cfg, repos)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, syscall.SIGTERM, syscall.SIGINT)
var wg sync.WaitGroup
for _, r := range repos {
wg.Add(1)
go func(r repo) {
defer wg.Done()
timer := time.NewTimer(0)
defer timer.Stop()
// Drain the initial fire — first iteration runs immediately via the loop body
if !timer.Stop() {
<-timer.C
}
for {
log.Printf("updating %s", r.Name)
out, err := mirror(ctx, cfg, r)
scanner := bufio.NewScanner(strings.NewReader(out))
for scanner.Scan() {
log.Printf("%s: %s", r.Name, scanner.Text())
}
if err != nil {
log.Printf("error updating %s, %s", r.Name, err)
} else {
log.Printf("updated %s", r.Name)
}
timer.Reset(r.Interval.Duration)
select {
case <-ctx.Done():
log.Printf("stopping updates for %s", r.Name)
return
case <-timer.C:
}
}
}(r)
}
gitBackend := &cgi.Handler{
Path: "/usr/bin/git",
Args: []string{"http-backend"},
Dir: cfg.BasePath,
Env: []string{
"GIT_PROJECT_ROOT=" + cfg.BasePath,
"GIT_HTTP_EXPORT_ALL=true",
},
}
semaphore := make(chan struct{}, cfg.MaxConcurrentConnections)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
select {
case semaphore <- struct{}{}:
case <-r.Context().Done():
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
return
}
defer func() { <-semaphore }()
gitBackend.ServeHTTP(w, r)
})
srv := &http.Server{
Addr: cfg.ListenAddr,
Handler: mux,
}
go func() {
sig := <-shutdownChan
log.Printf("received %s, shutting down", sig)
cancel()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP server shutdown error: %s, forcing close", err)
srv.Close()
}
}()
log.Printf("starting git HTTP server on %s", cfg.ListenAddr)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("failed to start server, %s", err)
}
wg.Wait()
log.Printf("shutdown complete")
}