Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cmd/heimdall/main.go
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
}
}
30 changes: 0 additions & 30 deletions cmd/main.go

This file was deleted.

4 changes: 3 additions & 1 deletion config.yaml → config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ endpoints:
target: https://httpbin.org/get
method: GET
headers:
X-My-Header: MyValue
X-My-Header:
- MyValue
- AnotherValue
allowed_headers:
- X-Another-Header
62 changes: 62 additions & 0 deletions config.go
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
}
92 changes: 92 additions & 0 deletions config_test.go
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)
})
}
Loading