all repos — homestead @ de8e02d2a10ce0a01817729e47f31e7b37103af2

Code for my website

split options of website/server

Alan Pearce
commit

de8e02d2a10ce0a01817729e47f31e7b37103af2

parent

5434a11ad1bf6693705a9ba38ec9464f2d001da9

1 file changed, 7 insertions(+), 80 deletions(-)

changed files
M cmd/server/main.gocmd/server/main.go
@@ -3,15 +3,9 @@
import ( "context" "fmt" - "net" - "net/url" "os" "os/signal" - "slices" - "strconv" - "strings" - "go.alanpearce.eu/website/internal/config" "go.alanpearce.eu/website/internal/server" "go.alanpearce.eu/website/internal/website" "go.alanpearce.eu/x/log"
@@ -21,17 +15,8 @@ "gitlab.com/tozd/go/errors"
) type Options struct { - Development bool `conf:"default:false,flag:dev"` - Source string `conf:"default:../website"` - Destination string `conf:"default:public"` - Redirect bool `conf:"default:true"` - Port int `conf:"default:8080,short:p"` - TLS bool `conf:"default:false"` - TLSPort int `conf:"default:8443"` - ListenAddress string `conf:"default:localhost"` - Domains string `conf:"default:localhost"` - ACMEIssuer string - ACMEIssuerCert string + Website *website.Options + Server *server.Options } func main() {
@@ -44,60 +29,16 @@ os.Exit(1)
} panic("parsing runtime configuration" + err.Error()) } - log := log.Configure(!options.Development) + log := log.Configure(!options.Website.Development) - cfg, err := config.GetConfig(options.Source, log) - if err != nil { - log.Fatal("could not read config", "error", err) - } - - // Domains? - webOpts := &website.Options{ - Source: options.Source, - Destination: options.Destination, - Redirect: options.Redirect, - Development: options.Development, - Config: cfg, - } - - if options.Development { - tmpdir, err := os.MkdirTemp("", "website") - if err != nil { - log.Fatal("could not create temporary directory", "error", err) - } - log.Info("using temporary directory", "dir", tmpdir) - defer os.RemoveAll(tmpdir) - webOpts.Destination = tmpdir - - cfg.CSP.ScriptSrc = slices.Insert(cfg.CSP.ScriptSrc, 0, "'unsafe-inline'") - cfg.CSP.ConnectSrc = slices.Insert(cfg.CSP.ConnectSrc, 0, "'self'") - if options.Domains != "" { - cfg.Domains = strings.Split(options.Domains, ",") - } else { - cfg.Domains = []string{options.ListenAddress} - } - cfg.BaseURL = mkBaseURL(options, cfg) - } - - serverOpts := &server.Options{ - Development: options.Development, - ListenAddress: options.ListenAddress, - Port: options.Port, - TLS: options.TLS, - TLSPort: options.TLSPort, - ACMEIssuer: options.ACMEIssuer, - ACMEIssuerCert: options.ACMEIssuerCert, - Config: cfg, - } - - sv, err := server.New(serverOpts, log.Named("server")) + sv, err := server.New(options.Server, log.Named("server")) if err != nil { log.Error("could not create server", "error", err) return } - website, err := website.New(webOpts, log.Named("website")) + website, err := website.New(options.Website, log.Named("website")) if err != nil { log.Error("could not initialise website", "error", err)
@@ -105,7 +46,7 @@ return
} sv.HostApp(website.App) - if options.Redirect { + if options.Website.Redirect { sv.HostFallbackApp(website.MakeRedirectorApp()) }
@@ -122,21 +63,7 @@ }()
<-ctx.Done() log.Debug("calling stop") + website.App.Shutdown() <-sv.Stop() log.Debug("done") } - -func mkBaseURL(options *Options, cfg *config.Config) config.URL { - scheme := "http" - port := options.Port - if options.TLS { - scheme = "https" - port = options.TLSPort - } - return config.URL{ - URL: &url.URL{ - Scheme: scheme, - Host: net.JoinHostPort(cfg.Domains[0], strconv.Itoa(port)), - }, - } -}