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

37
main.go
View File

@ -13,8 +13,11 @@ import (
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
var (
@ -116,7 +119,21 @@ func main() {
r.Handle("/metrics", promhttp.Handler())
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(".", "_", "-", "_")
@ -179,3 +196,21 @@ func addServer(server string) {
"longitude": city.Location.Longitude,
}).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
}