From 4a6f56b44bb6e7a513def40af1f99e78b9496391 Mon Sep 17 00:00:00 2001 From: crapStone Date: Thu, 18 Jan 2024 21:22:19 +0100 Subject: [PATCH] WIP --- config/config.go | 28 ++++++++++++++-------------- config/setup.go | 21 +++++++++++++++++---- config/setup_test.go | 9 +++++---- example_config.toml | 23 +++++++++++------------ go.mod | 1 + go.sum | 2 ++ server/startup.go | 4 ++++ 7 files changed, 54 insertions(+), 34 deletions(-) diff --git a/config/config.go b/config/config.go index b4321c3..6cb972b 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,7 @@ package config type Config struct { - LogLevel string + LogLevel string `default:"warn"` Server ServerConfig Gitea GiteaConfig Database DatabaseConfig @@ -9,10 +9,10 @@ type Config struct { } type ServerConfig struct { - Host string - Port uint16 - HttpPort uint16 - HttpServerEnabled bool + Host string `default:"[::]"` + Port uint16 `default:"443"` + HttpPort uint16 `default:"80"` + HttpServerEnabled bool `default:"true"` MainDomain string RawDomain string PagesBranches []string @@ -23,24 +23,24 @@ type ServerConfig struct { type GiteaConfig struct { Root string Token string - LFSEnabled bool - FollowSymlinks bool - DefaultMimeType string + LFSEnabled bool `default:"false"` + FollowSymlinks bool `default:"false"` + DefaultMimeType string `default:"application/octet-stream"` ForbiddenMimeTypes []string } type DatabaseConfig struct { - Type string - Conn string + Type string `default:"sqlite3"` + Conn string `default:"certs.sqlite"` } type ACMEConfig struct { Email string - APIEndpoint string - AcceptTerms bool - UseRateLimits bool + APIEndpoint string `default:"https://acme-v02.api.letsencrypt.org/directory"` + AcceptTerms bool `default:"false"` + UseRateLimits bool `default:"true"` EAB_HMAC string EAB_KID string DNSProvider string - AccountConfigFile string + AccountConfigFile string `default:"acme-account.json"` } diff --git a/config/setup.go b/config/setup.go index 83e1049..e774084 100644 --- a/config/setup.go +++ b/config/setup.go @@ -4,6 +4,7 @@ import ( "os" "path" + "github.com/creasty/defaults" "github.com/pelletier/go-toml/v2" "github.com/rs/zerolog/log" "github.com/urfave/cli/v2" @@ -13,10 +14,23 @@ var ALWAYS_BLACKLISTED_PATHS = []string{ "/.well-known/acme-challenge/", } +func NewDefaultConfig() Config { + config := Config{} + if err := defaults.Set(&config); err != nil { + panic(err) + } + + // defaults does not support setting arrays from strings + config.Server.PagesBranches = []string{"main", "master", "pages"} + + return config +} + func ReadConfig(ctx *cli.Context) (*Config, error) { + config := NewDefaultConfig() // if config is not given as argument return empty config if !ctx.IsSet("config-file") { - return &Config{}, nil + return &config, nil } configFile := path.Clean(ctx.String("config-file")) @@ -27,9 +41,8 @@ func ReadConfig(ctx *cli.Context) (*Config, error) { return nil, err } - config := &Config{} - err = toml.Unmarshal(content, config) - return config, err + err = toml.Unmarshal(content, &config) + return &config, err } func MergeConfig(ctx *cli.Context, config *Config) { diff --git a/config/setup_test.go b/config/setup_test.go index ba1241f..a863e2f 100644 --- a/config/setup_test.go +++ b/config/setup_test.go @@ -48,13 +48,13 @@ func readTestConfig() (*Config, error) { return nil, err } - expectedConfig := &Config{} - err = toml.Unmarshal(content, expectedConfig) + expectedConfig := NewDefaultConfig() + err = toml.Unmarshal(content, &expectedConfig) if err != nil { return nil, err } - return expectedConfig, nil + return &expectedConfig, nil } func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) { @@ -62,7 +62,8 @@ func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) { t, func(ctx *cli.Context) error { cfg, err := ReadConfig(ctx) - assert.Equal(t, &Config{}, cfg) + expected := NewDefaultConfig() + assert.Equal(t, &expected, cfg) return err }, diff --git a/example_config.toml b/example_config.toml index e02984c..30e77c4 100644 --- a/example_config.toml +++ b/example_config.toml @@ -1,33 +1,32 @@ -# specify at which log level should be logged -# Possible options: trace, debug, info, warn, error logLevel = 'debug' [server] -host = '127.0.0.1' +host = '[::]' port = 443 httpPort = 80 httpServerEnabled = true -mainDomain = '' -rawDomain = '' +mainDomain = 'codeberg.page' +rawDomain = 'raw.codeberg.page' +pagesBranches = ["pages"] allowedCorsDomains = [] blacklistedPaths = [] [gitea] -root = '' -token = '' -lfsEnabled = false -followSymlinks = false +root = 'https://codeberg.org' +token = 'ASDF1234' +lfsEnabled = true +followSymlinks = true [database] type = 'sqlite' conn = 'certs.sqlite' [ACME] -email = '' -apiEndpoint = '' +email = 'noreply@example.email' +apiEndpoint = 'https://acme-v02.api.letsencrypt.org/directory' acceptTerms = false useRateLimits = false eab_hmac = '' eab_kid = '' dnsProvider = '' -accountConfigFile = '' +accountConfigFile = 'acme-account.json' diff --git a/go.mod b/go.mod index 35b2285..47e1e71 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,7 @@ require ( github.com/cloudflare/cloudflare-go v0.20.0 // indirect github.com/cpu/goacmedns v0.1.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect + github.com/creasty/defaults v1.7.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/davidmz/go-pageant v1.0.2 // indirect github.com/deepmap/oapi-codegen v1.6.1 // indirect diff --git a/go.sum b/go.sum index 8a6a1c2..1a10599 100644 --- a/go.sum +++ b/go.sum @@ -131,6 +131,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creasty/defaults v1.7.0 h1:eNdqZvc5B509z18lD8yc212CAqJNvfT1Jq6L8WowdBA= +github.com/creasty/defaults v1.7.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM= github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/server/startup.go b/server/startup.go index d6c4a55..6e5a823 100644 --- a/server/startup.go +++ b/server/startup.go @@ -3,6 +3,7 @@ package server import ( "context" "crypto/tls" + "encoding/json" "fmt" "net" "net/http" @@ -42,6 +43,9 @@ func Serve(ctx *cli.Context) error { } log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger().Level(logLevel) + foo, err := json.Marshal(cfg) + log.Trace().RawJSON("config", foo).Msg("starting server with config") + listeningSSLAddress := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port) listeningHTTPAddress := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.HttpPort)