diff options
39 files changed, 591 insertions, 492 deletions
diff --git a/.envrc b/.envrc index a75b02f..a506aa5 100644 --- a/.envrc +++ b/.envrc @@ -5,3 +5,5 @@ else echo 'while direnv evaluated .envrc, could not find the command "lorri" [https://github.com/nix-community/lorri]' use nix fi + +dotenv diff --git a/.gitignore b/.gitignore index 9f41698..130d516 100644 --- a/.gitignore +++ b/.gitignore @@ -25,9 +25,10 @@ vendor/ go.work # End of https://www.toptal.com/developers/gitignore/api/go -/website/ +/public/ /.pre-commit-config.yaml /result *_templ.go *_templ.txt +/.env diff --git a/ci.nix b/ci.nix new file mode 100644 index 0000000..33b514d --- /dev/null +++ b/ci.nix @@ -0,0 +1,18 @@ +{ pkgs ? ( + let + sources = import ./npins; + in + import sources.nixpkgs { } + ) +}: +pkgs.mkShell { + packages = with pkgs; [ + go + templ + hyperlink + just + + ko + flyctl + ]; +} diff --git a/cmd/build/main.go b/cmd/build/main.go index ee61a68..84de2dc 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -4,23 +4,24 @@ import ( "fmt" "os" - "website/internal/builder" - "website/internal/log" + "go.alanpearce.eu/website/internal/builder" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/x/log" "github.com/ardanlabs/conf/v3" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) func main() { - ioConfig := builder.IOConfig{} - if help, err := conf.Parse("", &ioConfig); err != nil { + ioConfig := &builder.IOConfig{} + if help, err := conf.Parse("", ioConfig); err != nil { if errors.Is(err, conf.ErrHelpWanted) { fmt.Println(help) os.Exit(1) } panic("error parsing configuration: " + err.Error()) } - log.Configure(!ioConfig.Development) + log := log.Configure(!ioConfig.Development) log.Debug("starting build process") if ioConfig.Source != "." { @@ -29,7 +30,12 @@ func main() { log.Panic("could not change to source directory") } } - _, err := builder.BuildSite(ioConfig) + cfg, err := config.GetConfig(ioConfig.Source, log) + if err != nil { + log.Error("could not read config", "error", err) + } + + _, err = builder.BuildSite(ioConfig, cfg, log) if err != nil { log.Error("could not build site", "error", err) os.Exit(1) diff --git a/cmd/cspgenerator/cspgenerator.go b/cmd/cspgenerator/cspgenerator.go index 89d2718..0252d32 100644 --- a/cmd/cspgenerator/cspgenerator.go +++ b/cmd/cspgenerator/cspgenerator.go @@ -1,13 +1,12 @@ package main import ( - "website/internal/config" - "website/internal/log" + "go.alanpearce.eu/website/internal/config" ) func main() { err := config.GenerateCSP() if err != nil { - log.Fatal("error generating csp", "error", err) + panic(err) } } diff --git a/cmd/server/main.go b/cmd/server/main.go index d73e19f..ca69ba2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -6,11 +6,11 @@ import ( "os" "os/signal" - "website/internal/log" - "website/internal/server" + "go.alanpearce.eu/x/log" + "go.alanpearce.eu/website/internal/server" "github.com/ardanlabs/conf/v3" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) func main() { @@ -23,16 +23,18 @@ func main() { } panic("parsing runtime configuration" + err.Error()) } - log.Configure(!runtimeConfig.Development) + log := log.Configure(!runtimeConfig.Development) - tmpdir, err := os.MkdirTemp("", "website") - if err != nil { - log.Fatal("could not create temporary directory", "error", err) + if runtimeConfig.Development { + tmpdir, err := os.MkdirTemp("", "website") + if err != nil { + log.Fatal("could not create temporary directory", "error", err) + } + defer os.RemoveAll(tmpdir) + runtimeConfig.Root = tmpdir } - defer os.RemoveAll(tmpdir) - runtimeConfig.Root = tmpdir - sv, err := server.New(&runtimeConfig) + sv, err := server.New(&runtimeConfig, log) if err != nil { log.Error("could not create server", "error", err) diff --git a/config.toml b/config.toml index db9bab6..f6bf8ed 100644 --- a/config.toml +++ b/config.toml @@ -19,6 +19,8 @@ domains = [ oidc_host = "https://id.alanpearce.eu/" +goatcounter = "https://stats.alanpearce.eu/count" + [[taxonomies]] name = "tags" feed = true @@ -38,11 +40,11 @@ oidc_host = "https://id.alanpearce.eu/" ] image-src = [ "'self'", - "https://gc.zgo.at", + "https://stats.alanpearce.eu", ] script-src = [ "'self'", - "https://gc.zgo.at", + "https://stats.alanpearce.eu", ] style-src = [ ## index.html style @@ -54,7 +56,7 @@ oidc_host = "https://id.alanpearce.eu/" "https://kagi.com", ] connect-src = [ - "https://alanpearce-eu.goatcounter.com/count", + "https://stats.alanpearce.eu/count", ] require-trusted-types-for = [ "'script'", diff --git a/fly.toml b/fly.toml index 1194b67..7508dd6 100644 --- a/fly.toml +++ b/fly.toml @@ -16,8 +16,8 @@ primary_region = "ams" TLS = "true" ROOT = "/data" PRODUCTION = "true" - LOCAL_PATH = "/data/website" - REMOTE_URL = "https://git.alanpearce.eu/website.git" + VCS_LOCAL_PATH = "/data/website" + VCS_REMOTE_URL = "https://git.alanpearce.eu/website.git" [[services]] internal_port = 8080 diff --git a/go.mod b/go.mod index d669148..05bb1fa 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module website +module go.alanpearce.eu/website -go 1.22.1 +go 1.22.3 require ( github.com/BurntSushi/toml v1.4.0 @@ -22,14 +22,11 @@ require ( github.com/kevinpollet/nego v0.0.0-20211010160919-a65cd48cee43 github.com/osdevisnot/sorvor v0.4.4 github.com/pberkel/caddy-storage-redis v1.2.0 - github.com/pkg/errors v0.9.1 github.com/snabb/sitemap v1.0.4 github.com/stefanfritsch/goldmark-fences v1.0.0 - github.com/sykesm/zap-logfmt v0.0.4 - github.com/thessem/zap-prettyconsole v0.5.0 github.com/yuin/goldmark v1.7.4 - go.uber.org/zap v1.27.0 - golang.org/x/net v0.26.0 + gitlab.com/tozd/go/errors v0.8.1 + go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 ) require ( @@ -71,6 +68,7 @@ require ( github.com/miekg/dns v1.1.61 // indirect github.com/onsi/ginkgo/v2 v2.19.0 // indirect github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.54.0 // indirect @@ -84,16 +82,20 @@ require ( github.com/snabb/diagio v1.0.4 // indirect github.com/spf13/cobra v1.8.1 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/sykesm/zap-logfmt v0.0.4 // indirect + github.com/thessem/zap-prettyconsole v0.5.0 // indirect github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/zeebo/blake3 v0.2.3 // indirect go.uber.org/automaxprocs v1.5.3 // indirect go.uber.org/mock v0.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect go.uber.org/zap/exp v0.2.0 // indirect golang.org/x/crypto v0.24.0 // indirect golang.org/x/crypto/x509roots/fallback v0.0.0-20240624163532-1c7450041f58 // indirect golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect golang.org/x/mod v0.18.0 // indirect + golang.org/x/net v0.26.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.21.0 // indirect golang.org/x/term v0.21.0 // indirect @@ -104,4 +106,5 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + moul.io/zapfilter v1.7.0 // indirect ) diff --git a/go.sum b/go.sum index 9c285cb..bbafba5 100644 --- a/go.sum +++ b/go.sum @@ -34,6 +34,7 @@ github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b h1:uUXgbcPDK3KpW29o github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benpate/derp v0.31.0 h1:Vo3oQrD+eDLY/FQ4W3HUtV1Et7lkm8OEF6rJQlSd6xg= github.com/benpate/derp v0.31.0/go.mod h1:y+PJWv5VOBOnd1y4CGk/c7xVS0Pwxg9BGQE5r/SGc8w= github.com/benpate/digit v0.12.1 h1:2Dx6IJbvvIlPMKJCSG7/XQJ/Y8BDJGVanzrGfLkWq1c= @@ -107,6 +108,7 @@ github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncV github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/pprof v0.0.0-20240625030939-27f56978b8b0 h1:e+8XbKB6IMn8A4OAyZccO4pYfB3s7bt6azNIPE7AnPg= @@ -153,6 +155,7 @@ github.com/pberkel/caddy-storage-redis v1.2.0 h1:CqJ9K4z2DAHF+euR0295QjEfNhbV/HA github.com/pberkel/caddy-storage-redis v1.2.0/go.mod h1:ztw2IxbDCQ+NUrD841IvdcwiGyh1m1HgB2nBj/geS4I= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -179,6 +182,7 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= @@ -198,14 +202,18 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/sykesm/zap-logfmt v0.0.4 h1:U2WzRvmIWG1wDLCFY3sz8UeEmsdHQjHFNlIdmroVFaI= github.com/sykesm/zap-logfmt v0.0.4/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA= +github.com/tailscale/depaware v0.0.0-20210622194025-720c4b409502/go.mod h1:p9lPsd+cx33L3H9nNoecRRxPssFKUwwI50I3pZ0yT+8= github.com/thessem/zap-prettyconsole v0.5.0 h1:AOu1GGUuDkGmj4tgRPSVf0vYGzDM+6cPWjKOcmjEcQs= github.com/thessem/zap-prettyconsole v0.5.0/go.mod h1:3qfsE7y+bLOq7EQ+fMZHD3HYEp24ULFf5nhLSx6rjrE= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg= github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= @@ -215,24 +223,36 @@ github.com/zeebo/blake3 v0.2.3 h1:TFoLXsjeXqRNFxSbk35Dk4YtszE/MQQGK10BH4ptoTg= github.com/zeebo/blake3 v0.2.3/go.mod h1:mjJjZpnsyIVtVgTOSpJ9vmRE4wgDeyt2HU3qXvvKCaQ= github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo= github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= +gitlab.com/tozd/go/errors v0.8.1 h1:RfylffRAsl3PbDdHNUBEkTleTCiL/RIT+Ef8p0HRNCI= +gitlab.com/tozd/go/errors v0.8.1/go.mod h1:PvIdUMLpPwxr+KEBxghQaCMydHXGYdJQn/PhdMqYREY= +go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60 h1:a4PJum9vqboETepA81QlBXAqgC9Zeu3F9EojnGiHauA= +go.alanpearce.eu/x v0.0.0-20240630201241-61dffc8ded60/go.mod h1:GaYgUfXSlaHBvdrInLYyKDMKo2Bmx1+IIFrlnZkZW+A= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.8.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= +go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= +go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/zap v1.12.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= +go.uber.org/zap v1.20.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go.uber.org/zap/exp v0.2.0 h1:FtGenNNeCATRB3CmB/yEUnjEFeJWpB/pMcy7e2bKPYs= go.uber.org/zap/exp v0.2.0/go.mod h1:t0gqAIdh1MfKv9EwN/dLwfZnJxe9ITAZN78HEWPFWDQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= @@ -245,6 +265,9 @@ golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0J golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= @@ -252,7 +275,9 @@ golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= @@ -263,6 +288,8 @@ golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= @@ -270,9 +297,12 @@ golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -312,11 +342,15 @@ golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201211185031-d93e913c1a58/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -328,9 +362,14 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +moul.io/zapfilter v1.7.0 h1:7aFrG4N72bDH9a2BtYUuUaDS981Dxu3qybWfeqaeBDU= +moul.io/zapfilter v1.7.0/go.mod h1:M+N2s+qZiA+bzRoyKMVRxyuERijS2ovi2pnMyiOGMvc= diff --git a/internal/atom/atom.go b/internal/atom/atom.go index a5965a4..f75d18a 100644 --- a/internal/atom/atom.go +++ b/internal/atom/atom.go @@ -1,28 +1,47 @@ package atom import ( + "bytes" "encoding/xml" + "net/url" "time" - "website/internal/config" + "go.alanpearce.eu/website/internal/config" ) -func MakeTagURI(config config.Config, specific string) string { +func MakeTagURI(config *config.Config, specific string) string { return "tag:" + config.OriginalDomain + "," + config.DomainStartDate + ":" + specific } +func LinkXSL(w *bytes.Buffer, url string) error { + _, err := w.WriteString(`<?xml-stylesheet href="`) + if err != nil { + return err + } + err = xml.EscapeText(w, []byte(url)) + if err != nil { + return err + } + _, err = w.WriteString(`" type="text/xsl"?>`) + if err != nil { + return err + } + + return nil +} + type Link struct { XMLName xml.Name `xml:"link"` - Rel string `xml:"rel,attr"` - Type string `xml:"type,attr"` + Rel string `xml:"rel,attr,omitempty"` + Type string `xml:"type,attr,omitempty"` Href string `xml:"href,attr"` } -func MakeLink(url string) Link { +func MakeLink(url *url.URL) Link { return Link{ Rel: "alternate", Type: "text/html", - Href: url, + Href: url.String(), } } @@ -41,3 +60,12 @@ type FeedEntry struct { Content FeedContent `xml:"content"` Author string `xml:"author>name"` } + +type Feed struct { + XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"` + Title string `xml:"title"` + Link Link `xml:"link"` + ID string `xml:"id"` + Updated time.Time `xml:"updated"` + Entries []*FeedEntry `xml:"entry"` +} diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 4436151..b99d919 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -6,23 +6,24 @@ import ( "io" "os" "path" + "path/filepath" "slices" "time" - "website/internal/config" - "website/internal/content" - "website/internal/log" - "website/internal/sitemap" - "website/templates" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/content" + "go.alanpearce.eu/x/log" + "go.alanpearce.eu/website/internal/sitemap" + "go.alanpearce.eu/website/templates" "github.com/a-h/templ" mapset "github.com/deckarep/golang-set/v2" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type IOConfig struct { Source string `conf:"default:.,short:s,flag:src"` - Destination string `conf:"default:website,short:d,flag:dest"` + Destination string `conf:"default:public,short:d,flag:dest"` Development bool `conf:"default:false,flag:dev"` } @@ -86,30 +87,34 @@ func writerToFile(writer io.WriterTo, pathParts ...string) error { return nil } -func build(outDir string, config config.Config) (*Result, error) { +func joinSourcePath(src string) func(string) string { + return func(rel string) string { + return filepath.Join(src, rel) + } +} + +func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, error) { + outDir := ioConfig.Destination + joinSource := joinSourcePath(ioConfig.Source) log.Debug("output", "dir", outDir) r := &Result{ Hashes: make([]string, 0), } - privateDir := path.Join(outDir, "private") - if err := mkdirp(privateDir); err != nil { - return nil, errors.WithMessage(err, "could not create private directory") - } - publicDir := path.Join(outDir, "public") - if err := mkdirp(publicDir); err != nil { - return nil, errors.WithMessage(err, "could not create public directory") - } - err := copyRecursive("static", publicDir) + err := copyRecursive(joinSource("static"), outDir) if err != nil { return nil, errors.WithMessage(err, "could not copy static files") } - if err := mkdirp(publicDir, "post"); err != nil { + if err := mkdirp(outDir, "post"); err != nil { return nil, errors.WithMessage(err, "could not create post output directory") } log.Debug("reading posts") - posts, tags, err := content.ReadPosts("content", "post", publicDir) + posts, tags, err := content.ReadPosts(&content.Config{ + Root: joinSource("content"), + InputDir: "post", + OutputDir: outDir, + }, log.Named("content")) if err != nil { return nil, err } @@ -121,7 +126,7 @@ func build(outDir string, config config.Config) (*Result, error) { } for _, post := range posts { - if err := mkdirp(publicDir, "post", post.Basename); err != nil { + if err := mkdirp(outDir, "post", post.Basename); err != nil { return nil, errors.WithMessage(err, "could not create directory for post") } log.Debug("rendering post", "post", post.Basename) @@ -131,13 +136,13 @@ func build(outDir string, config config.Config) (*Result, error) { } } - if err := mkdirp(publicDir, "tags"); err != nil { + if err := mkdirp(outDir, "tags"); err != nil { return nil, errors.WithMessage(err, "could not create directory for tags") } log.Debug("rendering tags list") if err := renderToFile( templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), - publicDir, + outDir, "tags", "index.html", ); err != nil { @@ -152,14 +157,14 @@ func build(outDir string, config config.Config) (*Result, error) { matchingPosts = append(matchingPosts, post) } } - if err := mkdirp(publicDir, "tags", tag); err != nil { + if err := mkdirp(outDir, "tags", tag); err != nil { return nil, errors.WithMessage(err, "could not create directory") } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag if err := renderToFile( templates.TagPage(config, tag, matchingPosts, url), - publicDir, + outDir, "tags", tag, "index.html", @@ -178,13 +183,13 @@ func build(outDir string, config config.Config) (*Result, error) { if err != nil { return nil, errors.WithMessage(err, "could not render tag feed page") } - if err := outputToFile(feed, publicDir, "tags", tag, "atom.xml"); err != nil { + if err := writerToFile(feed, outDir, "tags", tag, "atom.xml"); err != nil { return nil, err } } log.Debug("rendering list page") - if err := renderToFile(templates.ListPage(config, posts, "/post"), publicDir, "post", "index.html"); err != nil { + if err := renderToFile(templates.ListPage(config, posts, "/post"), outDir, "post", "index.html"); err != nil { return nil, err } sitemap.AddPath("/post/", lastMod) @@ -194,16 +199,16 @@ func build(outDir string, config config.Config) (*Result, error) { if err != nil { return nil, errors.WithMessage(err, "could not render feed") } - if err := outputToFile(feed, publicDir, "atom.xml"); err != nil { + if err := writerToFile(feed, outDir, "atom.xml"); err != nil { return nil, err } log.Debug("rendering feed styles") - feedStyles, err := renderFeedStyles() + feedStyles, err := renderFeedStyles(ioConfig.Source) if err != nil { return nil, errors.WithMessage(err, "could not render feed styles") } - if err := outputToFile(feedStyles, publicDir, "feed-styles.xsl"); err != nil { + if err := outputToFile(feedStyles, outDir, "feed-styles.xsl"); err != nil { return nil, err } _, err = feedStyles.Seek(0, 0) @@ -217,7 +222,7 @@ func build(outDir string, config config.Config) (*Result, error) { r.Hashes = append(r.Hashes, h) log.Debug("rendering homepage") - _, text, err := content.GetPost(path.Join("content", "index.md")) + _, text, err := content.GetPost(joinSource(filepath.Join("content", "index.md"))) if err != nil { return nil, err } @@ -225,43 +230,42 @@ func build(outDir string, config config.Config) (*Result, error) { if err != nil { return nil, err } - if err := renderToFile(templates.Homepage(config, posts, content), publicDir, "index.html"); err != nil { + if err := renderToFile(templates.Homepage(config, posts, content), outDir, "index.html"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer sitemap.AddPath("/", time.Time{}) - h, _ = getHTMLStyleHash(publicDir, "index.html") + h, _ = getHTMLStyleHash(outDir, "index.html") r.Hashes = append(r.Hashes, h) log.Debug("rendering sitemap") - if err := writerToFile(sitemap, publicDir, "sitemap.xml"); err != nil { + if err := writerToFile(sitemap, outDir, "sitemap.xml"); err != nil { return nil, err } log.Debug("rendering robots.txt") - rob, err := renderRobotsTXT(config) + rob, err := renderRobotsTXT(ioConfig.Source, config) if err != nil { return nil, err } - if err := outputToFile(rob, publicDir, "robots.txt"); err != nil { + if err := outputToFile(rob, outDir, "robots.txt"); err != nil { return nil, err } return r, nil } -func BuildSite(ioConfig IOConfig) (*Result, error) { - config, err := config.GetConfig() - if err != nil { - return nil, errors.WithMessage(err, "could not get config") +func BuildSite(ioConfig *IOConfig, cfg *config.Config, log *log.Logger) (*Result, error) { + if cfg == nil { + return nil, errors.New("config is nil") } - config.InjectLiveReload = ioConfig.Development + cfg.InjectLiveReload = ioConfig.Development compressFiles = !ioConfig.Development templates.Setup() - loadCSS() + loadCSS(ioConfig.Source) - return build(ioConfig.Destination, *config) + return build(ioConfig, cfg, log) } diff --git a/internal/builder/template.go b/internal/builder/template.go index da45fb7..9f019df 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -1,53 +1,41 @@ package builder import ( + "bytes" "encoding/xml" "io" "os" "path/filepath" "strings" "text/template" - "website/internal/atom" - "website/internal/config" - "website/internal/content" + + "go.alanpearce.eu/website/internal/atom" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/content" "github.com/PuerkitoBio/goquery" "github.com/antchfx/xmlquery" "github.com/antchfx/xpath" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) var ( - css string - templateFiles = make(map[string]*os.File) - nsMap = map[string]string{ + css string + nsMap = map[string]string{ "xsl": "http://www.w3.org/1999/XSL/Transform", "atom": "http://www.w3.org/2005/Atom", "xhtml": "http://www.w3.org/1999/xhtml", } ) -func loadCSS() { - bytes, err := os.ReadFile("templates/style.css") +func loadCSS(source string) { + bytes, err := os.ReadFile(filepath.Join(source, "templates/style.css")) if err != nil { panic(err) } css = string(bytes) } -func loadTemplate(path string) (file *os.File, err error) { - if templateFiles[path] == nil { - file, err = os.OpenFile(path, os.O_RDONLY, 0) - if err != nil { - return nil, errors.Wrapf(err, "could not load template at path %s", path) - } - templateFiles[path] = file - } - file = templateFiles[path] - - return -} - type QuerySelection struct { *goquery.Selection } @@ -66,9 +54,9 @@ func (q *QueryDocument) Find(selector string) *QuerySelection { return &QuerySelection{q.Document.Find(selector)} } -func renderRobotsTXT(config config.Config) (io.Reader, error) { +func renderRobotsTXT(source string, config *config.Config) (io.Reader, error) { r, w := io.Pipe() - tpl, err := template.ParseFiles("templates/robots.tmpl") + tpl, err := template.ParseFiles(filepath.Join(source, "templates/robots.tmpl")) if err != nil { return nil, err } @@ -87,41 +75,30 @@ func renderRobotsTXT(config config.Config) (io.Reader, error) { func renderFeed( title string, - config config.Config, + config *config.Config, posts []content.Post, specific string, -) (io.Reader, error) { - reader, err := loadTemplate("templates/feed.xml") +) (io.WriterTo, error) { + buf := &bytes.Buffer{} + datetime := posts[0].Date.UTC() + + buf.WriteString(xml.Header) + err := atom.LinkXSL(buf, "/feed-styles.xsl") if err != nil { return nil, err } - defer func() { - _, err := reader.Seek(0, io.SeekStart) - if err != nil { - panic("could not reset reader: " + err.Error()) - } - }() - doc, err := xmlquery.Parse(reader) - if err != nil { - return nil, errors.Wrap(err, "could not parse XML") - } - feed := doc.SelectElement("feed") - feed.SelectElement("title").FirstChild.Data = title - feed.SelectElement("link").SetAttr("href", config.BaseURL.String()) - feed.SelectElement("id").FirstChild.Data = atom.MakeTagURI(config, specific) - datetime, err := posts[0].Date.UTC().MarshalText() - if err != nil { - return nil, errors.Wrap(err, "could not convert post date to text") + feed := &atom.Feed{ + Title: title, + Link: atom.MakeLink(config.BaseURL.URL), + ID: atom.MakeTagURI(config, specific), + Updated: datetime, + Entries: make([]*atom.FeedEntry, len(posts)), } - feed.SelectElement("updated").FirstChild.Data = string(datetime) - tpl := feed.SelectElement("entry") - xmlquery.RemoveFromTree(tpl) - for _, post := range posts { - fullURL := config.BaseURL.JoinPath(post.URL).String() - text, err := xml.MarshalIndent(&atom.FeedEntry{ + for i, post := range posts { + feed.Entries[i] = &atom.FeedEntry{ Title: post.Title, - Link: atom.MakeLink(fullURL), + Link: atom.MakeLink(config.BaseURL.JoinPath(post.URL)), ID: atom.MakeTagURI(config, post.Basename), Updated: post.Date.UTC(), Summary: post.Description, @@ -130,31 +107,19 @@ func renderFeed( Content: post.Content, Type: "html", }, - }, " ", " ") - if err != nil { - return nil, errors.Wrap(err, "could not marshal xml") - } - entry, err := xmlquery.ParseWithOptions( - strings.NewReader(string(text)), - xmlquery.ParserOptions{ - Decoder: &xmlquery.DecoderOptions{ - Strict: false, - AutoClose: xml.HTMLAutoClose, - Entity: xml.HTMLEntity, - }, - }, - ) - if err != nil { - return nil, errors.Wrap(err, "could not parse XML") } - xmlquery.AddChild(feed, entry.SelectElement("entry")) + } + enc := xml.NewEncoder(buf) + err = enc.Encode(feed) + if err != nil { + return nil, err } - return strings.NewReader(doc.OutputXML(true)), nil + return buf, nil } -func renderFeedStyles() (*strings.Reader, error) { - tpl, err := template.ParseFiles("templates/feed-styles.xsl") +func renderFeedStyles(source string) (*strings.Reader, error) { + tpl, err := template.ParseFiles(filepath.Join(source, "templates/feed-styles.xsl")) if err != nil { return nil, err } @@ -169,6 +134,9 @@ func renderFeedStyles() (*strings.Reader, error) { err = tpl.Execute(w, map[string]interface{}{ "css": esc.String(), }) + if err != nil { + return nil, err + } return strings.NewReader(w.String()), nil } diff --git a/internal/config/config.go b/internal/config/config.go index 7d43462..47d5de8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,10 +3,12 @@ package config import ( "io/fs" "net/url" - "website/internal/log" + "path/filepath" + + "go.alanpearce.eu/x/log" "github.com/BurntSushi/toml" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type Taxonomy struct { @@ -38,6 +40,7 @@ type Config struct { Description string DomainStartDate string `toml:"domain_start_date"` OriginalDomain string `toml:"original_domain"` + GoatCounter URL `toml:"goatcounter"` Domains []string OIDCHost URL `toml:"oidc_host"` Taxonomies []Taxonomy @@ -48,10 +51,11 @@ type Config struct { Menus map[string][]MenuItem } -func GetConfig() (*Config, error) { +func GetConfig(dir string, log *log.Logger) (*Config, error) { config := Config{} - log.Debug("reading config.toml") - _, err := toml.DecodeFile("config.toml", &config) + filename := filepath.Join(dir, "config.toml") + log.Debug("reading config", "filename", filename) + _, err := toml.DecodeFile(filename, &config) if err != nil { switch t := err.(type) { case *fs.PathError: diff --git a/internal/config/cspgenerator.go b/internal/config/cspgenerator.go index 40eca01..9974819 100644 --- a/internal/config/cspgenerator.go +++ b/internal/config/cspgenerator.go @@ -9,7 +9,7 @@ import ( "github.com/crewjam/csp" "github.com/fatih/structtag" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) func GenerateCSP() error { diff --git a/internal/content/posts.go b/internal/content/posts.go index 5185d06..f4c6c76 100644 --- a/internal/content/posts.go +++ b/internal/content/posts.go @@ -8,15 +8,16 @@ import ( "slices" "strings" "time" - "website/internal/log" + + "go.alanpearce.eu/x/log" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2" - "github.com/pkg/errors" fences "github.com/stefanfritsch/goldmark-fences" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" htmlrenderer "github.com/yuin/goldmark/renderer/html" + "gitlab.com/tozd/go/errors" ) type PostMatter struct { @@ -79,16 +80,22 @@ func RenderMarkdown(content []byte) (string, error) { return buf.String(), nil } -func ReadPosts(root string, inputDir string, outputDir string) ([]Post, Tags, error) { +type Config struct { + Root string + InputDir string + OutputDir string +} + +func ReadPosts(config *Config, log *log.Logger) ([]Post, Tags, error) { tags := mapset.NewSet[string]() posts := []Post{} - subdir := filepath.Join(root, inputDir) + subdir := filepath.Join(config.Root, config.InputDir) files, err := os.ReadDir(subdir) if err != nil { return nil, nil, errors.WithMessagef(err, "could not read post directory %s", subdir) } - outputReplacer := strings.NewReplacer(root, outputDir, ".md", "/index.html") - urlReplacer := strings.NewReplacer(root, "", ".md", "/") + outputReplacer := strings.NewReplacer(config.Root, config.OutputDir, ".md", "/index.html") + urlReplacer := strings.NewReplacer(config.Root, "", ".md", "/") for _, f := range files { pathFromRoot := filepath.Join(subdir, f.Name()) if !f.IsDir() && path.Ext(pathFromRoot) == ".md" { diff --git a/internal/listenfd/listenfd.go b/internal/listenfd/listenfd.go index 7d020b0..5287898 100644 --- a/internal/listenfd/listenfd.go +++ b/internal/listenfd/listenfd.go @@ -1,16 +1,50 @@ package listenfd import ( + "crypto/tls" "net" "os" "strconv" - "github.com/pkg/errors" + "go.alanpearce.eu/x/log" + + "gitlab.com/tozd/go/errors" ) const fdStart = 3 -func GetListener(i uint64) (net.Listener, error) { +func GetListener(i uint64, addr string, log *log.Logger) (l net.Listener, err error) { + l, err = getFDSocket(i) + if err != nil { + log.Warn("could not create listener from listenfd", "error", err) + } + + log.Debug("listener from listenfd?", "passed", l != nil) + if l == nil { + l, err = net.Listen("tcp", addr) + if err != nil { + return nil, errors.Wrap(err, "could not create listener") + } + } + + return +} + +func GetListenerTLS( + i uint64, + addr string, + config *tls.Config, + log *log.Logger, +) (l net.Listener, err error) { + l, err = GetListener(i, addr, log) + if err != nil { + return nil, err + } + + return tls.NewListener(l, config), nil +} + +func getFDSocket(i uint64) (net.Listener, error) { lfds, present := os.LookupEnv("LISTEN_FDS") if !present { return nil, nil diff --git a/internal/log/log.go b/internal/log/log.go deleted file mode 100644 index a0f5ba7..0000000 --- a/internal/log/log.go +++ /dev/null @@ -1,63 +0,0 @@ -package log - -import ( - "os" - - zaplogfmt "github.com/sykesm/zap-logfmt" - prettyconsole "github.com/thessem/zap-prettyconsole" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -var logger *zap.SugaredLogger - -func DPanic(msg string, rest ...any) { - logger.DPanicw(msg, rest...) -} -func Debug(msg string, rest ...any) { - logger.Debugw(msg, rest...) -} -func Info(msg string, rest ...any) { - logger.Infow(msg, rest...) -} -func Warn(msg string, rest ...any) { - logger.Warnw(msg, rest...) -} -func Error(msg string, rest ...any) { - logger.Errorw(msg, rest...) -} -func Panic(msg string, rest ...any) { - logger.Panicw(msg, rest...) -} -func Fatal(msg string, rest ...any) { - logger.Fatalw(msg, rest...) -} - -func getLevelFromEnv() (zapcore.Level, error) { - if str, found := os.LookupEnv("LOG_LEVEL"); found { - l, err := zap.ParseAtomicLevel(str) - - return l.Level(), err - } - - return zap.InfoLevel, nil -} - -func Configure(isProduction bool) { - var l *zap.Logger - level, err := getLevelFromEnv() - if err != nil { - panic(err) - } - if isProduction { - cfg := zap.NewProductionEncoderConfig() - cfg.TimeKey = "" - l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level)) - } else { - cfg := prettyconsole.NewConfig() - cfg.EncoderConfig.TimeKey = "" - cfg.Level.SetLevel(level) - l = zap.Must(cfg.Build()) - } - logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar() -} diff --git a/internal/server/dev.go b/internal/server/dev.go index aac869d..6fcc93e 100644 --- a/internal/server/dev.go +++ b/internal/server/dev.go @@ -3,16 +3,16 @@ package server import ( "fmt" "io/fs" - "log/slog" "os" "path" "path/filepath" "slices" "time" - "website/internal/log" + + "go.alanpearce.eu/x/log" "github.com/fsnotify/fsnotify" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type FileWatcher struct { @@ -20,6 +20,7 @@ type FileWatcher struct { } var ( + l *log.Logger ignores = []string{ "*.templ", "*.go", @@ -31,7 +32,7 @@ func matches(name string) func(string) bool { return func(pattern string) bool { matched, err := path.Match(pattern, name) if err != nil { - log.Warn("error checking watcher ignores", "error", err) + l.Warn("error checking watcher ignores", "error", err) } return matched @@ -42,23 +43,24 @@ func ignored(pathname string) bool { return slices.ContainsFunc(ignores, matches(path.Base(pathname))) } -func NewFileWatcher() (*FileWatcher, error) { +func NewFileWatcher(log *log.Logger) (*FileWatcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { return nil, errors.WithMessage(err, "could not create watcher") } + l = log return &FileWatcher{watcher}, nil } func (watcher FileWatcher) AddRecursive(from string) error { - log.Debug("walking directory tree", "root", from) + l.Debug("walking directory tree", "root", from) err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error { if err != nil { return errors.WithMessagef(err, "could not walk directory %s", path) } if entry.IsDir() { - log.Debug("adding directory to watcher", "path", path) + l.Debug("adding directory to watcher", "path", path) if err = watcher.Add(path); err != nil { return errors.WithMessagef(err, "could not add directory %s to watcher", path) } @@ -76,17 +78,17 @@ func (watcher FileWatcher) Start(callback func(string)) { select { case event := <-watcher.Events: if !ignored(event.Name) { - log.Debug("watcher event", "name", event.Name, "op", event.Op.String()) + l.Debug("watcher event", "name", event.Name, "op", event.Op.String()) if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) { f, err := os.Stat(event.Name) if err != nil { - slog.Error( + l.Error( fmt.Sprintf("error handling %s event: %v", event.Op.String(), err), ) } else if f.IsDir() { err = watcher.Add(event.Name) if err != nil { - slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err)) + l.Error(fmt.Sprintf("error adding new folder to watcher: %v", err)) } } } @@ -101,7 +103,7 @@ func (watcher FileWatcher) Start(callback func(string)) { } } case err := <-watcher.Errors: - slog.Error(fmt.Sprintf("error in watcher: %v", err)) + l.Error("error in watcher", "error", err) } } } diff --git a/internal/server/logging.go b/internal/server/logging.go index 5d607bb..f744931 100644 --- a/internal/server/logging.go +++ b/internal/server/logging.go @@ -2,7 +2,8 @@ package server import ( "net/http" - "website/internal/log" + + "go.alanpearce.eu/x/log" ) type LoggingResponseWriter struct { @@ -22,20 +23,18 @@ func NewLoggingResponseWriter(w http.ResponseWriter) *LoggingResponseWriter { return &LoggingResponseWriter{w, http.StatusOK} } -func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler { +func wrapHandlerWithLogging(wrappedHandler http.Handler, log *log.Logger) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - host := r.Host lw := NewLoggingResponseWriter(w) wrappedHandler.ServeHTTP(lw, r) if r.URL.Path == "/health" { return } - statusCode := lw.statusCode log.Info( "http request", "method", r.Method, - "status", statusCode, - "host", host, + "status", lw.statusCode, + "host", r.Host, "path", r.URL.Path, "location", lw.Header().Get("Location"), ) diff --git a/internal/server/mime.go b/internal/server/mime.go index 696a0ad..cb1b1cf 100644 --- a/internal/server/mime.go +++ b/internal/server/mime.go @@ -2,21 +2,18 @@ package server import ( "mime" - "website/internal/log" + + "go.alanpearce.eu/x/log" ) var newMIMEs = map[string]string{ ".xsl": "text/xsl", } -func fixupMIMETypes() { +func fixupMIMETypes(log *log.Logger) { for ext, newType := range newMIMEs { if err := mime.AddExtensionType(ext, newType); err != nil { log.Error("could not update mime type", "ext", ext, "mime", newType) } } } - -func init() { - fixupMIMETypes() -} diff --git a/internal/server/server.go b/internal/server/server.go index 3110ec0..203c5c5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -7,21 +7,21 @@ import ( "net/http" "net/url" "os" + "path/filepath" "slices" "strconv" + "strings" "time" - "website/internal/builder" - cfg "website/internal/config" - "website/internal/log" - "website/internal/vcs" - "website/internal/website" + "go.alanpearce.eu/website/internal/builder" + cfg "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/x/log" + "go.alanpearce.eu/website/internal/vcs" + "go.alanpearce.eu/website/internal/website" "github.com/ardanlabs/conf/v3" "github.com/osdevisnot/sorvor/pkg/livereload" - "github.com/pkg/errors" - "golang.org/x/net/http2" - "golang.org/x/net/http2/h2c" + "gitlab.com/tozd/go/errors" ) var ( @@ -31,28 +31,45 @@ var ( ) type Config struct { - Development bool `conf:"default:false,flag:dev"` - Root string `conf:"default:website"` + Root string `conf:"default:public"` Redirect bool `conf:"default:true"` ListenAddress string `conf:"default:localhost"` - Port int `conf:"default:3000,short:p"` - TLSPort int `conf:"default:443"` + Port int `conf:"default:8080,short:p"` + TLSPort int `conf:"default:8443"` TLS bool `conf:"default:false"` + + Development bool `conf:"default:false,flag:dev"` + ACMECA string `conf:"env:ACME_CA"` + ACMECACert string `conf:"env:ACME_CA_CERT"` + Domains string } type Server struct { *http.Server - runtimeConfig *Config - config *cfg.Config + redirectServer *http.Server + runtimeConfig *Config + config *cfg.Config + log *log.Logger } -func applyDevModeOverrides(config *cfg.Config, listenAddress string) { +func applyDevModeOverrides(config *cfg.Config, runtimeConfig *Config) { config.CSP.ScriptSrc = slices.Insert(config.CSP.ScriptSrc, 0, "'unsafe-inline'") config.CSP.ConnectSrc = slices.Insert(config.CSP.ConnectSrc, 0, "'self'") + if runtimeConfig.Domains != "" { + config.Domains = strings.Split(runtimeConfig.Domains, ",") + } else { + config.Domains = []string{runtimeConfig.ListenAddress} + } + scheme := "http" + port := runtimeConfig.Port + if runtimeConfig.TLS { + scheme = "https" + port = runtimeConfig.TLSPort + } config.BaseURL = cfg.URL{ URL: &url.URL{ - Scheme: "http", - Host: listenAddress, + Scheme: scheme, + Host: net.JoinHostPort(config.Domains[0], strconv.Itoa(port)), }, } } @@ -66,19 +83,13 @@ func updateCSPHashes(config *cfg.Config, r *builder.Result) { func serverHeaderHandler(wrappedHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.ProtoMajor >= 2 && r.Header.Get("Host") != "" { - // net/http does this for HTTP/1.1, but not h2c - // TODO: check with HTTP/2.0 (i.e. with TLS) - r.Host = r.Header.Get("Host") - r.Header.Del("Host") - } w.Header().Set("Server", serverHeader) wrappedHandler.ServeHTTP(w, r) }) } -func rebuild(builderConfig builder.IOConfig, config *cfg.Config) error { - r, err := builder.BuildSite(builderConfig) +func rebuild(builderConfig *builder.IOConfig, config *cfg.Config, log *log.Logger) error { + r, err := builder.BuildSite(builderConfig, config, log.Named("builder")) if err != nil { return errors.WithMessage(err, "could not build site") } @@ -87,51 +98,57 @@ func rebuild(builderConfig builder.IOConfig, config *cfg.Config) error { return nil } -func New(runtimeConfig *Config) (*Server, error) { +func New(runtimeConfig *Config, log *log.Logger) (*Server, error) { + builderConfig := &builder.IOConfig{ + Destination: runtimeConfig.Root, + Development: runtimeConfig.Development, + } + if !runtimeConfig.Development { vcsConfig := &vcs.Config{} - _, err := conf.Parse("", vcsConfig) + _, err := conf.Parse("VCS", vcsConfig) if err != nil { return nil, err } - _, err = vcs.CloneOrUpdate(vcsConfig) + _, err = vcs.CloneOrUpdate(vcsConfig, log.Named("vcs")) if err != nil { return nil, err } - err = os.Chdir(vcsConfig.LocalPath) + err = os.Chdir(runtimeConfig.Root) if err != nil { return nil, err } - runtimeConfig.Root = "website" + + builderConfig.Source = vcsConfig.LocalPath + + publicDir := filepath.Join(runtimeConfig.Root, "public") + builderConfig.Destination = publicDir + runtimeConfig.Root = publicDir } - config, err := cfg.GetConfig() + config, err := cfg.GetConfig(builderConfig.Source, log.Named("config")) if err != nil { return nil, errors.WithMessage(err, "error parsing configuration file") } if runtimeConfig.Development { - applyDevModeOverrides(config, runtimeConfig.ListenAddress) + applyDevModeOverrides(config, runtimeConfig) } listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, strconv.Itoa(runtimeConfig.Port)) top := http.NewServeMux() - builderConfig := builder.IOConfig{ - Source: "content", - Destination: runtimeConfig.Root, - Development: runtimeConfig.Development, - } - - err = rebuild(builderConfig, config) + err = rebuild(builderConfig, config, log) if err != nil { return nil, err } + fixupMIMETypes(log) + if runtimeConfig.Development { liveReload := livereload.New() top.Handle("/_/reload", liveReload) liveReload.Start() - fw, err := NewFileWatcher() + fw, err := NewFileWatcher(log.Named("watcher")) if err != nil { return nil, errors.WithMessage(err, "could not create file watcher") } @@ -151,7 +168,7 @@ func New(runtimeConfig *Config) (*Server, error) { } go fw.Start(func(filename string) { log.Info("rebuilding site", "changed_file", filename) - err := rebuild(builderConfig, config) + err := rebuild(builderConfig, config, log) if err != nil { log.Error("error rebuilding site", "error", err) } @@ -159,24 +176,27 @@ func New(runtimeConfig *Config) (*Server, error) { } loggingMux := http.NewServeMux() - mux, err := website.NewMux(config, runtimeConfig.Root) + mux, err := website.NewMux(config, runtimeConfig.Root, log.Named("website")) if err != nil { return nil, errors.Wrap(err, "could not create website mux") } + rMux := http.NewServeMux() + rMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + path, _ := website.CanonicalisePath(r.URL.Path) + newURL := config.BaseURL.JoinPath(path) + http.Redirect(w, r, newURL.String(), 301) + }) if runtimeConfig.Redirect { loggingMux.Handle(config.BaseURL.Hostname()+"/", mux) - loggingMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - newURL := config.BaseURL.JoinPath(r.URL.String()) - http.Redirect(w, r, newURL.String(), 301) - }) + loggingMux.Handle("/", rMux) } else { loggingMux.Handle("/", mux) } top.Handle("/", serverHeaderHandler( - wrapHandlerWithLogging(loggingMux), + wrapHandlerWithLogging(loggingMux, log), ), ) @@ -186,15 +206,22 @@ func New(runtimeConfig *Config) (*Server, error) { return &Server{ Server: &http.Server{ + ReadHeaderTimeout: 10 * time.Second, + ReadTimeout: 1 * time.Minute, + WriteTimeout: 2 * time.Minute, + IdleTimeout: 10 * time.Minute, + Addr: listenAddress, + Handler: top, + }, + redirectServer: &http.Server{ + ReadHeaderTimeout: 10 * time.Second, + ReadTimeout: 1 * time.Minute, + WriteTimeout: 2 * time.Minute, + IdleTimeout: 10 * time.Minute, Addr: listenAddress, - ReadHeaderTimeout: 1 * time.Minute, - Handler: http.MaxBytesHandler(h2c.NewHandler( - top, - &http2.Server{ - IdleTimeout: 15 * time.Minute, - }, - ), 0), + Handler: rMux, }, + log: log, config: config, runtimeConfig: runtimeConfig, }, nil @@ -217,19 +244,19 @@ func (s *Server) Start() error { } func (s *Server) Stop() chan struct{} { - log.Debug("stop called") + s.log.Debug("stop called") idleConnsClosed := make(chan struct{}) go func() { - log.Debug("shutting down server") + s.log.Debug("shutting down server") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err := s.Server.Shutdown(ctx) - log.Debug("server shut down") + s.log.Debug("server shut down") if err != nil { // Error from closing listeners, or context timeout: - log.Warn("HTTP server Shutdown", "error", err) + s.log.Warn("HTTP server Shutdown", "error", err) } close(idleConnsClosed) }() diff --git a/internal/server/tcp.go b/internal/server/tcp.go index 4dc3314..12fdeb2 100644 --- a/internal/server/tcp.go +++ b/internal/server/tcp.go @@ -1,26 +1,13 @@ package server import ( - "net" - - "website/internal/listenfd" - "website/internal/log" - - "github.com/pkg/errors" + "go.alanpearce.eu/website/internal/listenfd" ) func (s *Server) serveTCP() error { - l, err := listenfd.GetListener(0) + l, err := listenfd.GetListener(0, s.Addr, s.log.Named("tcp.listenfd")) if err != nil { - log.Warn("could not create listener from listenfd", "error", err) - } - - log.Debug("listener from listenfd?", "passed", l != nil) - if l == nil { - l, err = net.Listen("tcp", s.Addr) - if err != nil { - return errors.Wrap(err, "could not create listener") - } + return err } return s.Serve(l) diff --git a/internal/server/tls.go b/internal/server/tls.go index 370134c..655455c 100644 --- a/internal/server/tls.go +++ b/internal/server/tls.go @@ -2,12 +2,18 @@ package server import ( "context" + "crypto/x509" + "net" + "net/http" + "strconv" + + "go.alanpearce.eu/website/internal/listenfd" "github.com/ardanlabs/conf/v3" "github.com/caddyserver/caddy/v2" "github.com/caddyserver/certmagic" certmagic_redis "github.com/pberkel/caddy-storage-redis" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type redisConfig struct { @@ -19,32 +25,113 @@ type redisConfig struct { } func (s *Server) serveTLS() (err error) { - rc := &redisConfig{} - _, err = conf.Parse("REDIS", rc) + var issuer *certmagic.ACMEIssuer + log := s.log.Named("tls") + + // setting cfg.Logger is too late somehow + certmagic.Default.Logger = log.GetLogger().Named("certmagic") + cfg := certmagic.NewDefault() + cfg.DefaultServerName = s.config.Domains[0] + + issuer = &certmagic.DefaultACME + certmagic.DefaultACME.Agreed = true + certmagic.DefaultACME.Email = s.config.Email + + if s.runtimeConfig.Development { + ca := s.runtimeConfig.ACMECA + if ca == "" { + return errors.New("can't enable tls in development without an ACME_CA") + } + + cp, err := x509.SystemCertPool() + if err != nil { + log.Warn("could not get system certificate pool", "error", err) + cp = x509.NewCertPool() + } + + if cacert := s.runtimeConfig.ACMECACert; cacert != "" { + cp.AppendCertsFromPEM([]byte(cacert)) + } + + // caddy's ACME server (step-ca) doesn't specify an OCSP server + cfg.OCSP.DisableStapling = true + + listenAddress := s.runtimeConfig.ListenAddress + if listenAddress[0] == '[' { + listenAddress = listenAddress[1 : len(listenAddress)-1] + } + + issuer = certmagic.NewACMEIssuer(cfg, certmagic.ACMEIssuer{ + CA: s.runtimeConfig.ACMECA, + TrustedRoots: cp, + DisableTLSALPNChallenge: true, + ListenHost: listenAddress, + AltHTTPPort: s.runtimeConfig.Port, + AltTLSALPNPort: s.runtimeConfig.TLSPort, + }) + cfg.Issuers[0] = issuer + } else { + rc := &redisConfig{} + _, err = conf.Parse("REDIS", rc) + if err != nil { + return errors.Wrap(err, "could not parse redis config") + } + + rs := certmagic_redis.New() + rs.Address = []string{rc.Address} + rs.Username = rc.Username + rs.Password = rc.Password + rs.EncryptionKey = rc.EncryptionKey + rs.KeyPrefix = rc.KeyPrefix + + cfg.Storage = rs + err = rs.Provision(caddy.Context{ + Context: context.Background(), + }) + if err != nil { + return errors.Wrap(err, "could not provision redis storage") + } + } + + ln, err := listenfd.GetListener( + 1, + net.JoinHostPort(s.runtimeConfig.ListenAddress, strconv.Itoa(s.runtimeConfig.Port)), + log.Named("listenfd"), + ) if err != nil { - return errors.Wrap(err, "could not parse redis config") + return errors.Wrap(err, "could not bind plain socket") } - rs := certmagic_redis.New() - rs.Address = []string{rc.Address} - rs.Username = rc.Username - rs.Password = rc.Password - rs.EncryptionKey = rc.EncryptionKey - rs.KeyPrefix = rc.KeyPrefix - - certmagic.Default.Storage = rs - err = rs.Provision(caddy.Context{ - Context: context.Background(), - }) + go func(ln net.Listener) { + s.redirectServer.Handler = issuer.HTTPChallengeHandler(s.redirectServer.Handler) + if err := s.redirectServer.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Error("error in http handler", "error", err) + } + }(ln) + + log.Debug( + "starting certmagic", + "http_port", + s.runtimeConfig.Port, + "https_port", + s.runtimeConfig.TLSPort, + ) + err = cfg.ManageSync(context.TODO(), s.config.Domains) if err != nil { - return errors.Wrap(err, "could not provision redis storage") + return errors.Wrap(err, "could not enable TLS") } + tlsConfig := cfg.TLSConfig() + tlsConfig.NextProtos = append([]string{"h2", "http/1.1"}, tlsConfig.NextProtos...) - certmagic.DefaultACME.Agreed = true - certmagic.DefaultACME.Email = s.config.Email - certmagic.Default.DefaultServerName = s.config.Domains[0] - certmagic.HTTPPort = s.runtimeConfig.Port - certmagic.HTTPSPort = s.runtimeConfig.TLSPort + sln, err := listenfd.GetListenerTLS( + 0, + net.JoinHostPort(s.runtimeConfig.ListenAddress, strconv.Itoa(s.runtimeConfig.TLSPort)), + tlsConfig, + log.Named("listenfd"), + ) + if err != nil { + return errors.Wrap(err, "could not bind tls socket") + } - return certmagic.HTTPS(s.config.Domains, s.Server.Handler) + return s.Serve(sln) } diff --git a/internal/sitemap/sitemap.go b/internal/sitemap/sitemap.go index a38e277..b166f73 100644 --- a/internal/sitemap/sitemap.go +++ b/internal/sitemap/sitemap.go @@ -3,7 +3,8 @@ package sitemap import ( "io" "time" - "website/internal/config" + + "go.alanpearce.eu/website/internal/config" "github.com/snabb/sitemap" ) @@ -13,9 +14,9 @@ type Sitemap struct { Sitemap *sitemap.Sitemap } -func New(cfg config.Config) *Sitemap { +func New(cfg *config.Config) *Sitemap { return &Sitemap{ - config: &cfg, + config: cfg, Sitemap: sitemap.New(), } } diff --git a/internal/vcs/repository.go b/internal/vcs/repository.go index 8ee08b2..e034ea4 100644 --- a/internal/vcs/repository.go +++ b/internal/vcs/repository.go @@ -2,11 +2,12 @@ package vcs import ( "os" - "website/internal/config" - "website/internal/log" + + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/x/log" "github.com/go-git/go-git/v5" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type Config struct { @@ -17,9 +18,10 @@ type Config struct { type Repository struct { repo *git.Repository + log *log.Logger } -func CloneOrUpdate(cfg *Config) (*Repository, error) { +func CloneOrUpdate(cfg *Config, log *log.Logger) (*Repository, error) { gr, err := git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{ URL: cfg.RemoteURL.String(), Progress: os.Stdout, @@ -34,8 +36,9 @@ func CloneOrUpdate(cfg *Config) (*Repository, error) { } repo := &Repository{ repo: gr, + log: log, } - _, err := repo.Update(cfg) + _, err := repo.Update() if err != nil { return nil, err } @@ -45,18 +48,19 @@ func CloneOrUpdate(cfg *Config) (*Repository, error) { return &Repository{ repo: gr, + log: log, }, nil } -func (r *Repository) Update(cfg *Config) (bool, error) { - log.Info("updating repository") +func (r *Repository) Update() (bool, error) { + r.log.Info("updating repository") head, err := r.repo.Head() if err != nil { return false, err } - log.Info("updating from", "rev", head.Hash().String()) + r.log.Info("updating from", "rev", head.Hash().String()) wt, err := r.repo.Worktree() if err != nil { return false, err @@ -67,7 +71,7 @@ func (r *Repository) Update(cfg *Config) (bool, error) { }) if err != nil { if errors.Is(err, git.NoErrAlreadyUpToDate) { - log.Info("already up-to-date") + r.log.Info("already up-to-date") return true, nil } @@ -79,7 +83,25 @@ func (r *Repository) Update(cfg *Config) (bool, error) { if err != nil { return false, err } - log.Info("updated to", "rev", head.Hash().String()) + r.log.Info("updated to", "rev", head.Hash().String()) + + return true, r.Clean(wt) +} + +func (r *Repository) Clean(wt *git.Worktree) error { + st, err := wt.Status() + if err != nil { + return err + } + + if !st.IsClean() { + err = wt.Clean(&git.CleanOptions{ + Dir: true, + }) + if err != nil { + return err + } + } - return true, nil + return nil } diff --git a/internal/website/filemap.go b/internal/website/filemap.go index 28dcd40..64b914f 100644 --- a/internal/website/filemap.go +++ b/internal/website/filemap.go @@ -10,9 +10,9 @@ import ( "path/filepath" "strings" - "website/internal/log" + "go.alanpearce.eu/x/log" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) type File struct { @@ -79,7 +79,7 @@ func registerFile(urlpath string, fp string) error { return nil } -func registerContentFiles(root string) error { +func registerContentFiles(root string, log *log.Logger) error { err := filepath.WalkDir(root, func(filePath string, f fs.DirEntry, err error) error { if err != nil { return errors.WithMessagef(err, "failed to access path %s", filePath) diff --git a/internal/website/mux.go b/internal/website/mux.go index fbd648e..6844551 100644 --- a/internal/website/mux.go +++ b/internal/website/mux.go @@ -3,19 +3,19 @@ package website import ( "encoding/json" "net/http" - "path" "strings" - "website/internal/config" - ihttp "website/internal/http" - "website/internal/log" - "website/templates" + + "go.alanpearce.eu/website/internal/config" + ihttp "go.alanpearce.eu/website/internal/http" + "go.alanpearce.eu/x/log" + "go.alanpearce.eu/website/templates" "github.com/benpate/digit" "github.com/kevinpollet/nego" - "github.com/pkg/errors" + "gitlab.com/tozd/go/errors" ) -func canonicalisePath(path string) (cPath string, differs bool) { +func CanonicalisePath(path string) (cPath string, differs bool) { cPath = path if strings.HasSuffix(path, "/index.html") { cPath, differs = strings.CutSuffix(path, "index.html") @@ -31,12 +31,14 @@ type webHandler func(http.ResponseWriter, *http.Request) *ihttp.Error type WrappedWebHandler struct { config *config.Config handler webHandler + log *log.Logger } -func wrapHandler(cfg *config.Config, webHandler webHandler) WrappedWebHandler { +func wrapHandler(cfg *config.Config, webHandler webHandler, log *log.Logger) WrappedWebHandler { return WrappedWebHandler{ config: cfg, handler: webHandler, + log: log, } } @@ -44,13 +46,13 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { if fail := recover(); fail != nil { w.WriteHeader(http.StatusInternalServerError) - log.Error("runtime panic!", "error", fail) + fn.log.Error("runtime panic!", "error", fail) } }() if err := fn.handler(w, r); err != nil { if strings.Contains(r.Header.Get("Accept"), "text/html") { w.WriteHeader(err.Code) - err := templates.Error(*fn.config, r.URL.Path, err).Render(r.Context(), w) + err := templates.Error(fn.config, r.URL.Path, err).Render(r.Context(), w) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -60,19 +62,18 @@ func (fn WrappedWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -func NewMux(cfg *config.Config, root string) (mux *http.ServeMux, err error) { +func NewMux(cfg *config.Config, root string, log *log.Logger) (mux *http.ServeMux, err error) { mux = &http.ServeMux{} - prefix := path.Join(root, "public") - log.Debug("registering content files", "prefix", prefix) - err = registerContentFiles(prefix) + log.Debug("registering content files", "root", root) + err = registerContentFiles(root, log) if err != nil { return nil, errors.WithMessagef(err, "registering content files") } templates.Setup() mux.Handle("/", wrapHandler(cfg, func(w http.ResponseWriter, r *http.Request) *ihttp.Error { - urlPath, shouldRedirect := canonicalisePath(r.URL.Path) + urlPath, shouldRedirect := CanonicalisePath(r.URL.Path) if shouldRedirect { http.Redirect(w, r, urlPath, 302) @@ -100,7 +101,7 @@ func NewMux(cfg *config.Config, root string) (mux *http.ServeMux, err error) { http.ServeFile(w, r, files[urlPath].alternatives[enc]) return nil - })) + }, log)) var acctResource = "acct:" + cfg.Email me := digit.NewResource(acctResource). diff --git a/justfile b/justfile index 6584e01..9c02f31 100755 --- a/justfile +++ b/justfile @@ -1,19 +1,22 @@ #!/usr/bin/env cached-nix-shell -#!nix-shell -i "just --justfile" +#!nix-shell ci.nix -i "just --justfile" -docker-registry := "registry.fly.io/alanpearce-eu" +docker_registry := "registry.fly.io/alanpearce-eu" +listen_address := env_var_or_default("LISTEN_ADDRESS", "::1") +tls_port := env_var_or_default("TLS_PORT", "8443") +port := env_var_or_default("PORT", "8080") default: @just --list --justfile {{ justfile() }} --unsorted clean: - rm -fr website + rm -fr public check-licenses: go-licenses check ./... check-links: - hyperlink website/public --sources content + hyperlink public --sources content update-all: npins update @@ -24,11 +27,11 @@ build: go run ./cmd/build dev: - modd + systemfd -s https::{{ listen_address }}:{{ tls_port }} -s http::{{ listen_address }}:{{ port }} -- modd ci: build check-links cd *DEPLOY_FLAGS: fly auth docker templ generate - fly deploy --image $(KO_DOCKER_REPO={{ docker-registry }} ko build --bare ./cmd/server) {{ DEPLOY_FLAGS }} + fly deploy --image $(KO_DOCKER_REPO={{ docker_registry }} ko build --bare ./cmd/server) {{ DEPLOY_FLAGS }} diff --git a/modd.conf b/modd.conf index f956fea..0c73ea6 100644 --- a/modd.conf +++ b/modd.conf @@ -1,5 +1,4 @@ **/*.go !**/*_templ.go { - daemon +sigint: systemfd -s http::3000 -- \ - templ generate --watch \ + daemon +sigint: templ generate --watch \ --cmd="go run ./cmd/server --dev" } diff --git a/shell.nix b/shell.nix index 118b942..4b62685 100644 --- a/shell.nix +++ b/shell.nix @@ -21,8 +21,5 @@ pkgs.mkShell { systemfd just modd - - ko - flyctl ]; } diff --git a/templates/atom.xml b/templates/atom.xml deleted file mode 100644 index 81c9a76..0000000 --- a/templates/atom.xml +++ /dev/null @@ -1,48 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<?xml-stylesheet href="/feed-styles.xsl" type="text/xsl"?> -<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="{{ lang }}"> - <title>{{ config.title }} - {%- if term %} - {{ term.name }} - {%- elif section.title %} - {{ section.title }} - {%- endif -%} - </title> - {%- if config.description %} - <subtitle>{{ config.description }}</subtitle> - {%- endif %} - <link href="{{ feed_url | safe }}" rel="self" type="application/atom+xml"/> - <link href=" - {%- if section -%} - {{ section.permalink | escape_xml | safe }} - {%- else -%} - {{ config.base_url | escape_xml | safe }} - {%- endif -%} - "/> - <generator uri="https://www.getzola.org/">Zola</generator> - <updated>{{ last_updated | date(format="%+") }}</updated> - <id>{{ feed_url | safe }}</id> - {%- for page in pages %} - <entry xml:lang="{{ page.lang }}"> - <title>{{ page.title }}</title> - <published>{{ page.date | date(format="%+") }}</published> - <updated>{{ page.updated | default(value=page.date) | date(format="%+") }}</updated> - <author> - <name> - {%- if page.authors -%} - {{ page.authors[0] }} - {%- elif config.author -%} - {{ config.author }} - {%- else -%} - Unknown - {%- endif -%} - </name> - </author> - <link rel="alternate" href="{{ page.permalink | safe }}" type="text/html"/> - <id>{{ page.permalink | safe }}</id> - {% if page.summary %} - <summary type="html">{{ page.summary }}</summary> - {% else %} - <content type="html">{{ page.content }}</content> - {% endif %} - </entry> - {%- endfor %} -</feed> diff --git a/templates/error.templ b/templates/error.templ index 2da7bef..369cb83 100644 --- a/templates/error.templ +++ b/templates/error.templ @@ -1,12 +1,12 @@ package templates import ( - "website/internal/config" - "website/internal/http" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/http" "strconv" ) -templ Error(config config.Config, path string, err *http.Error) { +templ Error(config *config.Config, path string, err *http.Error) { @Page(config, PageSettings{ Title: "Error", Path: path, diff --git a/templates/feed.xml b/templates/feed.xml deleted file mode 100644 index ddc90dd..0000000 --- a/templates/feed.xml +++ /dev/null @@ -1,24 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<?xml-stylesheet href="/feed-styles.xsl" type="text/xsl"?> -<feed xmlns="http://www.w3.org/2005/Atom"> - <title>Example Feed</title> - <link href="http://example.org/"></link> - <id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id> - <updated>2003-12-13T18:30:02Z</updated> - <entry> - <title>Atom-Powered Robots Run Amok</title> - <link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"></link> - <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id> - <updated>2003-12-13T18:30:02Z</updated> - <summary>Some text.</summary> - <content type="html"> - <div> - <p>This is the entry content.</p> - </div> - </content> - <author> - <name>John Doe</name> - </author> - </entry> - -</feed> diff --git a/templates/homepage.templ b/templates/homepage.templ index 0afbb2f..aa61c40 100644 --- a/templates/homepage.templ +++ b/templates/homepage.templ @@ -1,11 +1,11 @@ package templates import ( - "website/internal/config" - "website/internal/content" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/content" ) -templ Homepage(config config.Config, posts []content.Post, content string) { +templ Homepage(config *config.Config, posts []content.Post, content string) { @Page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/list.templ b/templates/list.templ index 602c15c..fc59677 100644 --- a/templates/list.templ +++ b/templates/list.templ @@ -1,11 +1,11 @@ package templates import ( - "website/internal/config" - "website/internal/content" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/content" ) -templ TagPage(config config.Config, tag string, posts []content.Post, path string) { +templ TagPage(config *config.Config, tag string, posts []content.Post, path string) { @Page(config, PageSettings{ Title: tag, Path: path, @@ -24,7 +24,7 @@ templ TagPage(config config.Config, tag string, posts []content.Post, path strin } } -templ ListPage(config config.Config, posts []content.Post, path string) { +templ ListPage(config *config.Config, posts []content.Post, path string) { @Page(config, PageSettings{ Title: config.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/page.templ b/templates/page.templ index 7869369..e5cb073 100644 --- a/templates/page.templ +++ b/templates/page.templ @@ -2,9 +2,8 @@ package templates import ( "io/fs" - "net/url" - "website/internal/config" + "go.alanpearce.eu/website/internal/config" ) var ( @@ -43,7 +42,7 @@ templ menuItem(item config.MenuItem) { >{ item.Name }</a> } -templ Page(site config.Config, page PageSettings) { +templ Page(site *config.Config, page PageSettings) { <!DOCTYPE html> <html lang={ site.DefaultLanguage }> <head> @@ -71,10 +70,10 @@ templ Page(site config.Config, page PageSettings) { <footer> Content is <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>. - <a href="https://git.alanpearce.eu/website/">Site source code</a> is + <a href="https://git.alanpearce.eu/go.alanpearce.eu/website/">Site source code</a> is <a href="https://opensource.org/licenses/MIT">MIT</a> </footer> - @counter(page.Path, page.Title) + @counter(site, page.Path, page.Title) if site.InjectLiveReload { <script defer> new EventSource("/_/reload").onmessage = event => { @@ -87,11 +86,7 @@ templ Page(site config.Config, page PageSettings) { </html> } -func mkURL(path string, title string) string { - u, err := url.Parse("https://alanpearce-eu.goatcounter.com/count") - if err != nil { - panic(err) - } +func mkURL(u config.URL, path string, title string) string { q := u.Query() q.Add("p", path) q.Add("t", title) @@ -100,10 +95,10 @@ func mkURL(path string, title string) string { return u.String() } -templ counter(path string, title string) { - <script data-goatcounter="https://alanpearce-eu.goatcounter.com/count" async src="https://gc.zgo.at/count.v4.js" crossorigin="anonymous" integrity="sha384-nRw6qfbWyJha9LhsOtSb2YJDyZdKvvCFh0fJYlkquSFjUxp9FVNugbfy8q1jdxI+"></script> +templ counter(config *config.Config, path string, title string) { + <script data-goatcounter={ config.GoatCounter.String() } async src="https://stats.alanpearce.eu/count.v4.js" crossorigin="anonymous" integrity="sha384-nRw6qfbWyJha9LhsOtSb2YJDyZdKvvCFh0fJYlkquSFjUxp9FVNugbfy8q1jdxI+"></script> <noscript> - <img src={ string(templ.URL(mkURL(path, title))) }/> + <img src={ string(templ.URL(mkURL(config.GoatCounter, path, title))) }/> </noscript> } diff --git a/templates/post.templ b/templates/post.templ index 7b82584..9717b4e 100644 --- a/templates/post.templ +++ b/templates/post.templ @@ -2,8 +2,8 @@ package templates import ( "time" - "website/internal/config" - "website/internal/content" + "go.alanpearce.eu/website/internal/config" + "go.alanpearce.eu/website/internal/content" ) func Unsafe(html string) templ.Component { @@ -19,7 +19,7 @@ templ postDate(d time.Time) { </time> } -templ PostPage(config config.Config, post content.Post) { +templ PostPage(config *config.Config, post content.Post) { @Page(config, PageSettings{ Title: post.Title, TitleAttrs: templ.Attributes{ diff --git a/templates/tags.templ b/templates/tags.templ index 7218ca1..c872a0d 100644 --- a/templates/tags.templ +++ b/templates/tags.templ @@ -1,12 +1,12 @@ package templates -import "website/internal/config" +import "go.alanpearce.eu/website/internal/config" templ tagLink(tag string, attrs templ.Attributes) { <a { attrs... } href={ templ.SafeURL("/tags/" + tag) }>#{ tag }</a> } -templ TagsPage(config config.Config, title string, tags []string, path string) { +templ TagsPage(config *config.Config, title string, tags []string, path string) { @Page(config, PageSettings{ Title: title, Path: path, |