Compare commits

..

No commits in common. 'ad935e7313ffb5c9e54b842a7702bfaac1a5b182' and '8cfebbeebffef85c3c47e8932c4e7e373b26cab2' have entirely different histories.

@ -4,30 +4,26 @@ import (
"flag"
"github.com/caarlos0/env"
"os"
"path"
)
func DefaultConfig() Config {
c := Config{
RootPath: "./",
return Config{
DB: "./sgot.db",
APIkey: "thisIsNotASecretTokenNow",
Host: "localhost",
Port: "6889",
Title: "Sgot",
}
c.DB = c.RootPath + "shrty.db"
return c
}
type Config struct {
RootPath string `env:"SHRTY_ROOT_PATH"`
DB string `env:"SHRTY_DB_FILE"`
APIkey string `env:"SHRTY_API_KEY"`
Host string `env:"SHRTY_HOST"`
Port string `env:"SHRTY_PORT"`
Domain string `env:"SHRTY_DOMAIN"`
Title string `env:"SHRTY_TITLE"`
CheckDomain bool `env:"SHRTY_CHECK_DOMAIN"`
DB string `env:"SHRT_DB_FILE"`
APIkey string `env:"SHRT_API_KEY"`
Host string `env:"SHRT_HOST"`
Port string `env:"SHRT_PORT"`
Domain string `env:"SHRT_DOMAIN"`
Title string `env:"SHRT_TITLE"`
CheckDomain bool `env:"SHRT_CHECK_DOMAIN"`
}
func (c Config) HostPort() string {
@ -52,17 +48,10 @@ 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")

@ -9,7 +9,7 @@ import (
func TestConfig_ReadConfigDefaults(t *testing.T){
originalArgs := os.Args
os.Args = []string{"shrty"}
os.Args = []string{"shrt"}
defer func(){ os.Args = originalArgs }()
d := DefaultConfig()
@ -24,7 +24,6 @@ func TestConfig_ReadConfig(t *testing.T){
"--title=title",
"--apikey=apikey",
"--domain=domain",
"--root=root",
}
expected := &Config{
@ -34,7 +33,6 @@ func TestConfig_ReadConfig(t *testing.T){
Title: "title",
APIkey: "apikey",
Domain: "domain",
RootPath: "root",
}
cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), input)
@ -43,20 +41,18 @@ 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"))
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")
assert.NoError(t, os.Setenv("SHRT_DB_FILE", "db"))
defer os.Unsetenv("SHRT_DB_FILE")
assert.NoError(t, os.Setenv("SHRT_API_KEY", "apikey"))
defer os.Unsetenv("SHRT_API_KEY")
assert.NoError(t, os.Setenv("SHRT_HOST", "host"))
defer os.Unsetenv("SHRT_HOST")
assert.NoError(t, os.Setenv("SHRT_PORT", "port"))
defer os.Unsetenv("SHRT_PORT")
assert.NoError(t, os.Setenv("SHRT_DOMAIN", "domain"))
defer os.Unsetenv("SHRT_DOMAIN")
assert.NoError(t, os.Setenv("SHRT_TITLE", "title"))
defer os.Unsetenv("SHRT_TITLE")
expected := &Config{
Host: "host",
@ -65,37 +61,6 @@ func TestConfig_ReadConfigFromEnv(t *testing.T) {
Title: "title",
APIkey: "apikey",
Domain: "domain",
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{})

@ -3,10 +3,9 @@ package main
import (
"net/http"
"github.com/kreativmonkey/shrt/shrty"
shrty "github.com/kreativmonkey/shrt/shrty"
"log"
"fmt"
"path"
)
const version = "0.06"
@ -36,9 +35,9 @@ type shrt struct {
func main() {
router := NewRouter()
s := http.StripPrefix("/css/", http.FileServer(http.Dir(path.Join(config.RootPath, "/template/css/"))))
s := http.StripPrefix("/css/", http.FileServer(http.Dir("./template/css/")))
router.PathPrefix("/css/").Handler(s)
s = http.StripPrefix("/img/", http.FileServer(http.Dir(path.Join(config.RootPath, "/template/img/"))))
s = http.StripPrefix("/img/", http.FileServer(http.Dir("./template/img/")))
router.PathPrefix("/img/").Handler(s)
/*

Loading…
Cancel
Save