armbian-router/main.go

115 lines
2.7 KiB
Go
Raw Normal View History

2022-01-10 04:47:40 +00:00
package main
import (
"flag"
2022-03-31 01:18:25 +00:00
"github.com/chi-middleware/logrus-logger"
"github.com/go-chi/chi/v5"
2022-03-31 04:56:59 +00:00
lru "github.com/hashicorp/golang-lru"
2022-01-10 04:53:23 +00:00
"github.com/oschwald/maxminddb-golang"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
2022-01-10 04:53:23 +00:00
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net/http"
"os"
"os/signal"
"syscall"
2022-01-10 04:47:40 +00:00
)
var (
2022-01-10 04:53:23 +00:00
db *maxminddb.Reader
servers ServerList
dlMap map[string]string
redirectsServed = promauto.NewCounter(prometheus.CounterOpts{
Name: "armbian_router_redirects",
Help: "The total number of processed redirects",
})
downloadsMapped = promauto.NewCounter(prometheus.CounterOpts{
Name: "armbian_router_download_maps",
Help: "The total number of mapped download paths",
})
2022-03-31 04:56:59 +00:00
serverCache *lru.Cache
topChoices int
2022-01-10 04:47:40 +00:00
)
2022-01-10 04:53:23 +00:00
// City represents a MaxmindDB city
2022-01-10 04:47:40 +00:00
type City struct {
2022-01-10 04:53:23 +00:00
Location struct {
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
} `maxminddb:"location"`
2022-01-10 04:47:40 +00:00
}
2022-03-31 04:56:59 +00:00
type ServerConfig struct {
Server string `mapstructure:"server" yaml:"server"`
Latitude float64 `mapstructure:"latitude" yaml:"latitude"`
Longitude float64 `mapstructure:"longitude" yaml:"longitude"`
Weight int `mapstructure:"weight" yaml:"weight"`
}
var (
configFlag = flag.String("config", "", "configuration file path")
)
2022-01-10 04:47:40 +00:00
func main() {
flag.Parse()
viper.SetDefault("bind", ":8080")
2022-03-31 04:56:59 +00:00
viper.SetDefault("cacheSize", 1024)
viper.SetDefault("topChoices", 3)
2022-01-10 04:53:23 +00:00
viper.SetConfigName("dlrouter") // name of config file (without extension)
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
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
if *configFlag != "" {
viper.SetConfigFile(*configFlag)
}
2022-03-31 04:56:59 +00:00
reloadConfig()
2022-01-10 04:53:23 +00:00
// Start check loop
go servers.checkLoop()
log.Info("Starting")
r := chi.NewRouter()
2022-01-10 04:53:23 +00:00
r.Use(RealIPMiddleware)
2022-03-31 01:18:25 +00:00
r.Use(logger.Logger("router", log.StandardLogger()))
2022-01-10 04:53:23 +00:00
r.Get("/status", statusHandler)
r.Get("/mirrors", mirrorsHandler)
r.Post("/reload", reloadHandler)
r.Get("/dl_map", dlMapHandler)
r.Get("/metrics", promhttp.Handler().ServeHTTP)
r.NotFound(redirectHandler)
go http.ListenAndServe(viper.GetString("bind"), r)
log.Info("Ready")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGHUP)
for {
sig := <-c
if sig != syscall.SIGHUP {
break
}
2022-03-31 04:56:59 +00:00
reloadConfig()
}
2022-01-10 04:47:40 +00:00
}