♻️ Refactored config package
This commit is contained in:
parent
10fe29b3f0
commit
99bc92794f
|
@ -1,5 +1,6 @@
|
|||
package config
|
||||
|
||||
type Repository interface {
|
||||
GetInt(key string) int
|
||||
GetString(key string) string
|
||||
}
|
||||
|
|
|
@ -1,40 +1,24 @@
|
|||
package viper
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"gitlab.com/toby3d/indieauth/internal/config"
|
||||
"source.toby3d.me/website/oauth/internal/config"
|
||||
)
|
||||
|
||||
type viperConfigRepository struct {
|
||||
viper *viper.Viper
|
||||
}
|
||||
|
||||
func NewViperConfigRepository(v *viper.Viper) (config.Repository, error) {
|
||||
v.AddConfigPath(filepath.Join(".", "configs"))
|
||||
v.AddConfigPath(".")
|
||||
v.SetConfigName("config")
|
||||
v.SetConfigType("yaml")
|
||||
|
||||
for key, value := range map[string]interface{}{
|
||||
"database.client": "bolt",
|
||||
"database.connection.filename": "data/development.db",
|
||||
"server.port": 3000,
|
||||
"url": "http://127.0.0.1:3000/",
|
||||
} {
|
||||
v.SetDefault(key, value)
|
||||
}
|
||||
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func NewViperConfigRepository(v *viper.Viper) config.Repository {
|
||||
return &viperConfigRepository{
|
||||
viper: v,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (v *viperConfigRepository) GetString(key string) string {
|
||||
return v.viper.GetString(key)
|
||||
}
|
||||
|
||||
func (v *viperConfigRepository) GetInt(key string) int {
|
||||
return v.viper.GetInt(key)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
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) {
|
||||
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) {
|
||||
v := viper.New()
|
||||
|
||||
v.MergeConfigMap(map[string]interface{}{
|
||||
"testing": map[string]interface{}{
|
||||
"answer": 42,
|
||||
},
|
||||
})
|
||||
|
||||
assert.Equal(t, 42, repository.NewViperConfigRepository(v).GetInt("testing.answer"))
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
package config
|
||||
|
||||
type UseCase interface {
|
||||
GetURL() string
|
||||
GetHost() string
|
||||
GetPort() string
|
||||
GetAddr() string
|
||||
GetDatabaseFileName() string
|
||||
Addr() string
|
||||
DBFileName() string
|
||||
Host() string
|
||||
Port() int
|
||||
URL() string
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"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) URL() string {
|
||||
return useCase.repo.GetString("url")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) Host() string {
|
||||
return useCase.repo.GetString("server.host")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) Port() int {
|
||||
return useCase.repo.GetInt("server.port")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) Addr() string {
|
||||
return net.JoinHostPort(useCase.repo.GetString("server.host"), useCase.repo.GetString("server.port"))
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) DBFileName() string {
|
||||
return useCase.repo.GetString("database.connection.filename")
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package usecase_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"source.toby3d.me/website/oauth/internal/config"
|
||||
repository "source.toby3d.me/website/oauth/internal/config/repository/viper"
|
||||
"source.toby3d.me/website/oauth/internal/config/usecase"
|
||||
)
|
||||
|
||||
var ucase config.UseCase
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
v := viper.New()
|
||||
|
||||
for key, val := range map[string]interface{}{
|
||||
"database.client": "bolt",
|
||||
"database.connection.filename": "./data/development.db",
|
||||
"server.host": "127.0.0.1",
|
||||
"server.port": 3000,
|
||||
"url": "http://127.0.0.1:3000/",
|
||||
} {
|
||||
v.Set(key, val)
|
||||
}
|
||||
|
||||
ucase = usecase.NewConfigUseCase(repository.NewViperConfigRepository(v))
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestAddr(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "127.0.0.1:3000", ucase.Addr())
|
||||
}
|
||||
|
||||
func TestDBFileName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "./data/development.db", ucase.DBFileName())
|
||||
}
|
||||
|
||||
func TestHost(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "127.0.0.1", ucase.Host())
|
||||
}
|
||||
|
||||
func TestPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, 3000, ucase.Port())
|
||||
}
|
||||
|
||||
func TestURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "http://127.0.0.1:3000/", ucase.URL())
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package usecase
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"gitlab.com/toby3d/indieauth/internal/config"
|
||||
)
|
||||
|
||||
type configUseCase struct {
|
||||
repo config.Repository
|
||||
}
|
||||
|
||||
func NewConfigUseCase(repo config.Repository) config.UseCase {
|
||||
return &configUseCase{
|
||||
repo: repo,
|
||||
}
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) GetURL() string {
|
||||
return useCase.repo.GetString("url")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) GetHost() string {
|
||||
return useCase.repo.GetString("server.host")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) GetPort() string {
|
||||
return useCase.repo.GetString("server.port")
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) GetAddr() string {
|
||||
return net.JoinHostPort(useCase.GetHost(), useCase.GetPort())
|
||||
}
|
||||
|
||||
func (useCase *configUseCase) GetDatabaseFileName() string {
|
||||
return useCase.repo.GetString("database.connection.filename")
|
||||
}
|
Loading…
Reference in New Issue