diff options
Diffstat (limited to 'internal/server')
-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() |