-
Notifications
You must be signed in to change notification settings - Fork 0
feat: refactor components #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| // cmd/heimdall/main.go | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log/slog" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/arthurdotwork/alog" | ||
| "github.com/arthurdotwork/heimdall" | ||
| ) | ||
|
|
||
| func main() { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| slog.SetDefault(alog.Logger(alog.WithAttrs(slog.String("app", "heimdall")))) | ||
|
|
||
| configPath := flag.String("config", "config.yaml", "Path to configuration file") | ||
| flag.Parse() | ||
|
|
||
| gateway, err := heimdall.New(*configPath) | ||
| if err != nil { | ||
| slog.ErrorContext(ctx, "failed to create gateway", "error", err) | ||
| return | ||
| } | ||
|
|
||
| ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM) | ||
| defer stop() | ||
|
|
||
| slog.InfoContext(ctx, "starting heimdall", "addr", fmt.Sprintf("0.0.0.0:%d", gateway.Config().Gateway.Port)) | ||
| if err := gateway.Start(ctx); err != nil { | ||
| slog.ErrorContext(ctx, "failed to start gateway", "error", err) | ||
| return | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,62 @@ | ||
| package heimdall | ||
|
|
||
| import ( | ||
| "os" | ||
| "time" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| type GatewayConfig struct { | ||
| Port int `yaml:"port"` | ||
| ReadTimeout time.Duration `yaml:"read_timeout"` | ||
| WriteTimeout time.Duration `yaml:"write_timeout"` | ||
| ShutdownTimeout time.Duration `yaml:"shutdown_timeout"` | ||
| } | ||
|
|
||
| type EndpointConfig struct { | ||
| Path string `yaml:"path"` | ||
| Target string `yaml:"target"` | ||
| Method string `yaml:"method"` | ||
| Headers map[string][]string `yaml:"headers"` | ||
| AllowedHeaders []string `yaml:"allowed_headers"` | ||
| } | ||
|
|
||
| type Config struct { | ||
| Gateway GatewayConfig `yaml:"gateway"` | ||
| Endpoints []EndpointConfig `yaml:"endpoints"` | ||
| } | ||
|
|
||
| func LoadFromFile(path string) (*Config, error) { | ||
| file, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var config Config | ||
| if err := yaml.Unmarshal(file, &config); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &config, nil | ||
| } | ||
|
|
||
| func (c *Config) WithDefaults() *Config { | ||
| if c.Gateway.Port == 0 { | ||
| c.Gateway.Port = 8080 | ||
| } | ||
|
|
||
| if c.Gateway.ReadTimeout == 0 { | ||
| c.Gateway.ReadTimeout = 5 * time.Second | ||
| } | ||
|
|
||
| if c.Gateway.WriteTimeout == 0 { | ||
| c.Gateway.WriteTimeout = 5 * time.Second | ||
| } | ||
|
|
||
| if c.Gateway.ShutdownTimeout == 0 { | ||
| c.Gateway.ShutdownTimeout = 10 * time.Second | ||
| } | ||
|
|
||
| return c | ||
| } |
This file contains hidden or 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,92 @@ | ||
| package heimdall_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/arthurdotwork/heimdall" | ||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| func createTempConfig(t *testing.T, config map[string]any) string { | ||
| file, err := os.CreateTemp("", "config-*.yaml") | ||
| require.NoError(t, err) | ||
| defer file.Close() //nolint:errcheck | ||
|
|
||
| yamlMarshalled, err := yaml.Marshal(config) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = file.Write(yamlMarshalled) | ||
| require.NoError(t, err) | ||
|
|
||
| err = file.Sync() | ||
| require.NoError(t, err) | ||
|
|
||
| return file.Name() | ||
| } | ||
|
|
||
| func TestConfig_LoadFromFile(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("it should return an error if it can not open the config file", func(t *testing.T) { | ||
| cfg, err := heimdall.LoadFromFile("nonexistent.yaml") | ||
| require.Error(t, err) | ||
| require.Nil(t, cfg) | ||
| }) | ||
|
|
||
| t.Run("it should return an error if the config is invalid", func(t *testing.T) { | ||
| config := map[string]any{"gateway": map[string]any{"port": "invalid-port"}} | ||
|
|
||
| configPath := createTempConfig(t, config) | ||
| cfg, err := heimdall.LoadFromFile(configPath) | ||
| require.Error(t, err) | ||
| require.Nil(t, cfg) | ||
| }) | ||
|
|
||
| t.Run("it should parse and return the config", func(t *testing.T) { | ||
| config := map[string]any{"gateway": map[string]any{"port": 8080}} | ||
|
|
||
| configPath := createTempConfig(t, config) | ||
| cfg, err := heimdall.LoadFromFile(configPath) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, cfg) | ||
| require.Equal(t, 8080, cfg.Gateway.Port) | ||
| }) | ||
| } | ||
|
|
||
| func TestConfig_WithDefaults(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("it should set default values for missing fields", func(t *testing.T) { | ||
| config := &heimdall.Config{ | ||
| Gateway: heimdall.GatewayConfig{}, | ||
| } | ||
|
|
||
| config = config.WithDefaults() | ||
|
|
||
| require.Equal(t, 8080, config.Gateway.Port) | ||
| require.Equal(t, 5*time.Second, config.Gateway.ReadTimeout) | ||
| require.Equal(t, 5*time.Second, config.Gateway.WriteTimeout) | ||
| require.Equal(t, 10*time.Second, config.Gateway.ShutdownTimeout) | ||
| }) | ||
|
|
||
| t.Run("it should not override existing values", func(t *testing.T) { | ||
| config := &heimdall.Config{ | ||
| Gateway: heimdall.GatewayConfig{ | ||
| Port: 9090, | ||
| ReadTimeout: 10 * time.Second, | ||
| WriteTimeout: 15 * time.Second, | ||
| ShutdownTimeout: 20 * time.Second, | ||
| }, | ||
| } | ||
|
|
||
| config = config.WithDefaults() | ||
|
|
||
| require.Equal(t, 9090, config.Gateway.Port) | ||
| require.Equal(t, 10*time.Second, config.Gateway.ReadTimeout) | ||
| require.Equal(t, 15*time.Second, config.Gateway.WriteTimeout) | ||
| require.Equal(t, 20*time.Second, config.Gateway.ShutdownTimeout) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.