This commit is contained in:
crapStone 2024-01-18 21:22:19 +01:00
parent 7b3a09d8ac
commit 4a6f56b44b
No known key found for this signature in database
GPG key ID: 22D4BF0CF7CC29C8
7 changed files with 54 additions and 34 deletions

View file

@ -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"`
}

View file

@ -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) {

View file

@ -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
},