From 23d658e902dbe67e565873f86523902ca616bace Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Tue, 28 Dec 2021 05:23:29 +0500 Subject: [PATCH] :recycle: Refactored config domain --- internal/domain/config.go | 99 ++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/internal/domain/config.go b/internal/domain/config.go index e3d30c0..839cf22 100644 --- a/internal/domain/config.go +++ b/internal/domain/config.go @@ -1,23 +1,100 @@ package domain import ( + "net" + "path/filepath" "testing" + "time" - "github.com/spf13/viper" + "github.com/valyala/fasttemplate" ) -// TODO(toby3d): create Config domain +type ( + Config struct { + Database ConfigDatabase + IndieAuth ConfigIndieAuth + Server ConfigServer + Name string + RunMode string + } + + ConfigIndieAuth struct { + JWTSecret interface{} + AccessTokenExpirationTime time.Duration + JWTSigningAlgorithm string + JWTSigningPrivateKeyFile string + CodeLength int + JWTNonceLength int + Enabled bool + } + + ConfigServer struct { + CertificateFile string + Domain string + Host string + KeyFile string + Protocol string + RootURL string + StaticRootPath string + StaticURLPrefix string + Port string + EnablePprof bool + } + + ConfigDatabase struct { + Path string + Type string + } +) + +// GetAddress return host:port address. +func (cs *ConfigServer) GetAddress() string { + return net.JoinHostPort(cs.Host, cs.Port) +} + +// GetRootURL returns generated from template RootURL. +func (cs *ConfigServer) GetRootURL() string { + return fasttemplate.ExecuteString(cs.RootURL, `{{`, `}}`, map[string]interface{}{ + "domain": cs.Domain, + "host": cs.Host, + "port": cs.Port, + "protocol": cs.Protocol, + "staticRootPath": cs.StaticRootPath, + "staticUrlPrefix": cs.StaticURLPrefix, + }) +} // TestConfig returns a valid *viper.Viper with the generated test data filled in. -func TestConfig(tb testing.TB) *viper.Viper { +func TestConfig(tb testing.TB) *Config { tb.Helper() - v := viper.New() - v.Set("indieauth.jwtSecret", "hackme") - v.Set("indieauth.jwtSigningAlgorithm", "HS256") - v.Set("server.domain", "example.com") - v.Set("server.protocol", "https") - v.Set("server.rootUrl", "{{protocol}}://{{domain}}/") - - return v + return &Config{ + Name: "IndieAuth", + RunMode: "dev", + Database: ConfigDatabase{ + Path: filepath.Join("test", "development.db"), + Type: "bolt", + }, + IndieAuth: ConfigIndieAuth{ + AccessTokenExpirationTime: time.Hour, + CodeLength: 32, //nolint: gomnd + Enabled: true, + JWTNonceLength: 22, //nolint: gomnd + JWTSecret: []byte("hackme"), + JWTSigningAlgorithm: "HS256", + JWTSigningPrivateKeyFile: filepath.Join("jwt", "private.pem"), + }, + Server: ConfigServer{ + CertificateFile: filepath.Join("https", "cert.pem"), + Domain: "localhost", + EnablePprof: false, + Host: "0.0.0.0", + KeyFile: filepath.Join("https", "key.pem"), + Port: "3000", + Protocol: "http", + RootURL: "{{protocol}}://{{domain}}:{{port}}/", + StaticRootPath: "/", + StaticURLPrefix: "/static", + }, + } }