summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRagnis Armus <ragnis@armus.ee>2018-07-15 16:37:10 +0300
committerRagnis Armus <ragnis@armus.ee>2018-07-15 16:37:10 +0300
commit7639b159eea881a66334fec0e1d8db250932b31b (patch)
treefac2b0a39275341ba10780c2f5b670a7ccebaab1
parentc2c92c691f2d0269395e63bdc111d4d7d059f5d6 (diff)
implement config reload via SIGUSR1
-rw-r--r--main.go41
1 files changed, 35 insertions, 6 deletions
diff --git a/main.go b/main.go
index 6933936..7641e7c 100644
--- a/main.go
+++ b/main.go
@@ -6,7 +6,10 @@ import (
"log"
"net"
"net/http"
+ "os"
+ "os/signal"
"strings"
+ "syscall"
"radr.ee/cgit-go-import/cgitrc"
)
@@ -18,7 +21,7 @@ type importRepo struct {
var (
importPrefix string
- handlers = make(map[string]http.Handler)
+ handlers map[string]http.Handler
)
func serveStatus(w http.ResponseWriter, status int) {
@@ -69,6 +72,30 @@ func findHandler(path string) (http.Handler, bool) {
return nil, false
}
+func updateHandlers(file string) error {
+ cfg, err := cgitrc.Open(file)
+ if err != nil {
+ return err
+ }
+ h := make(map[string]http.Handler, len(cfg.Repos))
+ for _, r := range cfg.Repos {
+ h["/"+r.URL] = createRepoHandler(cfg, r)
+ }
+ handlers = h
+ return nil
+}
+
+func watchSignal(sig os.Signal, fn func()) {
+ ch := make(chan os.Signal, 1)
+ signal.Notify(ch, sig)
+ go func() {
+ for {
+ <-ch
+ fn()
+ }
+ }()
+}
+
func listen(addr string) (net.Listener, error) {
proto := "tcp"
if s := strings.TrimPrefix(addr, "unix:"); addr != s {
@@ -93,13 +120,15 @@ func main() {
importPrefix += "/"
}
- cfg, err := cgitrc.Open(configFile)
- if err != nil {
+ if err := updateHandlers(configFile); err != nil {
log.Fatal(err)
}
- for _, r := range cfg.Repos {
- handlers["/"+r.URL] = createRepoHandler(cfg, r)
- }
+
+ watchSignal(syscall.SIGUSR1, func() {
+ if err := updateHandlers(configFile); err != nil {
+ log.Printf("reloading config: %v", err)
+ }
+ })
l, err := listen(bindAddr)
if err != nil {