Add option to specify config file

This commit is contained in:
Maddie Zhan 2020-06-16 16:10:58 +08:00
parent bd721cdb07
commit 60329fc832
3 changed files with 45 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package config package config
import ( import (
"os"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -53,7 +55,7 @@ func Load() Config {
if _, ok := err.(viper.ConfigFileNotFoundError); ok { if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Warnf("No config file found in search paths, using default values") log.Warnf("No config file found in search paths, using default values")
} else { } else {
log.Fatalf("Error reading config: %+v", err) log.Fatalf("Error reading config: %s", err)
} }
} }
@ -66,6 +68,30 @@ func Load() Config {
return conf 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 { func LoadedConfig() *Config {
if loadedConfig == nil { if loadedConfig == nil {
Load() Load()

18
main.go
View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"flag"
"github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database" "github.com/librespeed/speedtest/database"
"github.com/librespeed/speedtest/results" "github.com/librespeed/speedtest/results"
@ -9,9 +11,21 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func main() { var (
conf := config.Load() 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) results.Initialize(&conf)
database.SetDBInfo(&conf) database.SetDBInfo(&conf)
log.Fatal(web.ListenAndServe(&conf)) log.Fatal(web.ListenAndServe(&conf))

View File

@ -17,11 +17,7 @@ import (
) )
var ( var (
// get server location from ipinfo.io from start to minimize API access serverLat, serverLng float64
serverLat, serverLng = getServerLocation()
// for testing
// serverLat, serverLng = 22.7702, 112.9578
// serverLat, serverLng = 23.018, 113.7487
) )
func getRandomData(length int) []byte { func getRandomData(length int) []byte {
@ -71,9 +67,7 @@ func getIPInfo(addr string) results.IPInfoResponse {
return ret return ret
} }
func getServerLocation() (float64, float64) { func SetServerLocation(conf *config.Config) (float64, float64) {
conf := config.LoadedConfig()
if conf.ServerLat > 0 && conf.ServerLng > 0 { if conf.ServerLat > 0 && conf.ServerLng > 0 {
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng) log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
return conf.ServerLat, conf.ServerLng return conf.ServerLat, conf.ServerLng