Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
d2d8e6ecad | |||
bb3cea2ce2 | |||
8b2d22a93b | |||
c9593a5a0d |
@ -49,6 +49,8 @@ steps:
|
|||||||
repo: registry.meow.tf/tyler/armbian-router
|
repo: registry.meow.tf/tyler/armbian-router
|
||||||
registry: registry.meow.tf
|
registry: registry.meow.tf
|
||||||
depends_on: [ clone ]
|
depends_on: [ clone ]
|
||||||
|
when:
|
||||||
|
event: tag
|
||||||
volumes:
|
volumes:
|
||||||
- name: build
|
- name: build
|
||||||
temp: {}
|
temp: {}
|
20
http.go
20
http.go
@ -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) {
|
||||||
|
51
main.go
51
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"github.com/chi-middleware/logrus-logger"
|
"github.com/chi-middleware/logrus-logger"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/oschwald/maxminddb-golang"
|
"github.com/oschwald/maxminddb-golang"
|
||||||
@ -12,8 +13,11 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -41,7 +45,13 @@ type City struct {
|
|||||||
} `maxminddb:"location"`
|
} `maxminddb:"location"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
configFlag = flag.String("config", "", "configuration file path")
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
viper.SetDefault("bind", ":8080")
|
viper.SetDefault("bind", ":8080")
|
||||||
|
|
||||||
viper.SetConfigName("dlrouter") // name of config file (without extension)
|
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("/etc/dlrouter/") // path to look for the config file in
|
||||||
viper.AddConfigPath("$HOME/.dlrouter") // call multiple times to add many search paths
|
viper.AddConfigPath("$HOME/.dlrouter") // call multiple times to add many search paths
|
||||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
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
|
if err != nil { // Handle errors reading the config file
|
||||||
log.WithError(err).Fatalln("Unable to load config file")
|
log.WithError(err).Fatalln("Unable to load config file")
|
||||||
@ -106,7 +121,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(".", "_", "-", "_")
|
||||||
@ -169,3 +198,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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user