mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 06:16:58 +00:00
WIP
This commit is contained in:
parent
7b3a09d8ac
commit
4a6f56b44b
7 changed files with 54 additions and 34 deletions
|
@ -1,7 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
LogLevel string
|
LogLevel string `default:"warn"`
|
||||||
Server ServerConfig
|
Server ServerConfig
|
||||||
Gitea GiteaConfig
|
Gitea GiteaConfig
|
||||||
Database DatabaseConfig
|
Database DatabaseConfig
|
||||||
|
@ -9,10 +9,10 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
Host string
|
Host string `default:"[::]"`
|
||||||
Port uint16
|
Port uint16 `default:"443"`
|
||||||
HttpPort uint16
|
HttpPort uint16 `default:"80"`
|
||||||
HttpServerEnabled bool
|
HttpServerEnabled bool `default:"true"`
|
||||||
MainDomain string
|
MainDomain string
|
||||||
RawDomain string
|
RawDomain string
|
||||||
PagesBranches []string
|
PagesBranches []string
|
||||||
|
@ -23,24 +23,24 @@ type ServerConfig struct {
|
||||||
type GiteaConfig struct {
|
type GiteaConfig struct {
|
||||||
Root string
|
Root string
|
||||||
Token string
|
Token string
|
||||||
LFSEnabled bool
|
LFSEnabled bool `default:"false"`
|
||||||
FollowSymlinks bool
|
FollowSymlinks bool `default:"false"`
|
||||||
DefaultMimeType string
|
DefaultMimeType string `default:"application/octet-stream"`
|
||||||
ForbiddenMimeTypes []string
|
ForbiddenMimeTypes []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DatabaseConfig struct {
|
type DatabaseConfig struct {
|
||||||
Type string
|
Type string `default:"sqlite3"`
|
||||||
Conn string
|
Conn string `default:"certs.sqlite"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ACMEConfig struct {
|
type ACMEConfig struct {
|
||||||
Email string
|
Email string
|
||||||
APIEndpoint string
|
APIEndpoint string `default:"https://acme-v02.api.letsencrypt.org/directory"`
|
||||||
AcceptTerms bool
|
AcceptTerms bool `default:"false"`
|
||||||
UseRateLimits bool
|
UseRateLimits bool `default:"true"`
|
||||||
EAB_HMAC string
|
EAB_HMAC string
|
||||||
EAB_KID string
|
EAB_KID string
|
||||||
DNSProvider string
|
DNSProvider string
|
||||||
AccountConfigFile string
|
AccountConfigFile string `default:"acme-account.json"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/creasty/defaults"
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
@ -13,10 +14,23 @@ var ALWAYS_BLACKLISTED_PATHS = []string{
|
||||||
"/.well-known/acme-challenge/",
|
"/.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) {
|
func ReadConfig(ctx *cli.Context) (*Config, error) {
|
||||||
|
config := NewDefaultConfig()
|
||||||
// if config is not given as argument return empty config
|
// if config is not given as argument return empty config
|
||||||
if !ctx.IsSet("config-file") {
|
if !ctx.IsSet("config-file") {
|
||||||
return &Config{}, nil
|
return &config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
configFile := path.Clean(ctx.String("config-file"))
|
configFile := path.Clean(ctx.String("config-file"))
|
||||||
|
@ -27,9 +41,8 @@ func ReadConfig(ctx *cli.Context) (*Config, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &Config{}
|
err = toml.Unmarshal(content, &config)
|
||||||
err = toml.Unmarshal(content, config)
|
return &config, err
|
||||||
return config, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func MergeConfig(ctx *cli.Context, config *Config) {
|
func MergeConfig(ctx *cli.Context, config *Config) {
|
||||||
|
|
|
@ -48,13 +48,13 @@ func readTestConfig() (*Config, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedConfig := &Config{}
|
expectedConfig := NewDefaultConfig()
|
||||||
err = toml.Unmarshal(content, expectedConfig)
|
err = toml.Unmarshal(content, &expectedConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return expectedConfig, nil
|
return &expectedConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) {
|
func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) {
|
||||||
|
@ -62,7 +62,8 @@ func TestReadConfigShouldReturnEmptyConfigWhenConfigArgEmpty(t *testing.T) {
|
||||||
t,
|
t,
|
||||||
func(ctx *cli.Context) error {
|
func(ctx *cli.Context) error {
|
||||||
cfg, err := ReadConfig(ctx)
|
cfg, err := ReadConfig(ctx)
|
||||||
assert.Equal(t, &Config{}, cfg)
|
expected := NewDefaultConfig()
|
||||||
|
assert.Equal(t, &expected, cfg)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,33 +1,32 @@
|
||||||
# specify at which log level should be logged
|
|
||||||
# Possible options: trace, debug, info, warn, error
|
|
||||||
logLevel = 'debug'
|
logLevel = 'debug'
|
||||||
|
|
||||||
[server]
|
[server]
|
||||||
host = '127.0.0.1'
|
host = '[::]'
|
||||||
port = 443
|
port = 443
|
||||||
httpPort = 80
|
httpPort = 80
|
||||||
httpServerEnabled = true
|
httpServerEnabled = true
|
||||||
mainDomain = ''
|
mainDomain = 'codeberg.page'
|
||||||
rawDomain = ''
|
rawDomain = 'raw.codeberg.page'
|
||||||
|
pagesBranches = ["pages"]
|
||||||
allowedCorsDomains = []
|
allowedCorsDomains = []
|
||||||
blacklistedPaths = []
|
blacklistedPaths = []
|
||||||
|
|
||||||
[gitea]
|
[gitea]
|
||||||
root = ''
|
root = 'https://codeberg.org'
|
||||||
token = ''
|
token = 'ASDF1234'
|
||||||
lfsEnabled = false
|
lfsEnabled = true
|
||||||
followSymlinks = false
|
followSymlinks = true
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
type = 'sqlite'
|
type = 'sqlite'
|
||||||
conn = 'certs.sqlite'
|
conn = 'certs.sqlite'
|
||||||
|
|
||||||
[ACME]
|
[ACME]
|
||||||
email = ''
|
email = 'noreply@example.email'
|
||||||
apiEndpoint = ''
|
apiEndpoint = 'https://acme-v02.api.letsencrypt.org/directory'
|
||||||
acceptTerms = false
|
acceptTerms = false
|
||||||
useRateLimits = false
|
useRateLimits = false
|
||||||
eab_hmac = ''
|
eab_hmac = ''
|
||||||
eab_kid = ''
|
eab_kid = ''
|
||||||
dnsProvider = ''
|
dnsProvider = ''
|
||||||
accountConfigFile = ''
|
accountConfigFile = 'acme-account.json'
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -45,6 +45,7 @@ require (
|
||||||
github.com/cloudflare/cloudflare-go v0.20.0 // indirect
|
github.com/cloudflare/cloudflare-go v0.20.0 // indirect
|
||||||
github.com/cpu/goacmedns v0.1.1 // indirect
|
github.com/cpu/goacmedns v0.1.1 // indirect
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0 // 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/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/davidmz/go-pageant v1.0.2 // indirect
|
github.com/davidmz/go-pageant v1.0.2 // indirect
|
||||||
github.com/deepmap/oapi-codegen v1.6.1 // indirect
|
github.com/deepmap/oapi-codegen v1.6.1 // indirect
|
||||||
|
|
2
go.sum
2
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 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
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/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/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.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
|
|
@ -3,6 +3,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"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)
|
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)
|
listeningSSLAddress := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
|
||||||
listeningHTTPAddress := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.HttpPort)
|
listeningHTTPAddress := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.HttpPort)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue