-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
210 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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 | ||
} | ||
|
||
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 := pflag.NewFlagSet("uncors", pflag.ExitOnError) | ||
defineFlags(flags) | ||
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(flags *pflag.FlagSet) { | ||
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.Uint("http-port", defaultHTTPPort, "Local HTTP listening port") | ||
flags.Uint("https-port", 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.String("config", "", "Show debug output") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package configuration_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/internal/configuration" | ||
"github.com/evg4b/uncors/internal/middlewares/mock" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLoadConfiguration(t *testing.T) { | ||
fs := testutils.CreateFsForTest(t, "config_test_data") | ||
viperInstance := viper.New() | ||
viperInstance.SetFs(fs) | ||
|
||
t.Run("return default config", func(t *testing.T) { | ||
config, err := configuration.LoadConfiguration(viperInstance, []string{}) | ||
assert.NoError(t, err) | ||
assert.Equal(t, &configuration.UncorsConfig{ | ||
HTTPPort: 80, | ||
HTTPSPort: 443, | ||
Mappings: map[string]string{}, | ||
Mocks: []mock.Mock{}, | ||
}, config) | ||
}) | ||
|
||
t.Run("correctly parse configuration", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
config *configuration.UncorsConfig | ||
}{ | ||
{ | ||
name: "minimal config is set", | ||
args: []string{"--config", "/minimal-config.yaml"}, | ||
config: &configuration.UncorsConfig{ | ||
HTTPPort: 8080, | ||
HTTPSPort: 443, | ||
Mappings: map[string]string{ | ||
"http://demo": "https://demo.com", | ||
}, | ||
Mocks: []mock.Mock{}, | ||
}, | ||
}, | ||
} | ||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
config, err := configuration.LoadConfiguration(viperInstance, testCase.args) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, testCase.config, config) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
http-port: 8080 | ||
mappings: | ||
http://demo: https://demo.com |
2 changes: 1 addition & 1 deletion
2
internal/config/helpers_internal.go → internal/configuration/helpers_internal.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package config | ||
package configuration | ||
|
||
import ( | ||
"errors" | ||
|
2 changes: 1 addition & 1 deletion
2
internal/config/helpers_internal_test.go → ...al/configuration/helpers_internal_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package config | ||
package configuration | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package configuration | ||
|
||
import ( | ||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
func Validate(config *UncorsConfig) error { | ||
validate := validator.New() | ||
|
||
return validate.Struct(config) //nolint:wrapcheck | ||
} |
Oops, something went wrong.