Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated config loading system #9

Merged
merged 17 commits into from
Mar 19, 2023
Merged
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: go build -tags release -v .

- name: Test
run: go test -tags release -json -v -coverprofile=coverage.out ./... > test-report.out
run: go test -tags release -v -coverprofile=coverage.out ./...

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ server.crt
dist/
uncors
.idea
node_modules
node_modules
7 changes: 6 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ linters:
- gofumpt
- nolintlint
- tagliatelle
linters-settings:
varnamelen:
ignore-names:
- form
- to
presets:
- bugs
- comment
Expand All @@ -42,4 +47,4 @@ presets:
- performance
- style
- test
- unused
- unused
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a href="https://go.dev">
<img alt="Go version" src="https://img.shields.io/github/go-mod/go-version/evg4b/uncors">
</a>
<a href="https://github.com/evg4b/uncors/releases">
<a href="https://github.com/evg4b/uncors/releases/latest">
<img alt="GitHub version" src="https://img.shields.io/github/v/tag/evg4b/uncors?label=version">
</a>
<a href="https://github.com/evg4b/uncors/blob/main/LICENSE">
Expand Down Expand Up @@ -73,7 +73,7 @@ yarn add uncors --dev
## Binary (Cross-platform)

Download the appropriate version for your platform
from [UNCORS releases page](https://github.com/evg4b/uncors/releases).
from [UNCORS releases page](https://github.com/evg4b/uncors/releases/latest).
Once downloaded, the binary can be run from anywhere. You don’t need to install it into a global location.
This works well for shared hosts and other systems where you don’t have a privileged account.

Expand Down Expand Up @@ -124,13 +124,37 @@ uncors --http-port 8080 --to 'https://github.com' --from 'http://localhost'
* `--cert-file` - Path to HTTPS certificate file.
* `--key-file` - Path to matching for certificate private key.
* `--proxy` - HTTP/HTTPS proxy to provide requests to real server (used system by default).
* `--mocks` - File with defined [mocks](#mocks)
* `--config` - Configuration file with defined [mocks](#mocks)
* `--debug` - Show debug output.


## Configuration

```yaml
# Base configuration
http-port: 8080 # Local HTTP listened port.
mappings:
http://localhost:3000: https://githib.com
debug: false # Show debug output.
proxy: localhost:8080

# HTTPS configuration
https-port: 8081 # Local HTTPS listened port.
cert-file: ~/server.crt # Path to HTTPS certificate file.
key-file: ~/server.key # Path to matching for certificate private key.

#Mocks
mocks:
- path: /hello-word
response:
code: 200
raw-content: 'Hello word'
```

## Mocks

Uncors has endpoint mocks mechanism.
All mocks should be defined in yaml file and passed as parameter `--mocks`.
All mocks should be defined in yaml config file and passed as parameter `--mocks`.
Currently available path, method, queries and headers filters
(for more information see [gorilla/mux](https://github.com/gorilla/mux#matching-routes) route matching).

Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/PuerkitoBio/purell v1.2.0
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/go-playground/validator/v10 v10.11.2
github.com/gojuno/minimock/v3 v3.1.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-version v1.6.0
Expand All @@ -15,7 +16,6 @@ require (
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
golang.org/x/net v0.8.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -24,8 +24,11 @@ require (
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // 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.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/leodido/go-urn v1.2.2 // indirect
github.com/lithammer/fuzzysearch v1.1.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
Expand All @@ -37,8 +40,10 @@ require (
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.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 13 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a h1:v6zMvHuY9yue4+QkG/HQ/W67wvtQmWJ4SDo9aK/GIno=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a/go.mod h1:I79BieaU4fxrw4LMXby6q5OS9XnoR9UIKLOzDFjUmuw=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/gojuno/minimock/v3 v3.1.1 h1:Zed1eJLjVBZBiqkbo4xMTUL85PPBXvKS6D+p+2SmA/E=
github.com/gojuno/minimock/v3 v3.1.1/go.mod h1:PzXPeYeGaIyyj7k1atODX1dcvvC4vSzz7kYPEhCP7PY=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -165,6 +172,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/leodido/go-urn v1.2.2 h1:7z68G0FCGvDk646jz1AelTYNYWrTNm0bEcFAo147wt4=
github.com/leodido/go-urn v1.2.2/go.mod h1:kUaIbLZWttglzwNuG0pgsh5vuV6u2YcGBYz1hIPjtOQ=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down Expand Up @@ -196,7 +205,8 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
Expand Down Expand Up @@ -244,6 +254,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down
31 changes: 0 additions & 31 deletions internal/config/helpers.go

This file was deleted.

72 changes: 0 additions & 72 deletions internal/config/helpers_test.go

This file was deleted.

82 changes: 82 additions & 0 deletions internal/configuration/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package configuration

import (
"fmt"

"github.com/evg4b/uncors/internal/middlewares/mock"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
defaultHTTPPort = 80
defaultHTTPSPort = 443
)

type UncorsConfig struct {
// Base config_test_data
HTTPPort int `mapstructure:"http-port" validate:"required"`
Mappings map[string]string `mapstructure:"mappings" validate:"required"`
Proxy string `mapstructure:"proxy"`
Debug bool `mapstructure:"debug"`
// HTTPS config_test_data
HTTPSPort int `mapstructure:"https-port"`
CertFile string `mapstructure:"cert-file"`
KeyFile string `mapstructure:"key-file"`
// Mocks config_test_data
Mocks []mock.Mock `mapstructure:"mocks"`
}

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

func LoadConfiguration(viperInstance *viper.Viper, args []string) (*UncorsConfig, error) {
flags := defineFlags()
if err := flags.Parse(args); err != nil {
return nil, fmt.Errorf("filed parsing flags: %w", err)
}

if err := viperInstance.BindPFlags(flags); err != nil {
return nil, fmt.Errorf("filed binding flags: %w", err)
}

configuration := &UncorsConfig{
Mappings: map[string]string{},
Mocks: []mock.Mock{},
}

configPath := viperInstance.GetString("config")
if len(configPath) > 0 {
viperInstance.SetConfigFile(configPath)
if err := viperInstance.ReadInConfig(); err != nil {
return nil, fmt.Errorf("filed to read config file '%s': %w", configPath, err)
}
}

if err := viperInstance.Unmarshal(configuration); err != nil {
return nil, fmt.Errorf("filed parsing configuraion: %w", err)
}

if err := readURLMapping(viperInstance, configuration); err != nil {
return nil, fmt.Errorf("recognize url mapping: %w", err)
}

return configuration, nil
}

func defineFlags() *pflag.FlagSet {
flags := pflag.NewFlagSet("uncors", pflag.ContinueOnError)
flags.Usage = pflag.Usage
flags.StringSlice("to", []string{}, "Target host with protocol for to the resource to be proxy")
flags.StringSlice("from", []string{}, "Local host with protocol for to the resource from which proxying will take place") //nolint: lll
flags.UintP("http-port", "p", defaultHTTPPort, "Local HTTP listening port")
flags.UintP("https-port", "s", defaultHTTPSPort, "Local HTTPS listening port")
flags.String("cert-file", "", "Path to HTTPS certificate file")
flags.String("key-file", "", "Path to matching for certificate private key")
flags.String("proxy", "", "HTTP/HTTPS proxy to provide requests to real server (used system by default)")
flags.Bool("debug", false, "Show debug output")
flags.StringP("config", "c", "", "Show debug output")

return flags
}
Loading