Skip to content

Commit

Permalink
feat(config): Remove ardanlabs/conf
Browse files Browse the repository at this point in the history
  • Loading branch information
ldebruijn committed Sep 8, 2024
1 parent 7620f5f commit a4a8235
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 166 deletions.
134 changes: 13 additions & 121 deletions internal/app/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"github.com/ldebruijn/graphql-protect/internal/app/http"
"github.com/ldebruijn/graphql-protect/internal/app/log"
"github.com/ldebruijn/graphql-protect/internal/business/persistedoperations"
"github.com/ldebruijn/graphql-protect/internal/business/rules/accesslogging"
Expand Down Expand Up @@ -31,101 +32,10 @@ func TestNewConfig(t *testing.T) {
applyConfig: func(_ *os.File) {

},
want: &Config{
Web: struct {
ReadTimeout time.Duration `conf:"default:5s" yaml:"read_timeout"`
WriteTimeout time.Duration `conf:"default:10s" yaml:"write_timeout"`
IdleTimeout time.Duration `conf:"default:120s" yaml:"idle_timeout"`
ShutdownTimeout time.Duration `conf:"default:20s" yaml:"shutdown_timeout"`
Host string `conf:"default:0.0.0.0:8080" yaml:"host"`
Path string `conf:"default:/graphql" yaml:"path"`
}(struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
Host string
Path string
}{
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 2 * time.Minute,
ShutdownTimeout: 20 * time.Second,
Host: "0.0.0.0:8080",
Path: "/graphql",
}),
ObfuscateValidationErrors: false,
Schema: schema.Config{
Path: "./schema.graphql",
AutoReload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:30s" yaml:"interval"`
}(struct {
Enabled bool
Interval time.Duration
}{Enabled: true, Interval: 30 * time.Second}),
},
Target: proxy.Config{
Timeout: 10 * time.Second,
KeepAlive: 3 * time.Minute,
Host: "http://localhost:8081",
},
PersistedOperations: persistedoperations.Config{
Enabled: false,
Loader: persistedoperations.LoaderConfig{
Type: "local",
Location: "./store",
Reload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:5m" yaml:"interval"`
Timeout time.Duration `conf:"default:10s" yaml:"timeout"`
}{
Enabled: true,
Interval: 5 * time.Minute,
Timeout: 10 * time.Second,
},
},
RejectOnFailure: true,
},
BlockFieldSuggestions: block_field_suggestions.Config{
Enabled: true,
Mask: "[redacted]",
},
ObfuscateUpstreamErrors: true,
MaxTokens: tokens.Config{
Enabled: true,
Max: 1000,
RejectOnFailure: true,
},
MaxAliases: aliases.Config{
Enabled: true,
Max: 15,
RejectOnFailure: true,
},
EnforcePost: enforce_post.Config{
Enabled: true,
},
MaxDepth: max_depth.Config{
Enabled: true,
Max: 15,
RejectOnFailure: true,
},
MaxBatch: batch.Config{
Enabled: true,
Max: 5,
RejectOnFailure: true,
},
AccessLogging: accesslogging.Config{
Enabled: true,
IncludedHeaders: nil,
IncludeOperationName: true,
IncludeVariables: true,
IncludePayload: false,
},
Log: log.Config{
Format: log.JSONFormat,
},
},
want: func() *Config {
cfg := defaults()
return &cfg
}(),
wantErr: false,
},
{
Expand Down Expand Up @@ -206,35 +116,21 @@ log:
`))
},
want: &Config{
Web: struct {
ReadTimeout time.Duration `conf:"default:5s" yaml:"read_timeout"`
WriteTimeout time.Duration `conf:"default:10s" yaml:"write_timeout"`
IdleTimeout time.Duration `conf:"default:120s" yaml:"idle_timeout"`
ShutdownTimeout time.Duration `conf:"default:20s" yaml:"shutdown_timeout"`
Host string `conf:"default:0.0.0.0:8080" yaml:"host"`
Path string `conf:"default:/graphql" yaml:"path"`
}(struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
Host string
Path string
}{
Web: http.Config{
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 1 * time.Second,
ShutdownTimeout: 1 * time.Second,
Host: "host",
Path: "path",
}),
},
ObfuscateValidationErrors: true,
ObfuscateUpstreamErrors: false,
Schema: schema.Config{
Path: "path",
AutoReload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:30s" yaml:"interval"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
}(struct {
Enabled bool
Interval time.Duration
Expand All @@ -251,9 +147,9 @@ log:
Type: "gcp",
Location: "some-bucket",
Reload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:5m" yaml:"interval"`
Timeout time.Duration `conf:"default:10s" yaml:"timeout"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
Timeout time.Duration `yaml:"timeout"`
}{
Enabled: true,
Interval: 1 * time.Second,
Expand Down Expand Up @@ -325,11 +221,7 @@ log:
func TestWriteDefaultConfigToYaml(t *testing.T) {
t.Skip("not actually a test, abusing the test for easy generation of default configuration file")

cfg, err := NewConfig("")
if err != nil {
assert.NoError(t, err)
return
}
cfg := defaults()

file, err := os.OpenFile("default-config.yml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions internal/app/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package http
import "time"

type Config struct {
ReadTimeout time.Duration `conf:"default:5s" yaml:"read_timeout"`
WriteTimeout time.Duration `conf:"default:10s" yaml:"write_timeout"`
IdleTimeout time.Duration `conf:"default:120s" yaml:"idle_timeout"`
ShutdownTimeout time.Duration `conf:"default:20s" yaml:"shutdown_timeout"`
Host string `conf:"default:0.0.0.0:8080" yaml:"host"`
ReadTimeout time.Duration `yaml:"read_timeout"`
WriteTimeout time.Duration `yaml:"write_timeout"`
IdleTimeout time.Duration `yaml:"idle_timeout"`
ShutdownTimeout time.Duration `yaml:"shutdown_timeout"`
Host string `yaml:"host"`
// or maybe we just want to listen on everything and forward
Path string `conf:"default:/graphql" yaml:"path"`
// DebugHost string `conf:"default:0.0.0.0:4000"`
Path string `yaml:"path"`
// DebugHost string `yaml:"debug_host"`
}

func DefaultConfig() Config {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Config struct {
Format string `conf:"default:json" yaml:"format"`
Format string `yaml:"format"`
}

func DefaultConfig() Config {
Expand Down
20 changes: 10 additions & 10 deletions internal/business/persistedoperations/persisted_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type ErrorMessage struct {
}

type Config struct {
Enabled bool `conf:"default:false" yaml:"enabled"`
RejectOnFailure bool `conf:"default:true" yaml:"reject_on_failure"`
Enabled bool `yaml:"enabled"`
RejectOnFailure bool `yaml:"reject_on_failure"`
Loader LoaderConfig `yaml:"loader"`
}

Expand All @@ -65,9 +65,9 @@ func DefaultConfig() Config {
Type: "local",
Location: "./store",
Reload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:5m" yaml:"interval"`
Timeout time.Duration `conf:"default:10s" yaml:"timeout"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
Timeout time.Duration `yaml:"timeout"`
}(struct {
Enabled bool
Interval time.Duration
Expand All @@ -82,13 +82,13 @@ func DefaultConfig() Config {
}

type LoaderConfig struct {
Type string `conf:"default:local" yaml:"type"`
Location string `conf:"default:./store" yaml:"location"`
Type string `yaml:"type"`
Location string `yaml:"location"`
// Configuration for auto-reloading persisted operations
Reload struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:5m" yaml:"interval"`
Timeout time.Duration `conf:"default:10s" yaml:"timeout"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
Timeout time.Duration `yaml:"timeout"`
}
}

Expand Down
8 changes: 4 additions & 4 deletions internal/business/rules/accesslogging/accesslogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Enabled bool `yaml:"enabled"`
IncludedHeaders []string `yaml:"include_headers"`
IncludeOperationName bool `conf:"default:true" yaml:"include_operation_name"`
IncludeVariables bool `conf:"default:true" yaml:"include_variables"`
IncludePayload bool `conf:"default:false" yaml:"include_payload"`
IncludeOperationName bool `yaml:"include_operation_name"`
IncludeVariables bool `yaml:"include_variables"`
IncludePayload bool `yaml:"include_payload"`
}

func DefaultConfig() Config {
Expand Down
6 changes: 3 additions & 3 deletions internal/business/rules/aliases/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ var (
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Max int `conf:"default:15" yaml:"max"`
RejectOnFailure bool `conf:"default:true" yaml:"reject_on_failure"`
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
RejectOnFailure bool `yaml:"reject_on_failure"`
}

func DefaultConfig() Config {
Expand Down
6 changes: 3 additions & 3 deletions internal/business/rules/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ var (
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Max int `conf:"default:5" yaml:"max"`
RejectOnFailure bool `conf:"default:true" yaml:"reject_on_failure"`
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
RejectOnFailure bool `yaml:"reject_on_failure"`
}

func DefaultConfig() Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ var resultCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Mask string `conf:"default:[redacted]" yaml:"mask"`
Enabled bool `yaml:"enabled"`
Mask string `yaml:"mask"`
}

func DefaultConfig() Config {
Expand Down
2 changes: 1 addition & 1 deletion internal/business/rules/enforce_post/enforce_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Enabled bool `yaml:"enabled"`
}

func DefaultConfig() Config {
Expand Down
6 changes: 3 additions & 3 deletions internal/business/rules/max_depth/max_depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var resultCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Max int `conf:"default:15" yaml:"max"`
RejectOnFailure bool `conf:"default:true" yaml:"reject_on_failure"`
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
RejectOnFailure bool `yaml:"reject_on_failure"`
}

func DefaultConfig() Config {
Expand Down
6 changes: 3 additions & 3 deletions internal/business/rules/tokens/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var resultCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
)

type Config struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Max int `conf:"default:1000" yaml:"max"`
RejectOnFailure bool `conf:"default:true" yaml:"reject_on_failure"`
Enabled bool `yaml:"enabled"`
Max int `yaml:"max"`
RejectOnFailure bool `yaml:"reject_on_failure"`
}

func DefaultConfig() Config {
Expand Down
10 changes: 5 additions & 5 deletions internal/business/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ var reloadGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
[]string{"state"})

type Config struct {
Path string `conf:"default:./schema.graphql" yaml:"path"`
Path string `yaml:"path"`
AutoReload struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:30s" yaml:"interval"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
} `yaml:"auto_reload"`
}

func DefaultConfig() Config {
return Config{
Path: "./schema.graphql",
AutoReload: struct {
Enabled bool `conf:"default:true" yaml:"enabled"`
Interval time.Duration `conf:"default:30s" yaml:"interval"`
Enabled bool `yaml:"enabled"`
Interval time.Duration `yaml:"interval"`
}(struct {
Enabled bool
Interval time.Duration
Expand Down
6 changes: 3 additions & 3 deletions internal/http/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

type Config struct {
Timeout time.Duration `conf:"default:10s" yaml:"timeout"`
KeepAlive time.Duration `conf:"default:180s" yaml:"keep_alive"`
Host string `conf:"default:http://localhost:8081" yaml:"host"`
Timeout time.Duration `yaml:"timeout"`
KeepAlive time.Duration `yaml:"keep_alive"`
Host string `/localhost:8081" yaml:"host"`

Check failure on line 20 in internal/http/proxy/proxy.go

View workflow job for this annotation

GitHub Actions / lint

structtag: struct field tag `/localhost:8081" yaml:"host"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value (govet)
Tracing TracingConfig `yaml:"tracing"`
}

Expand Down

0 comments on commit a4a8235

Please sign in to comment.