refactor server startup
Alan Pearce alan@alanpearce.eu
Tue, 25 Jun 2024 20:11:20 +0200
3 files changed, 16 insertions(+), 35 deletions(-)
M cmd/server/main.go → cmd/server/main.go
@@ -25,15 +25,6 @@ panic("parsing runtime configuration" + err.Error()) } log.Configure(!runtimeConfig.Development) - if runtimeConfig.Development { - tmpdir, err := os.MkdirTemp("", "website") - if err != nil { - log.Fatal("could not create temporary directory to build website: %v", err) - } - runtimeConfig.Root = tmpdir - defer os.RemoveAll(tmpdir) - } - sv, err := server.New(&runtimeConfig) if err != nil { log.Error("could not create server", "error", err)
M internal/builder/builder.go → internal/builder/builder.go
@@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "net/url" "os" "path" "slices" @@ -24,8 +23,7 @@ type IOConfig struct { Source string `conf:"default:.,short:s,flag:src"` Destination string `conf:"default:website,short:d,flag:dest"` - BaseURL config.URL - Development bool `conf:"default:false,flag:dev"` + Development bool `conf:"default:false,flag:dev"` } type Result struct { @@ -266,13 +264,6 @@ return nil, errors.WithMessage(err, "could not get config") } config.InjectLiveReload = ioConfig.Development compressFiles = !ioConfig.Development - - if ioConfig.BaseURL.URL != nil { - config.BaseURL.URL, err = url.Parse(ioConfig.BaseURL.String()) - if err != nil { - return nil, errors.WithMessage(err, "could not re-parse base URL") - } - } if ioConfig.Development && ioConfig.Destination != "website" { err = os.RemoveAll(ioConfig.Destination)
M internal/server/server.go → internal/server/server.go
@@ -103,6 +103,13 @@ if err != nil { return nil, err } runtimeConfig.Root = "website" + } else { + tmpdir, err := os.MkdirTemp("", "website") + if err != nil { + return nil, err + } + defer os.RemoveAll(tmpdir) + runtimeConfig.Root = tmpdir } config, err := cfg.GetConfig() @@ -113,15 +120,14 @@ listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, strconv.Itoa(runtimeConfig.Port)) top := http.NewServeMux() + builderConfig := builder.IOConfig{ + Source: "content", + Destination: runtimeConfig.Root, + Development: runtimeConfig.Development, + } + if runtimeConfig.Development { applyDevModeOverrides(config, listenAddress) - builderConfig := builder.IOConfig{ - Source: "content", - Destination: runtimeConfig.Root, - BaseURL: config.BaseURL, - Development: true, - } - rebuild(builderConfig, config) liveReload := livereload.New() top.Handle("/_/reload", liveReload) @@ -147,22 +153,15 @@ } go fw.Start(func(filename string) { rebuild(builderConfig, config) }) - } else { - builderConfig := builder.IOConfig{ - Source: "content", - Destination: runtimeConfig.Root, - BaseURL: config.BaseURL, - Development: false, - } - rebuild(builderConfig, config) } + + rebuild(builderConfig, config) loggingMux := http.NewServeMux() mux, err := website.NewMux(config, runtimeConfig.Root) if err != nil { return nil, errors.Wrap(err, "could not create website mux") } - log.Debug("binding main handler to", "host", listenAddress) if runtimeConfig.Development || runtimeConfig.Redirect { loggingMux.Handle(config.BaseURL.Hostname()+"/", mux)