Allow user to specify assets path in settings

Fixes #2
This commit is contained in:
Maddie Zhan 2020-06-16 15:34:29 +08:00
parent 8971ba8117
commit bd721cdb07
6 changed files with 18 additions and 4 deletions

View File

@ -96,6 +96,9 @@ manually, you can install newer version of Go into your GOPATH:
# ipinfo.io API key, if applicable # ipinfo.io API key, if applicable
ipinfo_api_key="" 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 # password for logging into statistics page, change this to enable stats page
statistics_password="PASSWORD" statistics_password="PASSWORD"
# redact IP addresses # redact IP addresses

View File

@ -15,6 +15,8 @@ type Config struct {
StatsPassword string `mapstructure:"statistics_password"` StatsPassword string `mapstructure:"statistics_password"`
RedactIP bool `mapstructure:"redact_ip_addresses"` RedactIP bool `mapstructure:"redact_ip_addresses"`
AssetsPath string `mapstructure:"assets_path"`
DatabaseType string `mapstructure:"database_type"` DatabaseType string `mapstructure:"database_type"`
DatabaseHostname string `mapstructure:"database_hostname"` DatabaseHostname string `mapstructure:"database_hostname"`
DatabaseName string `mapstructure:"database_name"` DatabaseName string `mapstructure:"database_name"`
@ -35,6 +37,7 @@ func init() {
viper.SetDefault("enable_cors", false) viper.SetDefault("enable_cors", false)
viper.SetDefault("statistics_password", "PASSWORD") viper.SetDefault("statistics_password", "PASSWORD")
viper.SetDefault("redact_ip_addresses", false) viper.SetDefault("redact_ip_addresses", false)
viper.SetDefault("assets_path", "./assets")
viper.SetDefault("database_type", "postgresql") viper.SetDefault("database_type", "postgresql")
viper.SetDefault("database_hostname", "localhost") viper.SetDefault("database_hostname", "localhost")
viper.SetDefault("database_name", "speedtest") viper.SetDefault("database_name", "speedtest")

View File

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

View File

@ -10,6 +10,7 @@ import (
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -75,10 +76,10 @@ type IPInfoResponse struct {
Readme string `json:"readme"` Readme string `json:"readme"`
} }
func init() { func Initialize(c *config.Config) {
// changed to use Noto Sans instead of OpenSans, due to issue: // changed to use Noto Sans instead of OpenSans, due to issue:
// https://github.com/golang/freetype/issues/8 // 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) log.Fatalf("Error opening NotoSansDisplay-Light font: %s", err)
} else { } else {
f, err := freetype.ParseFont(b) f, err := freetype.ParseFont(b)
@ -88,7 +89,7 @@ func init() {
fontLight = f 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) log.Fatalf("Error opening NotoSansDisplay-Medium font: %s", err)
} else { } else {
f, err := freetype.ParseFont(b) f, err := freetype.ParseFont(b)

View File

@ -8,6 +8,9 @@ server_lng=0
# ipinfo.io API key, if applicable # ipinfo.io API key, if applicable
ipinfo_api_key="" ipinfo_api_key=""
# assets directory path, defaults to `assets` in the same directory
assets_path="./assets"
# password for logging into statistics page # password for logging into statistics page
statistics_password="PASSWORD" statistics_password="PASSWORD"
# redact IP addresses # redact IP addresses

View File

@ -81,9 +81,11 @@ func pages(w http.ResponseWriter, r *http.Request) {
r.RequestURI = "/index.html" r.RequestURI = "/index.html"
} }
conf := config.LoadedConfig()
uri := strings.Split(r.RequestURI, "?")[0] uri := strings.Split(r.RequestURI, "?")[0]
if strings.HasSuffix(uri, ".html") || strings.HasSuffix(uri, ".js") { 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 { } else {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
} }