Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 21, 2023
1 parent 53a4dc1 commit bb9fe44
Show file tree
Hide file tree
Showing 34 changed files with 284 additions and 116 deletions.
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/pseidemann/finish v1.2.0
github.com/pterm/pterm v0.12.62
github.com/pterm/pterm v0.12.63
github.com/samber/lo v1.38.1
github.com/spf13/afero v1.9.5
github.com/spf13/pflag v1.0.5
Expand All @@ -23,7 +23,7 @@ require (
)

require (
atomicgo.dev/cursor v0.1.2 // indirect
atomicgo.dev/cursor v0.1.3 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.0.2 // indirect
github.com/containerd/console v1.0.3 // indirect
Expand All @@ -32,22 +32,22 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
Expand Down
163 changes: 163 additions & 0 deletions go.sum

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions internal/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

type CacheGlobs []string

func (globs CacheGlobs) Clone() CacheGlobs {
if globs == nil {
func (g CacheGlobs) Clone() CacheGlobs {
if g == nil {
return nil
}

cacheGlobs := make(CacheGlobs, 0, len(globs))
cacheGlobs = append(cacheGlobs, globs...)
cacheGlobs := make(CacheGlobs, 0, len(g))
cacheGlobs = append(cacheGlobs, g...)

return cacheGlobs
}
Expand All @@ -23,15 +23,15 @@ type CacheConfig struct {
Methods []string `mapstructure:"methods"`
}

func (config *CacheConfig) Clone() *CacheConfig {
func (c *CacheConfig) Clone() *CacheConfig {
var methods []string
if config.Methods != nil {
methods = append(methods, config.Methods...)
if c.Methods != nil {
methods = append(methods, c.Methods...)
}

return &CacheConfig{
ExpirationTime: config.ExpirationTime,
ClearTime: config.ClearTime,
ExpirationTime: c.ExpirationTime,
ClearTime: c.ClearTime,
Methods: methods,
}
}
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type UncorsConfig struct {
CacheConfig CacheConfig `mapstructure:"cache-config"`
}

func (config *UncorsConfig) IsHTTPSEnabled() bool {
return len(config.CertFile) > 0 && len(config.KeyFile) > 0 && config.HTTPSPort > 0
func (c *UncorsConfig) IsHTTPSEnabled() bool {
return len(c.CertFile) > 0 && len(c.KeyFile) > 0 && c.HTTPSPort > 0
}

func LoadConfiguration(viperInstance *viper.Viper, args []string) (*UncorsConfig, error) {
Expand Down
12 changes: 6 additions & 6 deletions internal/config/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type Mapping struct {
Cache CacheGlobs `mapstructure:"cache"`
}

func (u *Mapping) Clone() Mapping {
func (m *Mapping) Clone() Mapping {
return Mapping{
From: u.From,
To: u.To,
Statics: u.Statics.Clone(),
Mocks: u.Mocks.Clone(),
Cache: u.Cache.Clone(),
From: m.From,
To: m.To,
Statics: m.Statics.Clone(),
Mocks: m.Mocks.Clone(),
Cache: m.Cache.Clone(),
}
}

Expand Down
16 changes: 8 additions & 8 deletions internal/config/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ package config
import (
"strings"

"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/pkg/urlx"
"github.com/samber/lo"
)

type Mappings []Mapping

func (mappings Mappings) String() string {
var builder strings.Builder
func (m Mappings) String() string {
builder := &strings.Builder{}

for _, group := range lo.GroupBy(mappings, extractHost) {
for _, group := range lo.GroupBy(m, extractHost) {
for _, mapping := range group {
builder.WriteString(sfmt.Sprintf("%s => %s\n", mapping.From, mapping.To))
helpers.Fprintf(builder, "%s => %s\n", mapping.From, mapping.To)
}

mapping := group[0]
for _, mock := range mapping.Mocks {
builder.WriteString(sfmt.Sprintf(" mock: [%s %d] %s\n", mock.Method, mock.Response.Code, mock.Path))
helpers.Fprintf(builder, " mock: %s\n", mock.String())
}
for _, static := range mapping.Statics {
builder.WriteString(sfmt.Sprintf(" static: %s => %s\n", static.Path, static.Dir))
helpers.Fprintf(builder, " static: %s\n", static.String())
}
for _, cacheGlob := range mapping.Cache {
builder.WriteString(sfmt.Sprintf(" cache: %s\n", cacheGlob))
helpers.Fprintf(builder, " cache: %s\n", cacheGlob)
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/config/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (m *Mock) Clone() Mock {
}
}

func (m *Mock) String() string {
return helpers.Sprintf("[%s %d] %s", m.Method, m.Response.Code, m.Path)
}

type Mocks []Mock

func (m Mocks) Clone() Mocks {
Expand Down
11 changes: 8 additions & 3 deletions internal/config/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"reflect"

"github.com/evg4b/uncors/internal/helpers"
"github.com/mitchellh/mapstructure"
"github.com/samber/lo"
)
Expand All @@ -21,14 +22,18 @@ func (s *StaticDirectory) Clone() StaticDirectory {
}
}

func (s *StaticDirectory) String() string {
return helpers.Sprintf("%s => %s", s.Path, s.Dir)
}

type StaticDirectories []StaticDirectory

func (d StaticDirectories) Clone() StaticDirectories {
if d == nil {
func (s StaticDirectories) Clone() StaticDirectories {
if s == nil {
return nil
}

return lo.Map(d, func(item StaticDirectory, index int) StaticDirectory {
return lo.Map(s, func(item StaticDirectory, index int) StaticDirectory {
return item.Clone()
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/config/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"github.com/go-playground/validator/v10"
)

func Validate(config *UncorsConfig) error {
func Validate(c *UncorsConfig) error {
validate := validator.New()

return validate.Struct(config) //nolint:wrapcheck
return validate.Struct(c) //nolint:wrapcheck
}
2 changes: 1 addition & 1 deletion internal/contracts/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}

type MiddlewareHandler interface {
type Middleware interface {
Wrap(next Handler) Handler
}

Expand Down
14 changes: 7 additions & 7 deletions internal/contracts/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
"testing"

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)

func TestCastToHTTPHandler(t *testing.T) {
const expectedBody = `{ "OK": true }`
uncorsHandler := contracts.HandlerFunc(func(w contracts.ResponseWriter, r *contracts.Request) {
handlerStub := contracts.HandlerFunc(func(w contracts.ResponseWriter, r *contracts.Request) {
w.WriteHeader(http.StatusOK)
sfmt.Fprint(w, expectedBody)
helpers.Fprint(w, expectedBody)
})

request := httptest.NewRequest(http.MethodGet, "/data", nil)
httpHandler := contracts.CastToHTTPHandler(uncorsHandler)
handler := contracts.CastToHTTPHandler(handlerStub)

t.Run("cast correctly", func(t *testing.T) {
recorder := httptest.NewRecorder()
responseWriter := contracts.WrapResponseWriter(recorder)

assert.NotPanics(t, func() {
httpHandler.ServeHTTP(responseWriter, request)
handler.ServeHTTP(responseWriter, request)
assert.Equal(t, expectedBody, testutils.ReadBody(t, recorder))
})
})
Expand All @@ -35,7 +35,7 @@ func TestCastToHTTPHandler(t *testing.T) {
recorder := httptest.NewRecorder()

assert.PanicsWithValue(t, contracts.ErrResponseNotCasted, func() {
httpHandler.ServeHTTP(recorder, request)
handler.ServeHTTP(recorder, request)
})
})
}
Expand All @@ -44,7 +44,7 @@ func TestHandlerFunc(t *testing.T) {
const expectedBody = `{ "OK": true }`
uncorsHandler := contracts.HandlerFunc(func(w contracts.ResponseWriter, r *contracts.Request) {
w.WriteHeader(http.StatusOK)
sfmt.Fprint(w, expectedBody)
helpers.Fprint(w, expectedBody)
})

recorder := httptest.NewRecorder()
Expand Down
4 changes: 2 additions & 2 deletions internal/contracts/response_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)
Expand All @@ -18,7 +18,7 @@ func TestResponseWriterWrapper(t *testing.T) {
writer := contracts.WrapResponseWriter(recorder)

writer.WriteHeader(expectedCode)
sfmt.Fprint(writer, expectedValue)
helpers.Fprint(writer, expectedValue)

t.Run("save status code", func(t *testing.T) {
assert.Equal(t, expectedCode, writer.StatusCode())
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/cache/cacheable_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/evg4b/uncors/internal/handler/cache"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/assert"
)
Expand All @@ -26,7 +26,7 @@ func TestCacheableResponseWriter(t *testing.T) {
{
name: "write body bytes only",
action: func(w http.ResponseWriter) {
sfmt.Fprint(w, bodyString)
helpers.Fprint(w, bodyString)
},
expected: &cache.CachedResponse{
Header: http.Header{
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestCacheableResponseWriter(t *testing.T) {
action: func(w http.ResponseWriter) {
header := w.Header()
header.Set(headers.ContentLength, "999")
sfmt.Fprint(w, bodyString)
helpers.Fprint(w, bodyString)
},
expected: &cache.CachedResponse{
Header: http.Header{
Expand All @@ -81,7 +81,7 @@ func TestCacheableResponseWriter(t *testing.T) {
header.Set(headers.ContentLength, "9")
header.Set(headers.Authorization, authorization)
writer.WriteHeader(http.StatusBadGateway)
sfmt.Fprint(writer, bodyString)
helpers.Fprint(writer, bodyString)
},
expected: &cache.CachedResponse{
Code: http.StatusBadGateway,
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/cache/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/cache"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/go-http-utils/headers"
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestCacheMiddleware(t *testing.T) {
handler := testutils.NewCounter(func(writer contracts.ResponseWriter, request *contracts.Request) {
writer.WriteHeader(testCase.statusCode)
testutils.CopyHeaders(expectedHeader, writer.Header())
sfmt.Fprintf(writer, expectedBody)
helpers.Fprintf(writer, expectedBody)
})

wrappedHandler := middleware.Wrap(handler)
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestCacheMiddleware(t *testing.T) {
handler := testutils.NewCounter(func(writer contracts.ResponseWriter, request *contracts.Request) {
writer.WriteHeader(testCase.statusCode)
testutils.CopyHeaders(expectedHeader, writer.Header())
sfmt.Fprintf(writer, expectedBody)
helpers.Fprintf(writer, expectedBody)
})

wrappedHandler := middleware.Wrap(handler)
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/mock/serve_raw_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mock
import (
"net/http"

"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/go-http-utils/headers"
)

Expand All @@ -16,5 +16,5 @@ func (h *Handler) serveRawContent(writer http.ResponseWriter) {
}

writer.WriteHeader(normaliseCode(response.Code))
sfmt.Fprint(writer, response.Raw)
helpers.Fprint(writer, response.Raw)
}
4 changes: 2 additions & 2 deletions internal/handler/static/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/static"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestMiddleware(t *testing.T) {
static.WithLogger(loggerMock),
static.WithNext(contracts.HandlerFunc(func(writer contracts.ResponseWriter, _ *contracts.Request) {
writer.WriteHeader(testHTTPStatusCode)
sfmt.Fprint(writer, testHTTPBody)
helpers.Fprint(writer, testHTTPBody)
})),
)

Expand Down
3 changes: 1 addition & 2 deletions internal/handler/uncors_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/evg4b/uncors/internal/handler/proxy"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/infra"
"github.com/evg4b/uncors/internal/sfmt"
"github.com/evg4b/uncors/internal/ui"
"github.com/evg4b/uncors/internal/urlreplacer"
"github.com/evg4b/uncors/pkg/urlx"
Expand Down Expand Up @@ -109,7 +108,7 @@ const wildcard = "*"
func replaceWildcards(host string) string {
count := strings.Count(host, wildcard)
for i := 1; i <= count; i++ {
host = strings.Replace(host, wildcard, sfmt.Sprintf("{p%d}", i), 1)
host = strings.Replace(host, wildcard, helpers.Sprintf("{p%d}", i), 1)
}

return host
Expand Down
Loading

0 comments on commit bb9fe44

Please sign in to comment.