diff --git a/README.md b/README.md index c3f02e9..9851e2c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,9 @@ manually, you can install newer version of Go into your GOPATH: server_lng=0 # ipinfo.io API key, if applicable ipinfo_api_key="" + + # assets directory path, defaults to `assets` in the same directory + assets_path="./assets" # password for logging into statistics page, change this to enable stats page statistics_password="PASSWORD" diff --git a/config/config.go b/config/config.go index babcbb7..e22579f 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,8 @@ type Config struct { StatsPassword string `mapstructure:"statistics_password"` RedactIP bool `mapstructure:"redact_ip_addresses"` + AssetsPath string `mapstructure:"assets_path"` + DatabaseType string `mapstructure:"database_type"` DatabaseHostname string `mapstructure:"database_hostname"` DatabaseName string `mapstructure:"database_name"` @@ -35,6 +37,7 @@ func init() { viper.SetDefault("enable_cors", false) viper.SetDefault("statistics_password", "PASSWORD") viper.SetDefault("redact_ip_addresses", false) + viper.SetDefault("assets_path", "./assets") viper.SetDefault("database_type", "postgresql") viper.SetDefault("database_hostname", "localhost") viper.SetDefault("database_name", "speedtest") diff --git a/main.go b/main.go index 9137af1..2d37e24 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/librespeed/speedtest/config" "github.com/librespeed/speedtest/database" + "github.com/librespeed/speedtest/results" "github.com/librespeed/speedtest/web" log "github.com/sirupsen/logrus" @@ -11,6 +12,7 @@ import ( func main() { conf := config.Load() + results.Initialize(&conf) database.SetDBInfo(&conf) log.Fatal(web.ListenAndServe(&conf)) } diff --git a/results/telemetry.go b/results/telemetry.go index 1492170..cc9f3ff 100644 --- a/results/telemetry.go +++ b/results/telemetry.go @@ -10,6 +10,7 @@ import ( "math/rand" "net" "net/http" + "path/filepath" "regexp" "strings" "time" @@ -75,10 +76,10 @@ type IPInfoResponse struct { Readme string `json:"readme"` } -func init() { +func Initialize(c *config.Config) { // changed to use Noto Sans instead of OpenSans, due to issue: // https://github.com/golang/freetype/issues/8 - if b, err := ioutil.ReadFile("assets/NotoSansDisplay-Light.ttf"); err != nil { + if b, err := ioutil.ReadFile(filepath.Join(c.AssetsPath, "NotoSansDisplay-Light.ttf")); err != nil { log.Fatalf("Error opening NotoSansDisplay-Light font: %s", err) } else { f, err := freetype.ParseFont(b) @@ -88,7 +89,7 @@ func init() { fontLight = f } - if b, err := ioutil.ReadFile("assets/NotoSansDisplay-Medium.ttf"); err != nil { + if b, err := ioutil.ReadFile(filepath.Join(c.AssetsPath, "NotoSansDisplay-Medium.ttf")); err != nil { log.Fatalf("Error opening NotoSansDisplay-Medium font: %s", err) } else { f, err := freetype.ParseFont(b) diff --git a/settings.toml b/settings.toml index 42ef444..a48dce8 100644 --- a/settings.toml +++ b/settings.toml @@ -8,6 +8,9 @@ server_lng=0 # ipinfo.io API key, if applicable ipinfo_api_key="" +# assets directory path, defaults to `assets` in the same directory +assets_path="./assets" + # password for logging into statistics page statistics_password="PASSWORD" # redact IP addresses diff --git a/web/web.go b/web/web.go index 2c1baa7..7ce20bd 100644 --- a/web/web.go +++ b/web/web.go @@ -81,9 +81,11 @@ func pages(w http.ResponseWriter, r *http.Request) { r.RequestURI = "/index.html" } + conf := config.LoadedConfig() + uri := strings.Split(r.RequestURI, "?")[0] if strings.HasSuffix(uri, ".html") || strings.HasSuffix(uri, ".js") { - http.FileServer(http.Dir("assets")).ServeHTTP(w, r) + http.FileServer(http.Dir(conf.AssetsPath)).ServeHTTP(w, r) } else { w.WriteHeader(http.StatusForbidden) }