From ad935e7313ffb5c9e54b842a7702bfaac1a5b182 Mon Sep 17 00:00:00 2001 From: kreativmonkey Date: Wed, 5 Jul 2017 17:17:00 +0200 Subject: [PATCH] Root Path setting --- config.go | 11 +++++++++-- config_test.go | 36 +++++++++++++++++++++++++++++++++--- main.go | 7 ++++--- shrty.go | 0 4 files changed, 46 insertions(+), 8 deletions(-) delete mode 100644 shrty.go diff --git a/config.go b/config.go index eb748cd..f0fbc98 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "flag" "github.com/caarlos0/env" "os" + "path" ) func DefaultConfig() Config { @@ -14,7 +15,7 @@ func DefaultConfig() Config { Port: "6889", Title: "Sgot", } - c.DB = c.RootPath + "shrty.go" + c.DB = c.RootPath + "shrty.db" return c } @@ -51,11 +52,17 @@ func readConfig(f *flag.FlagSet, args []string) (*Config, error){ return nil, err } + // Setting Rootpath from flag or enviorment and set the db path to new root. f.StringVar(&config.RootPath, "root", config.RootPath, "The path to the shrty root directory.") + f.StringVar(&config.DB, "db-file", config.DB, "The db file to use") + if config.DB == "./shrty.db" && config.RootPath != "./" { + config.DB = path.Join(config.RootPath, "shrty.db") + } + + // Setting up all the other flags f.StringVar(&config.Host, "host", config.Host, "The host to listen on") f.StringVar(&config.Port, "port", config.Port, "The port to listen on") f.StringVar(&config.APIkey, "apikey", config.APIkey, "The Key to connect to the API") - f.StringVar(&config.DB, "db-file", config.DB, "The db file to use") f.StringVar(&config.Domain, "domain", config.Domain, "The domain for redirect links") f.StringVar(&config.Title, "title", config.Title, "The title on the Front") diff --git a/config_test.go b/config_test.go index 4f50816..d758f3a 100644 --- a/config_test.go +++ b/config_test.go @@ -9,7 +9,7 @@ import ( func TestConfig_ReadConfigDefaults(t *testing.T){ originalArgs := os.Args - os.Args = []string{"shrt"} + os.Args = []string{"shrty"} defer func(){ os.Args = originalArgs }() d := DefaultConfig() @@ -43,6 +43,8 @@ func TestConfig_ReadConfig(t *testing.T){ } func TestConfig_ReadConfigFromEnv(t *testing.T) { + assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root")) + defer os.Unsetenv("SHRTY_ROOT_PATH") assert.NoError(t, os.Setenv("SHRTY_DB_FILE", "db")) defer os.Unsetenv("SHRTY_DB_FILE") assert.NoError(t, os.Setenv("SHRTY_API_KEY", "apikey")) @@ -55,8 +57,6 @@ func TestConfig_ReadConfigFromEnv(t *testing.T) { defer os.Unsetenv("SHRTY_DOMAIN") assert.NoError(t, os.Setenv("SHRTY_TITLE", "title")) defer os.Unsetenv("SHRTY_TITLE") - assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root")) - defer os.Unsetenv("SHRTY_ROOT_PATH") expected := &Config{ Host: "host", @@ -68,6 +68,36 @@ func TestConfig_ReadConfigFromEnv(t *testing.T) { RootPath: "root", } + cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{}) + assert.NoError(t, err) + assert.Equal(t, expected, cfg) +} + +func TestConfig_ReadConfigWithRootpath(t *testing.T) { + assert.NoError(t, os.Setenv("SHRTY_ROOT_PATH", "root")) + defer os.Unsetenv("SHRTY_ROOT_PATH") + assert.NoError(t, os.Setenv("SHRTY_API_KEY", "apikey")) + defer os.Unsetenv("SHRTY_API_KEY") + assert.NoError(t, os.Setenv("SHRTY_HOST", "host")) + defer os.Unsetenv("SHRTY_HOST") + assert.NoError(t, os.Setenv("SHRTY_PORT", "port")) + defer os.Unsetenv("SHRTY_PORT") + assert.NoError(t, os.Setenv("SHRTY_DOMAIN", "domain")) + defer os.Unsetenv("SHRTY_DOMAIN") + assert.NoError(t, os.Setenv("SHRTY_TITLE", "title")) + defer os.Unsetenv("SHRTY_TITLE") + + + expected := &Config{ + RootPath:"root", + Host: "host", + Port: "port", + Title: "title", + APIkey: "apikey", + Domain: "domain", + DB: "root/shrty.db", + } + cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{}) assert.NoError(t, err) assert.Equal(t, expected, cfg) diff --git a/main.go b/main.go index 265a1fc..2c2cf22 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,10 @@ package main import ( "net/http" - shrty "github.com/kreativmonkey/shrt/shrty" + "github.com/kreativmonkey/shrt/shrty" "log" "fmt" + "path" ) const version = "0.06" @@ -35,9 +36,9 @@ type shrt struct { func main() { router := NewRouter() - s := http.StripPrefix("/css/", http.FileServer(http.Dir("./template/css/"))) + s := http.StripPrefix("/css/", http.FileServer(http.Dir(path.Join(config.RootPath, "/template/css/")))) router.PathPrefix("/css/").Handler(s) - s = http.StripPrefix("/img/", http.FileServer(http.Dir("./template/img/"))) + s = http.StripPrefix("/img/", http.FileServer(http.Dir(path.Join(config.RootPath, "/template/img/")))) router.PathPrefix("/img/").Handler(s) /* diff --git a/shrty.go b/shrty.go deleted file mode 100644 index e69de29..0000000