diff options
author | Alan Pearce | 2024-06-25 20:11:20 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-25 20:11:20 +0200 |
commit | 2fcf88ac2e088cdb7692ccfd7fccce01dd0cd523 (patch) | |
tree | 1a10913622f35818cade49d1856f0d999ff65fbd /internal/server | |
parent | 1b734d8731f4d5ada6ca0da07e2bcf59e00f209d (diff) | |
download | website-2fcf88ac2e088cdb7692ccfd7fccce01dd0cd523.tar.lz website-2fcf88ac2e088cdb7692ccfd7fccce01dd0cd523.tar.zst website-2fcf88ac2e088cdb7692ccfd7fccce01dd0cd523.zip |
refactor server startup
Diffstat (limited to 'internal/server')
-rw-r--r-- | internal/server/server.go | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index 556c660..02202ae 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -103,6 +103,13 @@ func New(runtimeConfig *Config) (*Server, error) { 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 @@ func New(runtimeConfig *Config) (*Server, error) { 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 @@ func New(runtimeConfig *Config) (*Server, error) { 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) |