all repos — homestead @ cde569513cdb91e6f605c4240924d12f7e5d031a

Code for my website

use treefmt-nix to format everything

Alan Pearce
commit

cde569513cdb91e6f605c4240924d12f7e5d031a

parent

bab78381a9c54b809dc76144aa9f773d387bb621

M README.mdREADME.md
@@ -3,6 +3,5 @@
## Goals 1. Static web server with prometheus-based analytics -2. Dynamic web server capable of generating Zola-based websites -3. More indieweb features - +1. Dynamic web server capable of generating Zola-based websites +1. More indieweb features
M domain/calendar/calendar.godomain/calendar/calendar.go
@@ -20,8 +20,10 @@ "alin.ovh/homestead/shared/cache"
"alin.ovh/homestead/shared/config" ) -const Filename = "calendar.ics" -const Refresh = 30 * time.Minute +const ( + Filename = "calendar.ics" + Refresh = 30 * time.Minute +) type Options struct { URL config.URL
M domain/content/builder/template/template.godomain/content/builder/template/template.go
@@ -18,13 +18,11 @@ "github.com/antchfx/xmlquery"
"github.com/antchfx/xpath" ) -var ( - 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", - } -) +var 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 RenderRobotsTXT(baseURL config.URL, w io.Writer) error { tpl, err := template.ParseFS(templates.Files, "robots.tmpl")
M domain/content/fetcher/fetcher.godomain/content/fetcher/fetcher.go
@@ -23,9 +23,11 @@ "github.com/Southclaws/fault/fmsg"
"github.com/google/renameio/v2" ) -var files = []string{"config.toml", "site.db"} -var numericFilename = regexp.MustCompile("[0-9]{3,}") -var timeout = 10 * time.Second +var ( + files = []string{"config.toml", "site.db"} + numericFilename = regexp.MustCompile("[0-9]{3,}") + timeout = 10 * time.Second +) type Fetcher struct { options *Options
M domain/content/publisher/mux.godomain/content/publisher/mux.go
@@ -12,7 +12,6 @@ func (app *App) Index(w http.ResponseWriter, _ *http.Request) ihttp.Error {
err := pubtpl.IndexPage(app.siteSettings, templates.PageSettings{ Title: "Home", }).Render(w) - if err != nil { return ihttp.InternalServerError("Failed to render index page", err) }
M domain/web/templates/files.godomain/web/templates/files.go
@@ -7,9 +7,7 @@
"alin.ovh/homestead/shared/env" ) -var ( - Files fs.FS -) +var Files fs.FS func init() { Files = os.DirFS(env.GetEnvFallback("KO_DATA_PATH", "kodata"))
M flake.lockflake.lock
@@ -76,6 +76,22 @@ "repo": "nixpkgs",
"type": "github" } }, + "nixpkgs_2": { + "locked": { + "lastModified": 1747958103, + "narHash": "sha256-qmmFCrfBwSHoWw7cVK4Aj+fns+c54EBP8cGqp/yK410=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "fe51d34885f7b5e3e7b59572796e1bcb427eccb1", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "pre-commit-hooks": { "inputs": { "flake-compat": "flake-compat",
@@ -103,6 +119,7 @@ "inputs": {
"gomod2nix": "gomod2nix", "nixpkgs": "nixpkgs", "pre-commit-hooks": "pre-commit-hooks", + "treefmt-nix": "treefmt-nix", "utils": "utils" } },
@@ -118,6 +135,24 @@ },
"original": { "owner": "nix-systems", "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1750931469, + "narHash": "sha256-0IEdQB1nS+uViQw4k3VGUXntjkDp7aAlqcxdewb/hAc=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "ac8e6f32e11e9c7f153823abc3ab007f2a65d3e1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", "type": "github" } },
M flake.nixflake.nix
@@ -7,94 +7,127 @@ inputs.gomod2nix.inputs.nixpkgs.follows = "nixpkgs";
inputs.gomod2nix.inputs.flake-utils.follows = "utils"; inputs.pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; inputs.pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs"; + inputs.treefmt-nix.url = "github:numtide/treefmt-nix"; - outputs = { self, nixpkgs, utils, pre-commit-hooks, gomod2nix, ... }: - utils.lib.eachDefaultSystem - (system: - let - pkgs = import nixpkgs { - inherit system; - overlays = [ - gomod2nix.overlays.default - ]; - }; - commonShellPackages = with pkgs; [ - go - hyperlink - just - ko - flyctl - redis - sqlc + outputs = + { + self, + nixpkgs, + utils, + pre-commit-hooks, + gomod2nix, + treefmt-nix, + ... + }: + utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + gomod2nix.overlays.default ]; - devPackages = with pkgs; [ - wgo - gopls - gotools - gomod2nix.legacyPackages.${system}.gomod2nix + }; + commonShellPackages = with pkgs; [ + go + hyperlink + just + ko + flyctl + redis + sqlc + ]; + devPackages = with pkgs; [ + wgo + gofumpt + gopls + gotools + gomod2nix.legacyPackages.${system}.gomod2nix + sqlfluff - systemfd - modd - ]; - in - { - devShells = { - ci = pkgs.mkShell { - packages = commonShellPackages; - }; - default = pkgs.mkShell { - inherit (self.checks.${system}.pre-commit-check) shellHook; - packages = devPackages ++ commonShellPackages; - }; + systemfd + modd + ]; + treefmtEval = treefmt-nix.lib.evalModule pkgs { + projectRootFile = "flake.nix"; + programs = { + nixfmt.enable = true; + taplo.enable = true; + mdformat.enable = true; + gofumpt.enable = true; + golines.enable = true; + prettier.enable = true; + sqlfluff.enable = true; + sqlfluff.dialect = "sqlite"; + }; + settings = { + taplo.excludes = [ "gomod2nix.toml" ]; + }; + }; + in + { + devShells = { + ci = pkgs.mkShell { + packages = commonShellPackages; + }; + default = pkgs.mkShell { + inherit (self.checks.${system}.pre-commit-check) shellHook; + packages = devPackages ++ commonShellPackages; }; - checks = { - pre-commit-check = pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - go-mod-tidy = { - enable = true; - name = "go-mod-tidy"; - description = "Run `go mod tidy`"; - types_or = [ "go" "go-mod" ]; - entry = "${pkgs.go}/bin/go mod tidy"; - pass_filenames = false; - }; - golines.enable = true; - golangci-lint.enable = true; - gotest.enable = true; - govet.enable = true; - gomod2nix = { - enable = true; - name = "gomod2nix"; - description = "Import go.mod updates to nix"; - types_or = [ "go-sum" ]; - entry = "${pkgs.gomod2nix}/bin/gomod2nix"; - pass_filenames = false; - }; - sqlc = { - enable = true; - name = "sqlc"; - description = "Generate go code from SQL"; - types_or = [ "sql" ]; - entry = "${pkgs.sqlc}/bin/sqlc generate"; - pass_filenames = false; - }; + }; + formatter = treefmtEval.config.build.wrapper; + checks = { + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + go-mod-tidy = { + enable = true; + name = "go-mod-tidy"; + description = "Run `go mod tidy`"; + types_or = [ + "go" + "go-mod" + ]; + entry = "${pkgs.go}/bin/go mod tidy"; + pass_filenames = false; + }; + golines.enable = true; + golangci-lint.enable = true; + gotest.enable = true; + govet.enable = true; + gomod2nix = { + enable = true; + name = "gomod2nix"; + description = "Import go.mod updates to nix"; + types_or = [ "go-sum" ]; + entry = "${pkgs.gomod2nix}/bin/gomod2nix"; + pass_filenames = false; + }; + sqlc = { + enable = true; + name = "sqlc"; + description = "Generate go code from SQL"; + types_or = [ "sql" ]; + entry = "${pkgs.sqlc}/bin/sqlc generate"; + pass_filenames = false; }; }; }; - packages = { - build = pkgs.buildGoApplication { - pname = "build"; - version = self.shortRev or self.dirtyShortRev; - src = ./.; - modules = ./gomod2nix.toml; - subPackages = [ "domain/content/builder/build" ]; - nativeBuildInputs = with pkgs; [ makeWrapper ]; + }; + packages = { + build = pkgs.buildGoApplication { + pname = "build"; + version = self.shortRev or self.dirtyShortRev; + src = ./.; + modules = ./gomod2nix.toml; + subPackages = [ "domain/content/builder/build" ]; + nativeBuildInputs = with pkgs; [ makeWrapper ]; - postInstall = '' - wrapProgram $out/bin/build --set KO_DATA_PATH ${./kodata} - ''; - }; + postInstall = '' + wrapProgram $out/bin/build --set KO_DATA_PATH ${./kodata} + ''; }; - }); + }; + } + ); }
M shared/cache/cache.goshared/cache/cache.go
@@ -7,8 +7,10 @@
"alin.ovh/homestead/shared/file" ) -var home string -var Root *os.Root +var ( + home string + Root *os.Root +) func init() { var err error
M shared/events/redis.goshared/events/redis.go
@@ -14,9 +14,11 @@ "github.com/redis/go-redis/v9"
"go.uber.org/zap" ) -const db = 0 -const key = "run_id" -const fallbackRunID uint64 = 210 +const ( + db = 0 + key = "run_id" + fallbackRunID uint64 = 210 +) type RedisOptions struct { Address string
M shared/storage/sqlite/db/query.sql.goshared/storage/sqlite/db/query.sql.go
@@ -11,11 +11,11 @@ )
const checkPath = `-- name: CheckPath :one SELECT - EXISTS( - SELECT 1 - FROM url - WHERE path = ? - ) AS differs + EXISTS( + SELECT 1 + FROM url + WHERE path = ? + ) AS differs ` func (q *Queries) CheckPath(ctx context.Context, path string) (int64, error) {
@@ -27,15 +27,15 @@ }
const getFile = `-- name: GetFile :many SELECT - file.file_id, file.url_id, file.content_type, file.last_modified, file.title, file.etag, file.style_hash, file.headers, - content.content_id, content.file_id, content.encoding, content.body + file.file_id, file.url_id, file.content_type, file.last_modified, file.title, file.etag, file.style_hash, file.headers, + content.content_id, content.file_id, content.encoding, content.body FROM url INNER JOIN file - USING (url_id) + USING (url_id) INNER JOIN content - USING (file_id) + USING (file_id) WHERE - url.path = ? + url.path = ? ` type GetFileRow struct {
@@ -96,15 +96,17 @@ return err
} const insertFile = `-- name: InsertFile :execlastid -INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title, headers) +INSERT INTO file ( + url_id, content_type, last_modified, etag, style_hash, title, headers +) VALUES ( - ?1, - ?2, - ?3, - ?4, - ?5, - ?6, - ?7 + ?1, + ?2, + ?3, + ?4, + ?5, + ?6, + ?7 ) `
M shared/storage/sqlite/query.sqlshared/storage/sqlite/query.sql
@@ -2,15 +2,17 @@ -- name: InsertURL :execlastid
INSERT INTO url (path) VALUES (?); -- name: InsertFile :execlastid -INSERT INTO file (url_id, content_type, last_modified, etag, style_hash, title, headers) +INSERT INTO file ( + url_id, content_type, last_modified, etag, style_hash, title, headers +) VALUES ( - @url_id, - @content_type, - @last_modified, - @etag, - @style_hash, - @title, - @headers + @url_id, + @content_type, + @last_modified, + @etag, + @style_hash, + @title, + @headers ); -- name: InsertContent :exec
@@ -19,20 +21,20 @@ VALUES (@fileid, @encoding, @body);
-- name: GetFile :many SELECT - sqlc.embed(file), - sqlc.embed(content) + sqlc.embed(file), + sqlc.embed(content) FROM url INNER JOIN file - USING (url_id) + USING (url_id) INNER JOIN content - USING (file_id) + USING (file_id) WHERE - url.path = ?; + url.path = ?; -- name: CheckPath :one SELECT - EXISTS( - SELECT 1 - FROM url - WHERE path = ? - ) AS differs; + EXISTS( + SELECT 1 + FROM url + WHERE path = ? + ) AS differs;
M shared/storage/sqlite/schema.sqlshared/storage/sqlite/schema.sql
@@ -1,30 +1,33 @@
CREATE TABLE IF NOT EXISTS url ( - url_id INTEGER PRIMARY KEY, - path TEXT NOT NULL + url_id INTEGER PRIMARY KEY, + path TEXT NOT NULL ) STRICT; + CREATE UNIQUE INDEX IF NOT EXISTS url_path - ON url (path); +ON url (path); CREATE TABLE IF NOT EXISTS file ( - file_id INTEGER PRIMARY KEY, - url_id INTEGER NOT NULL, - content_type TEXT NOT NULL, - last_modified INTEGER NOT NULL, - title TEXT NOT NULL, - etag TEXT NOT NULL, - style_hash TEXT NOT NULL, - headers BLOB NOT NULL, - FOREIGN KEY (url_id) REFERENCES url (url_id) + file_id INTEGER PRIMARY KEY, + url_id INTEGER NOT NULL, + content_type TEXT NOT NULL, + last_modified INTEGER NOT NULL, + title TEXT NOT NULL, + etag TEXT NOT NULL, + style_hash TEXT NOT NULL, + headers BLOB NOT NULL, + FOREIGN KEY (url_id) REFERENCES url (url_id) ) STRICT; + CREATE UNIQUE INDEX IF NOT EXISTS file_url_content_type - ON file (url_id, content_type); +ON file (url_id, content_type); CREATE TABLE IF NOT EXISTS content ( - content_id INTEGER PRIMARY KEY, - file_id INTEGER NOT NULL, - encoding TEXT NOT NULL, - body BLOB NOT NULL, - FOREIGN KEY (file_id) REFERENCES file (file_id) + content_id INTEGER PRIMARY KEY, + file_id INTEGER NOT NULL, + encoding TEXT NOT NULL, + body BLOB NOT NULL, + FOREIGN KEY (file_id) REFERENCES file (file_id) ) STRICT; + CREATE UNIQUE INDEX IF NOT EXISTS file_content - ON content (file_id, encoding); +ON content (file_id, encoding);