Add option to specify config file
This commit is contained in:
parent
bd721cdb07
commit
60329fc832
@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -53,7 +55,7 @@ func Load() Config {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
log.Warnf("No config file found in search paths, using default values")
|
||||
} else {
|
||||
log.Fatalf("Error reading config: %+v", err)
|
||||
log.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,6 +68,30 @@ func Load() Config {
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadFile(configFile string) Config {
|
||||
var conf Config
|
||||
|
||||
f, err := os.OpenFile(configFile, os.O_RDONLY, 0444)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open config file: %s", err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if err := viper.ReadConfig(f); err != nil {
|
||||
log.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&conf); err != nil {
|
||||
log.Fatalf("Error parsing config: %s", err)
|
||||
}
|
||||
|
||||
loadedConfig = &conf
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadedConfig() *Config {
|
||||
if loadedConfig == nil {
|
||||
Load()
|
||||
|
18
main.go
18
main.go
@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/librespeed/speedtest/config"
|
||||
"github.com/librespeed/speedtest/database"
|
||||
"github.com/librespeed/speedtest/results"
|
||||
@ -9,9 +11,21 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
conf := config.Load()
|
||||
var (
|
||||
optConfig = flag.String("c", "", "config file to be used, defaults to settings.toml in the same directory")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var conf config.Config
|
||||
if *optConfig != "" {
|
||||
conf = config.LoadFile(*optConfig)
|
||||
} else {
|
||||
conf = config.Load()
|
||||
}
|
||||
|
||||
web.SetServerLocation(&conf)
|
||||
results.Initialize(&conf)
|
||||
database.SetDBInfo(&conf)
|
||||
log.Fatal(web.ListenAndServe(&conf))
|
||||
|
@ -17,11 +17,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// get server location from ipinfo.io from start to minimize API access
|
||||
serverLat, serverLng = getServerLocation()
|
||||
// for testing
|
||||
// serverLat, serverLng = 22.7702, 112.9578
|
||||
// serverLat, serverLng = 23.018, 113.7487
|
||||
serverLat, serverLng float64
|
||||
)
|
||||
|
||||
func getRandomData(length int) []byte {
|
||||
@ -71,9 +67,7 @@ func getIPInfo(addr string) results.IPInfoResponse {
|
||||
return ret
|
||||
}
|
||||
|
||||
func getServerLocation() (float64, float64) {
|
||||
conf := config.LoadedConfig()
|
||||
|
||||
func SetServerLocation(conf *config.Config) (float64, float64) {
|
||||
if conf.ServerLat > 0 && conf.ServerLng > 0 {
|
||||
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
|
||||
return conf.ServerLat, conf.ServerLng
|
||||
|
Loading…
Reference in New Issue
Block a user