Skip to content

Commit

Permalink
Reorganised structure
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed May 21, 2023
1 parent 5bcdb48 commit c1fd15a
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 123 deletions.
16 changes: 8 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const (
)

type UncorsConfig struct {
HTTPPort int `mapstructure:"http-port" validate:"required"`
Mappings []URLMapping `mapstructure:"mappings" validate:"required"`
Proxy string `mapstructure:"proxy"`
Debug bool `mapstructure:"debug"`
HTTPSPort int `mapstructure:"https-port"`
CertFile string `mapstructure:"cert-file"`
KeyFile string `mapstructure:"key-file"`
HTTPPort int `mapstructure:"http-port" validate:"required"`
Mappings Mappings `mapstructure:"mappings" validate:"required"`
Proxy string `mapstructure:"proxy"`
Debug bool `mapstructure:"debug"`
HTTPSPort int `mapstructure:"https-port"`
CertFile string `mapstructure:"cert-file"`
KeyFile string `mapstructure:"key-file"`
}

func (config *UncorsConfig) IsHTTPSEnabled() bool {
Expand All @@ -39,7 +39,7 @@ func LoadConfiguration(viperInstance *viper.Viper, args []string) (*UncorsConfig
}

configuration := &UncorsConfig{
Mappings: []URLMapping{},
Mappings: []Mapping{},
}

if configPath := viperInstance.GetString("config"); len(configPath) > 0 {
Expand Down
8 changes: 4 additions & 4 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 80,
HTTPSPort: 443,
Mappings: []config.URLMapping{},
Mappings: []config.Mapping{},
},
},
{
Expand All @@ -37,7 +37,7 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 8080,
HTTPSPort: 443,
Mappings: []config.URLMapping{
Mappings: []config.Mapping{
{From: "http://demo", To: "https://demo.com"},
},
},
Expand All @@ -47,7 +47,7 @@ func TestLoadConfiguration(t *testing.T) {
args: []string{params.Config, "/full-config.yaml"},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []config.URLMapping{
Mappings: []config.Mapping{
{From: "http://demo1", To: "https://demo1.com"},
{From: "http://other-demo2", To: "https://demo2.io", Mocks: []config.Mock{
{
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestLoadConfiguration(t *testing.T) {
},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []config.URLMapping{
Mappings: []config.Mapping{
{From: "http://demo1", To: "https://demo1.com"},
{
From: "http://other-demo2",
Expand Down
6 changes: 4 additions & 2 deletions internal/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"
"github.com/evg4b/uncors/internal/config/hooks"

Check failure on line 5 in internal/config/helpers.go

View workflow job for this annotation

GitHub Actions / build

File is not `goimports`-ed (goimports)
"strings"

"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -29,15 +30,15 @@ func readURLMapping(config *viper.Viper, configuration *UncorsConfig) error {

for index, key := range from {
value := to[index]
prev, ok := lo.Find(configuration.Mappings, func(item URLMapping) bool {
prev, ok := lo.Find(configuration.Mappings, func(item Mapping) bool {
return strings.EqualFold(item.From, key)
})

if ok {
// log.Warningf("Mapping for %s from (%s) replaced new value (%s)", key, prev, value)
prev.To = value
} else {
configuration.Mappings = append(configuration.Mappings, URLMapping{
configuration.Mappings = append(configuration.Mappings, Mapping{
From: key,
To: value,
})
Expand All @@ -49,6 +50,7 @@ func readURLMapping(config *viper.Viper, configuration *UncorsConfig) error {

func decodeConfig[T any](data any, mapping *T, decodeFuncs ...mapstructure.DecodeHookFunc) error {
hook := mapstructure.ComposeDecodeHookFunc(
hooks.StringToTimeDurationHookFunc(),
mapstructure.StringToSliceHookFunc(","),
mapstructure.ComposeDecodeHookFunc(decodeFuncs...),
)
Expand Down
32 changes: 16 additions & 16 deletions internal/config/url_mapping.go → internal/config/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@ import (
"github.com/samber/lo"
)

type URLMapping struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
Statics StaticDirMappings `mapstructure:"statics"`
Mocks []Mock `mapstructure:"mocks"`
type Mapping struct {
From string `mapstructure:"from"`
To string `mapstructure:"to"`
Statics StaticDirs `mapstructure:"statics"`
Mocks []Mock `mapstructure:"mocks"`
}

func (u URLMapping) Clone() URLMapping {
return URLMapping{
func (u Mapping) Clone() Mapping {
return Mapping{
From: u.From,
To: u.To,
Statics: lo.If(u.Statics == nil, StaticDirMappings(nil)).
ElseF(func() StaticDirMappings {
return lo.Map(u.Statics, func(item StaticDirMapping, index int) StaticDirMapping {
Statics: lo.If(u.Statics == nil, StaticDirs(nil)).
ElseF(func() StaticDirs {
return lo.Map(u.Statics, func(item StaticDir, index int) StaticDir {
return item.Clone()
})
}),
}
}

var urlMappingType = reflect.TypeOf(URLMapping{})
var urlMappingFields = getTagValues(urlMappingType, "mapstructure")
var mappingType = reflect.TypeOf(Mapping{})
var mappingFields = getTagValues(mappingType, "mapstructure")

func URLMappingHookFunc() mapstructure.DecodeHookFunc {
return func(f reflect.Type, t reflect.Type, rawData any) (any, error) {
if t != urlMappingType || f.Kind() != reflect.Map {
if t != mappingType || f.Kind() != reflect.Map {
return rawData, nil
}

if data, ok := rawData.(map[string]any); ok {
availableFields, _ := lo.Difference(lo.Keys(data), urlMappingFields)
availableFields, _ := lo.Difference(lo.Keys(data), mappingFields)
if len(data) == 1 && len(availableFields) == 1 {
return URLMapping{
return Mapping{
From: availableFields[0],
To: data[availableFields[0]].(string), // nolint: forcetypeassert
}, nil
}

mapping := URLMapping{}
mapping := Mapping{}
err := decodeConfig(data, &mapping, StaticDirMappingHookFunc())

return mapping, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ func TestURLMappingHookFunc(t *testing.T) {
tests := []struct {
name string
config string
expected config.URLMapping
expected config.Mapping
}{
{
name: "simple key-value mapping",
config: "http://localhost:4200: https://github.com",
expected: config.URLMapping{
expected: config.Mapping{
From: "http://localhost:4200",
To: "https://github.com",
},
},
{
name: "full object mapping",
config: "{ from: http://localhost:3000, to: https://google.com }",
expected: config.URLMapping{
expected: config.Mapping{
From: "http://localhost:3000",
To: "https://google.com",
},
Expand All @@ -50,7 +50,7 @@ func TestURLMappingHookFunc(t *testing.T) {
err := viperInstance.ReadInConfig()
testutils.CheckNoError(t, err)

actual := config.URLMapping{}
actual := config.Mapping{}

err = viperInstance.Unmarshal(&actual, viper.DecodeHook(
config.URLMappingHookFunc(),
Expand All @@ -66,31 +66,31 @@ func TestURLMappingHookFunc(t *testing.T) {
func TestURLMappingClone(t *testing.T) {
tests := []struct {
name string
expected config.URLMapping
expected config.Mapping
}{
{
name: "empty structure",
expected: config.URLMapping{},
expected: config.Mapping{},
},
{
name: "structure with 1 field",
expected: config.URLMapping{
expected: config.Mapping{
From: localhost,
},
},
{
name: "structure with 2 field",
expected: config.URLMapping{
expected: config.Mapping{
From: localhost,
To: localhostSecure,
},
},
{
name: "structure with inner collections",
expected: config.URLMapping{
expected: config.Mapping{
From: localhost,
To: localhostSecure,
Statics: []config.StaticDirMapping{
Statics: []config.StaticDir{
{Path: "/cc", Dir: "cc"},
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/mappings.go → internal/config/mappings.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ui
package config

import (
"fmt"
"strings"

"github.com/evg4b/uncors/internal/config"
)

func Mappings(mappings []config.URLMapping) string {
type Mappings []Mapping

func (mappings Mappings) String() string {
var builder strings.Builder

for _, mapping := range mappings {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//nolint:lll
package ui_test
package config_test

import (
"testing"

"github.com/evg4b/uncors/internal/config"

Check failure on line 5 in internal/config/mappings_test.go

View workflow job for this annotation

GitHub Actions / build

File is not `goimports`-ed (goimports)
"testing"

"github.com/evg4b/uncors/internal/ui"
"github.com/stretchr/testify/assert"
)

func TestMappings(t *testing.T) {
tests := []struct {
name string
mappings []config.URLMapping
mappings config.Mappings
expected []string
}{
{
Expand All @@ -22,14 +20,14 @@ func TestMappings(t *testing.T) {
},
{
name: "http mapping only",
mappings: []config.URLMapping{
mappings: config.Mappings{
{From: "http://localhost", To: "https://github.com"},
},
expected: []string{"PROXY: http://localhost => https://github.com"},
},
{
name: "http and https mappings",
mappings: []config.URLMapping{
mappings: config.Mappings{
{From: "http://localhost", To: "https://github.com"},
{From: "https://localhost", To: "https://github.com"},
},
Expand All @@ -40,7 +38,7 @@ func TestMappings(t *testing.T) {
},
{
name: "mapping and mocks",
mappings: []config.URLMapping{
mappings: config.Mappings{
{From: "http://localhost", To: "https://github.com", Mocks: []config.Mock{
{}, {}, {},
}},
Expand All @@ -55,7 +53,7 @@ func TestMappings(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := ui.Mappings(tt.mappings)
actual := tt.mappings.String()

for _, expectedLine := range tt.expected {
assert.Contains(t, actual, expectedLine)
Expand Down
16 changes: 8 additions & 8 deletions internal/config/static_mapping.go → internal/config/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import (
"github.com/mitchellh/mapstructure"
)

type StaticDirMappings = []StaticDirMapping
type StaticDirs = []StaticDir

type StaticDirMapping struct {
type StaticDir struct {
Path string `mapstructure:"path"`
Dir string `mapstructure:"dir"`
Index string `mapstructure:"index"`
}

func (s StaticDirMapping) Clone() StaticDirMapping {
return StaticDirMapping{
func (s StaticDir) Clone() StaticDir {
return StaticDir{
Path: s.Path,
Dir: s.Dir,
Index: s.Index,
}
}

var staticDirMappingsType = reflect.TypeOf(StaticDirMappings{})
var staticDirMappingsType = reflect.TypeOf(StaticDirs{})

func StaticDirMappingHookFunc() mapstructure.DecodeHookFunc { //nolint: ireturn
return func(f reflect.Type, t reflect.Type, rawData any) (any, error) {
Expand All @@ -35,18 +35,18 @@ func StaticDirMappingHookFunc() mapstructure.DecodeHookFunc { //nolint: ireturn
return rawData, nil
}

var mappings StaticDirMappings
var mappings StaticDirs
for path, mappingDef := range mappingsDefs {
if def, ok := mappingDef.(string); ok {
mappings = append(mappings, StaticDirMapping{
mappings = append(mappings, StaticDir{
Path: path,
Dir: def,
})

continue
}

mapping := StaticDirMapping{}
mapping := StaticDir{}
err := decodeConfig(mappingDef, &mapping)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit c1fd15a

Please sign in to comment.