5 Commits

Author SHA1 Message Date
99c0137034 Fix redirect handler (again) to use notfound
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-30 22:16:50 -04:00
64551704a3 Use proper methods and fix redirect handler
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-30 22:08:51 -04:00
d2d8e6ecad I'm an idiot who forgot to flag.Parse()
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-30 21:44:58 -04:00
bb3cea2ce2 Add proper signal checking, support for reload (map only)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-03-30 21:37:47 -04:00
8b2d22a93b Add config path flag for non-docker running 2022-03-30 21:33:35 -04:00
2 changed files with 59 additions and 25 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) {

64
main.go
View File

@ -1,6 +1,7 @@
package main
import (
"flag"
"github.com/chi-middleware/logrus-logger"
"github.com/go-chi/chi/v5"
"github.com/oschwald/maxminddb-golang"
@ -12,8 +13,11 @@ import (
"net"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)
var (
@ -41,7 +45,13 @@ type City struct {
} `maxminddb:"location"`
}
var (
configFlag = flag.String("config", "", "configuration file path")
)
func main() {
flag.Parse()
viper.SetDefault("bind", ":8080")
viper.SetConfigName("dlrouter") // name of config file (without extension)
@ -49,7 +59,12 @@ func main() {
viper.AddConfigPath("/etc/dlrouter/") // path to look for the config file in
viper.AddConfigPath("$HOME/.dlrouter") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if *configFlag != "" {
viper.SetConfigFile(*configFlag)
}
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.WithError(err).Fatalln("Unable to load config file")
@ -99,14 +114,29 @@ func main() {
r.Use(RealIPMiddleware)
r.Use(logger.Logger("router", log.StandardLogger()))
r.HandleFunc("/status", statusHandler)
r.HandleFunc("/mirrors", mirrorsHandler)
r.HandleFunc("/reload", reloadHandler)
r.HandleFunc("/dl_map", dlMapHandler)
r.Handle("/metrics", promhttp.Handler())
r.HandleFunc("/", redirectHandler)
r.Get("/status", statusHandler)
r.Get("/mirrors", mirrorsHandler)
r.Post("/reload", reloadHandler)
r.Get("/dl_map", dlMapHandler)
r.Get("/metrics", promhttp.Handler().ServeHTTP)
http.ListenAndServe(viper.GetString("bind"), r)
r.NotFound(redirectHandler)
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(".", "_", "-", "_")
@ -169,3 +199,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
}