♻️ Refactored config package
This commit is contained in:
parent
10fe29b3f0
commit
99bc92794f
|
@ -1,5 +1,6 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
|
GetInt(key string) int
|
||||||
GetString(key string) string
|
GetString(key string) string
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,40 +1,24 @@
|
||||||
package viper
|
package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"gitlab.com/toby3d/indieauth/internal/config"
|
"source.toby3d.me/website/oauth/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type viperConfigRepository struct {
|
type viperConfigRepository struct {
|
||||||
viper *viper.Viper
|
viper *viper.Viper
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewViperConfigRepository(v *viper.Viper) (config.Repository, error) {
|
func NewViperConfigRepository(v *viper.Viper) config.Repository {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return &viperConfigRepository{
|
return &viperConfigRepository{
|
||||||
viper: v,
|
viper: v,
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *viperConfigRepository) GetString(key string) string {
|
func (v *viperConfigRepository) GetString(key string) string {
|
||||||
return v.viper.GetString(key)
|
return v.viper.GetString(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *viperConfigRepository) GetInt(key string) int {
|
||||||
|
return v.viper.GetInt(key)
|
||||||
|
}
|
||||||
|
|
36
internal/config/repository/viper/viper_config_test.go
Normal file
36
internal/config/repository/viper/viper_config_test.go
Normal file
|
@ -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
|
package config
|
||||||
|
|
||||||
type UseCase interface {
|
type UseCase interface {
|
||||||
GetURL() string
|
Addr() string
|
||||||
GetHost() string
|
DBFileName() string
|
||||||
GetPort() string
|
Host() string
|
||||||
GetAddr() string
|
Port() int
|
||||||
GetDatabaseFileName() string
|
URL() string
|
||||||
}
|
}
|
||||||
|
|
37
internal/config/usecase/config_ucase.go
Normal file
37
internal/config/usecase/config_ucase.go
Normal file
|
@ -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")
|
||||||
|
}
|
62
internal/config/usecase/config_ucase_test.go
Normal file
62
internal/config/usecase/config_ucase_test.go
Normal file
|
@ -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
Block a user