🔥 Removed config package, use Config domain instead

This commit is contained in:
Maxim Lebedev 2021-12-29 04:10:03 +05:00
parent 945f0b47dd
commit a4988c3ded
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
5 changed files with 0 additions and 240 deletions

View File

@ -1,7 +0,0 @@
package config
type Repository interface {
GetBool(key string) bool
GetInt(key string) int
GetString(key string) string
}

View File

@ -1,29 +0,0 @@
package viper
import (
"github.com/spf13/viper"
"source.toby3d.me/website/oauth/internal/config"
)
type viperConfigRepository struct {
viper *viper.Viper
}
func NewViperConfigRepository(v *viper.Viper) config.Repository {
return &viperConfigRepository{
viper: v,
}
}
func (v *viperConfigRepository) GetString(key string) string {
return v.viper.GetString(key)
}
func (v *viperConfigRepository) GetInt(key string) int {
return v.viper.GetInt(key)
}
func (v *viperConfigRepository) GetBool(key string) bool {
return v.viper.GetBool(key)
}

View File

@ -1,54 +0,0 @@
package viper_test
import (
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
repository "source.toby3d.me/website/oauth/internal/config/repository/viper"
)
func TestGetString(t *testing.T) {
t.Parallel()
v := viper.New()
_ = v.MergeConfigMap(map[string]interface{}{
"testing": map[string]interface{}{
"sample": "text",
"answer": 42,
},
})
repo := repository.NewViperConfigRepository(v)
assert.Equal(t, "text", repo.GetString("testing.sample"))
assert.Equal(t, "42", repo.GetString("testing.answer"))
}
func TestGetInt(t *testing.T) {
t.Parallel()
v := viper.New()
_ = v.MergeConfigMap(map[string]interface{}{
"testing": map[string]interface{}{
"answer": 42,
},
})
assert.Equal(t, 42, repository.NewViperConfigRepository(v).GetInt("testing.answer"))
}
func TestGetBool(t *testing.T) {
t.Parallel()
v := viper.New()
_ = v.MergeConfigMap(map[string]interface{}{
"testing": map[string]interface{}{
"enabled": true,
"disabled": false,
},
})
assert.True(t, repository.NewViperConfigRepository(v).GetBool("testing.enabled"))
assert.False(t, repository.NewViperConfigRepository(v).GetBool("testing.disabled"))
}

View File

@ -1,28 +0,0 @@
package config
import "time"
type UseCase interface {
GetDatabasePath() string // data/indieauth.db
GetDatabaseType() string // bolt
GetIndieAuthAccessTokenExpirationTime() time.Duration // time.Hour
GetIndieAuthCodeLength() int // 32
GetIndieAuthEnabled() bool // true
GetIndieAuthJWTSecret() []byte // hackme
GetIndieAuthJWTSigningAlgorithm() string // RS256
GetIndieAuthJWTSigningPrivateKeyFile() string // jwt/private.pem
GetIndieAuthJWTNonceLength() int // 22
GetName() string // IndieAuth
GetRunMode() string // dev
GetServerAddress() string // 0.0.0.0:3000
GetServerCertificate() string // https/cert.pem
GetServerDomain() string // localhost
GetServerEnablePPROF() bool // false
GetServerHost() string // 0.0.0.0
GetServerKey() string // https/key.pem
GetServerPort() int // 3000
GetServerProtocol() string // http
GetServerRootURL() string // http://localhost:3000/
GetServerStaticRootPath() string // /
GetServerStaticURLPrefix() string // /static
}

View File

@ -1,122 +0,0 @@
package usecase
import (
"net"
"path/filepath"
"time"
"github.com/valyala/fasttemplate"
"source.toby3d.me/website/oauth/internal/config"
)
type configUseCase struct {
repo config.Repository
}
func NewConfigUseCase(repo config.Repository) config.UseCase {
return &configUseCase{
repo: repo,
}
}
func (useCase *configUseCase) GetName() string {
return useCase.repo.GetString("name")
}
func (useCase *configUseCase) GetRunMode() string {
return useCase.repo.GetString("runMode")
}
func (useCase *configUseCase) GetServerProtocol() string {
return useCase.repo.GetString("server.protocol")
}
func (useCase *configUseCase) GetServerDomain() string {
return useCase.repo.GetString("server.domain")
}
func (useCase *configUseCase) GetServerRootURL() string {
t := fasttemplate.New(useCase.repo.GetString("server.rootUrl"), `{{`, `}}`)
data := make(map[string]interface{})
for _, key := range []string{
"domain",
"httpAddr",
"httpPort",
"protocol",
} {
data[key] = useCase.repo.GetString("server." + key)
}
return t.ExecuteString(data)
}
func (useCase *configUseCase) GetServerStaticURLPrefix() string {
return useCase.repo.GetString("server.staticUrlPrefix")
}
func (useCase *configUseCase) GetServerHost() string {
return useCase.repo.GetString("server.host")
}
func (useCase *configUseCase) GetServerPort() int {
return useCase.repo.GetInt("server.port")
}
func (useCase *configUseCase) GetServerAddress() string {
return net.JoinHostPort(useCase.repo.GetString("server.host"),
useCase.repo.GetString("server.port"))
}
func (useCase *configUseCase) GetServerCertificate() string {
return filepath.Clean(useCase.repo.GetString("server.certFile"))
}
func (useCase *configUseCase) GetServerKey() string {
return filepath.Clean(useCase.repo.GetString("server.keyFile"))
}
func (useCase *configUseCase) GetServerStaticRootPath() string {
return useCase.repo.GetString("server.staticRootPath")
}
func (useCase *configUseCase) GetServerEnablePPROF() bool {
return useCase.repo.GetBool("server.enablePprof")
}
func (useCase *configUseCase) GetDatabaseType() string {
return useCase.repo.GetString("database.type")
}
func (useCase *configUseCase) GetDatabasePath() string {
return filepath.Clean(useCase.repo.GetString("database.path"))
}
func (useCase *configUseCase) GetIndieAuthEnabled() bool {
return useCase.repo.GetBool("indieauth.enabled")
}
func (useCase *configUseCase) GetIndieAuthAccessTokenExpirationTime() time.Duration {
return time.Duration(useCase.repo.GetInt("indieauth.accessTokenExpirationTime")) * time.Second
}
func (useCase *configUseCase) GetIndieAuthJWTSigningAlgorithm() string {
return useCase.repo.GetString("indieauth.jwtSigningAlgorithm")
}
func (useCase *configUseCase) GetIndieAuthJWTSecret() []byte {
return []byte(useCase.repo.GetString("indieauth.jwtSecret"))
}
func (useCase *configUseCase) GetIndieAuthJWTSigningPrivateKeyFile() string {
return filepath.Clean(useCase.repo.GetString("indieauth.jwtSigningPrivateKeyFile"))
}
func (useCase *configUseCase) GetIndieAuthCodeLength() int {
return useCase.repo.GetInt("indieauth.codeLength")
}
func (useCase *configUseCase) GetIndieAuthJWTNonceLength() int {
return useCase.repo.GetInt("indieauth.jwtNonceLength")
}