diff options
author | Alan Pearce | 2024-06-25 17:07:47 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-25 17:07:47 +0200 |
commit | cd1125a213d742b5a89e8b60f0c7aafef13b8783 (patch) | |
tree | b3271d6883c15eea11252d76b3a7c4c6626e1ded /internal/server/server.go | |
parent | 83a4eef89b9906d8ad90411e9b256da6dab07b55 (diff) | |
download | website-cd1125a213d742b5a89e8b60f0c7aafef13b8783.tar.lz website-cd1125a213d742b5a89e8b60f0c7aafef13b8783.tar.zst website-cd1125a213d742b5a89e8b60f0c7aafef13b8783.zip |
fetch and build site on server start
Diffstat (limited to 'internal/server/server.go')
-rw-r--r-- | internal/server/server.go | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index 60b20d2..e75efd3 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -6,14 +6,17 @@ import ( "net" "net/http" "net/url" + "os" "slices" "time" "website/internal/builder" cfg "website/internal/config" "website/internal/log" + "website/internal/vcs" "website/internal/website" + "github.com/ardanlabs/conf/v3" "github.com/osdevisnot/sorvor/pkg/livereload" "github.com/pkg/errors" "golang.org/x/net/http2" @@ -72,8 +75,34 @@ func serverHeaderHandler(wrappedHandler http.Handler) http.Handler { }) } +func rebuild(builderConfig builder.IOConfig, config *cfg.Config) error { + r, err := builder.BuildSite(builderConfig) + if err != nil { + return errors.WithMessage(err, "could not build site") + } + updateCSPHashes(config, r) + + return nil +} + func New(runtimeConfig *Config) (*Server, error) { - var err error + if !runtimeConfig.Development { + vcsConfig := &vcs.Config{} + _, err := conf.Parse("", vcsConfig) + if err != nil { + return nil, err + } + _, err = vcs.CloneOrUpdate(vcsConfig) + if err != nil { + return nil, err + } + err = os.Chdir(vcsConfig.LocalPath) + if err != nil { + return nil, err + } + runtimeConfig.Root = "website" + } + config, err := cfg.GetConfig() if err != nil { return nil, errors.WithMessage(err, "error parsing configuration file") @@ -90,11 +119,7 @@ func New(runtimeConfig *Config) (*Server, error) { BaseURL: config.BaseURL, Development: true, } - r, err := builder.BuildSite(builderConfig) - if err != nil { - return nil, errors.WithMessage(err, "could not build site") - } - updateCSPHashes(config, r) + rebuild(builderConfig, config) liveReload := livereload.New() top.Handle("/_/reload", liveReload) @@ -118,13 +143,16 @@ func New(runtimeConfig *Config) (*Server, error) { return nil, errors.WithMessage(err, "could not add directory to file watcher") } go fw.Start(func(filename string) { - r, err := builder.BuildSite(builderConfig) - if err != nil { - log.Error("could not build site", "error", err) - } - updateCSPHashes(config, r) - liveReload.Reload() + rebuild(builderConfig, config) }) + } else { + builderConfig := builder.IOConfig{ + Source: "content", + Destination: runtimeConfig.Root, + BaseURL: config.BaseURL, + Development: false, + } + rebuild(builderConfig, config) } loggingMux := http.NewServeMux() |