Add proper signal checking, support for reload (map only)
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
Tyler 2022-03-30 21:37:47 -04:00
parent 8b2d22a93b
commit bb3cea2ce2
2 changed files with 39 additions and 18 deletions

20
http.go
View File

@ -3,8 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -74,22 +72,10 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
} }
func reloadHandler(w http.ResponseWriter, r *http.Request) { func reloadHandler(w http.ResponseWriter, r *http.Request) {
if mapFile := viper.GetString("dl_map"); mapFile != "" { reloadMap()
log.WithField("file", mapFile).Info("Loading download map")
newMap, err := loadMap(mapFile) w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dlMap = newMap
return
}
w.WriteHeader(http.StatusNotFound)
} }
func dlMapHandler(w http.ResponseWriter, r *http.Request) { func dlMapHandler(w http.ResponseWriter, r *http.Request) {

37
main.go
View File

@ -13,8 +13,11 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"os/signal"
"strings" "strings"
"sync" "sync"
"syscall"
) )
var ( var (
@ -116,7 +119,21 @@ func main() {
r.Handle("/metrics", promhttp.Handler()) r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", redirectHandler) r.HandleFunc("/", redirectHandler)
http.ListenAndServe(viper.GetString("bind"), r) go http.ListenAndServe(viper.GetString("bind"), r)
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP)
for {
sig := <-c
if sig != syscall.SIGHUP {
break
}
reloadMap()
}
} }
var metricReplacer = strings.NewReplacer(".", "_", "-", "_") var metricReplacer = strings.NewReplacer(".", "_", "-", "_")
@ -179,3 +196,21 @@ func addServer(server string) {
"longitude": city.Location.Longitude, "longitude": city.Location.Longitude,
}).Info("Added server") }).Info("Added server")
} }
func reloadMap() {
mapFile := viper.GetString("dl_map")
if mapFile == "" {
return
}
log.WithField("file", mapFile).Info("Loading download map")
newMap, err := loadMap(mapFile)
if err != nil {
return
}
dlMap = newMap
}