Skip to content

Commit

Permalink
Add option to specify config file
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie committed Jun 16, 2020
1 parent bd721cd commit 60329fc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
28 changes: 27 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {

This comment was marked as resolved.

Copy link
@xiruizhao

xiruizhao Sep 17, 2020

Contributor

viper.ReadConfig does not seem to work.
Just after viper.ReadConfig, I called fmt.Println(viper.AllSettings()) and it showed default values rather than those read from configFile.

This comment has been minimized.

Copy link
@maddie

maddie Sep 17, 2020

Author Collaborator

Can' reproduce this, this is what I've changed locally:

	if err := viper.ReadConfig(f); err != nil {
		log.Fatalf("Error reading config: %s", err)
	}

	log.Infof("%+v", viper.AllSettings()) // <-- added this line

And the values are correctly changed.

This comment has been minimized.

Copy link
@xiruizhao

xiruizhao Sep 17, 2020

Contributor

Sorry, my mistake. Turns out when I decompress the release archive, the settings.toml in it replaced my custom settings.toml.

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()
Expand Down
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"flag"

"github.com/librespeed/speedtest/config"
"github.com/librespeed/speedtest/database"
"github.com/librespeed/speedtest/results"
Expand All @@ -9,9 +11,21 @@ import (
log "github.com/sirupsen/logrus"
)

var (
optConfig = flag.String("c", "", "config file to be used, defaults to settings.toml in the same directory")
)

func main() {
conf := config.Load()
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))
Expand Down
10 changes: 2 additions & 8 deletions web/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 60329fc

Please sign in to comment.