Global Config Close #3
parent
9c36dd2732
commit
b981f8b71c
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type response struct {
|
||||
Action string `json:"action"`
|
||||
Status string `json:"status_code"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
// GET: http://example.com/api/action/shorten?key=API_KEY_HERE&url=https://google.com&custom_ending=CUSTOM_ENDING
|
||||
// Response: {"action": "shorten","result": "https://example.com/5kq"}
|
||||
func APIRequest(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/caarlos0/env"
|
||||
"os"
|
||||
)
|
||||
|
||||
func DefaultConfig() Config {
|
||||
return Config{
|
||||
DB: "./test.db",
|
||||
APIkey: "thisIsNotASecretTokenNow",
|
||||
Host: "localhost",
|
||||
Port: "6889",
|
||||
Title: "Sgot",
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func (c Config) HostPort() string {
|
||||
return c.Host + ":" + c.Port
|
||||
}
|
||||
|
||||
func ReadConfig() *Config {
|
||||
c, err := readConfig(flag.NewFlagSet(os.Args[0], flag.ExitOnError), os.Args[1:])
|
||||
if err != nil {
|
||||
// sould never happen, because of flag default policy ExitOnError
|
||||
panic(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func readConfig(f *flag.FlagSet, args []string) (*Config, error){
|
||||
config := DefaultConfig()
|
||||
|
||||
// Environment variables
|
||||
err := env.Parse(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
// Arguments variables
|
||||
err = f.Parse(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, err
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"os"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"flag"
|
||||
)
|
||||
|
||||
func TestConfig_ReadConfigDefaults(t *testing.T){
|
||||
originalArgs := os.Args
|
||||
os.Args = []string{"shrt"}
|
||||
defer func(){ os.Args = originalArgs }()
|
||||
|
||||
d := DefaultConfig()
|
||||
assert.Equal(t, &d, ReadConfig())
|
||||
}
|
||||
|
||||
func TestConfig_ReadConfig(t *testing.T){
|
||||
input := []string{
|
||||
"--host=host",
|
||||
"--port=port",
|
||||
"--db-file=db",
|
||||
"--title=title",
|
||||
"--apikey=apikey",
|
||||
"--domain=domain",
|
||||
}
|
||||
|
||||
expected := &Config{
|
||||
Host: "host",
|
||||
Port: "port",
|
||||
DB: "db",
|
||||
Title: "title",
|
||||
APIkey: "apikey",
|
||||
Domain: "domain",
|
||||
}
|
||||
|
||||
cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, cfg)
|
||||
}
|
||||
|
||||
func TestConfig_ReadConfigFromEnv(t *testing.T) {
|
||||
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",
|
||||
Port: "port",
|
||||
DB: "db",
|
||||
Title: "title",
|
||||
APIkey: "apikey",
|
||||
Domain: "domain",
|
||||
}
|
||||
|
||||
cfg, err := readConfig(flag.NewFlagSet("", flag.ContinueOnError), []string{})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, cfg)
|
||||
}
|
Loading…
Reference in New Issue