all repos — homestead @ de8e02d2a10ce0a01817729e47f31e7b37103af2

Code for my website

split options of website/server

Alan Pearce
commit

de8e02d2a10ce0a01817729e47f31e7b37103af2

parent

5434a11ad1bf6693705a9ba38ec9464f2d001da9

1 file changed, 50 insertions(+), 14 deletions(-)

changed files
M internal/website/mux.gointernal/website/mux.go
@@ -4,6 +4,7 @@ import (
"encoding/json" "fmt" "net/http" + "os" "regexp" "slices" "strings"
@@ -15,6 +16,7 @@ ihttp "go.alanpearce.eu/website/internal/http"
"go.alanpearce.eu/website/internal/server" "go.alanpearce.eu/website/internal/storage" "go.alanpearce.eu/website/internal/storage/files" + "go.alanpearce.eu/website/internal/vcs" "go.alanpearce.eu/website/internal/watcher" "go.alanpearce.eu/website/templates" "go.alanpearce.eu/x/log"
@@ -25,11 +27,11 @@ "github.com/osdevisnot/sorvor/pkg/livereload"
) type Options struct { - Source string - Destination string - Redirect bool - Development bool - Config *config.Config + Source string `conf:"default:../website"` + Destination string `conf:"default:public"` + Redirect bool `conf:"default:true"` + Development bool `conf:"default:false,flag:dev"` + BaseURL config.URL `conf:"default:localhost"` } type Website struct {
@@ -79,9 +81,12 @@ func New(
opts *Options, log *log.Logger, ) (*Website, error) { + mux := http.NewServeMux() website := &Website{ - config: opts.Config, - log: log, + log: log, + App: &server.App{ + Handler: mux, + }, } builderOptions := &builder.Options{ Source: opts.Source,
@@ -89,12 +94,46 @@ Development: opts.Development,
Destination: opts.Destination, } - mux := &http.ServeMux{} templates.Setup() - cfg := opts.Config + log.Debug("getting config from source", "source", opts.Source) + cfg, err := config.GetConfig(opts.Source, log) + if err != nil { + return nil, errors.WithMessage(err, "could not load configuration") + } - err := rebuild(builderOptions, cfg, log) + if opts.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) + website.App.Shutdown = func() { + os.RemoveAll(tmpdir) + } + builderOptions.Destination = tmpdir + + cfg.CSP.ScriptSrc = slices.Insert(cfg.CSP.ScriptSrc, 0, "'unsafe-inline'") + cfg.CSP.ConnectSrc = slices.Insert(cfg.CSP.ConnectSrc, 0, "'self'") + + cfg.BaseURL = opts.BaseURL + } + + website.Domain = cfg.BaseURL.Hostname() + + repo, err := vcs.CloneOrOpen(&vcs.Options{ + LocalPath: opts.Source, + RemoteURL: cfg.VCS.RemoteURL, + Branch: cfg.VCS.Branch, + }, log.Named("vcs")) + if err != nil { + return nil, errors.WithMessage(err, "could not update repository") + } + + _ = repo + // config may have changed, reload + + err = rebuild(builderOptions, cfg, log) if err != nil { return nil, errors.WithMessage(err, "could not build site") }
@@ -199,10 +238,7 @@ u := cfg.OIDCHost.JoinPath(oidcPath)
http.Redirect(w, r, u.String(), 302) }) - website.App = &server.App{ - Domain: cfg.Domains[0], - Handler: mux, - } + website.config = cfg return website, nil }