feat: initial template (Go + Templ + HTMX + CDN Tailwind)
Some checks failed
CD / Lint / Test / Vet (push) Failing after 5s
CD / Build & Import (push) Has been skipped
CD / Deploy via GitOps (push) Has been skipped

This commit is contained in:
Mathias Bergqvist
2026-05-05 08:19:04 +02:00
parent 57c1e460b7
commit a25120cd1d
11 changed files with 270 additions and 2 deletions

13
.context/PROJECT.md Normal file
View File

@@ -0,0 +1,13 @@
# __PROJECT_NAME__
## Identity
- **Name**: __PROJECT_NAME__
- **Owner**: Mathias
- **Client**: personal
- **Repo**: gitea.d-ma.be/mathias/__PROJECT_NAME__
- **Status**: active
## Stack
Go + Templ + HTMX + CDN Tailwind. See `~/dev/.context/AGENT.md` for cross-project conventions.

116
.gitea/workflows/cd.yml Normal file
View File

@@ -0,0 +1,116 @@
name: CD
on:
push:
branches: [main]
tags: ["v*"]
pull_request:
branches: [main]
env:
IMAGE: __PROJECT_NAME__
jobs:
check:
name: Lint / Test / Vet
runs-on: self-hosted
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: false
- name: Install toolchain
run: |
go version
go install github.com/a-h/templ/cmd/templ@latest
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh \
| sh -s -- -b "$(go env GOPATH)/bin" v2.11.4
- name: Run checks
run: task check
build:
name: Build & Import
needs: check
runs-on: self-hosted
if: github.event_name != 'pull_request'
outputs:
image-tag: ${{ steps.meta.outputs.sha-tag }}
steps:
- uses: actions/checkout@v4
- name: Derive image tags
id: meta
run: |
SHA=$(git rev-parse --short HEAD)
echo "sha-tag=${SHA}" >> "$GITHUB_OUTPUT"
- name: Build and push to local registry
run: |
REGISTRY="localhost:5000"
REF="${REGISTRY}/${{ env.IMAGE }}:${{ steps.meta.outputs.sha-tag }}"
buildah build \
--label "org.opencontainers.image.revision=${{ github.sha }}" \
-t ${REF} \
-t ${REGISTRY}/${{ env.IMAGE }}:latest \
.
buildah push --tls-verify=false ${REF}
buildah push --tls-verify=false ${REGISTRY}/${{ env.IMAGE }}:latest
echo "✓ Image pushed to ${REF}"
deploy:
name: Deploy via GitOps
needs: build
runs-on: self-hosted
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
environment: staging
steps:
- name: Update image tag in infra repo
env:
IMAGE_TAG: ${{ needs.build.outputs.image-tag }}
DEPLOY_KEY: ${{ secrets.INFRA_DEPLOY_KEY }}
run: |
set -euo pipefail
mkdir -p ~/.ssh
echo "$DEPLOY_KEY" > ~/.ssh/id_infra
chmod 600 ~/.ssh/id_infra
ssh-keyscan -p 30022 10.0.1.20 >> ~/.ssh/known_hosts 2>/dev/null
export GIT_SSH_COMMAND="ssh -i ~/.ssh/id_infra -o IdentitiesOnly=yes"
rm -rf /tmp/infra
git clone -b main ssh://git@10.0.1.20:30022/mathias/infra.git /tmp/infra
cd /tmp/infra
DEPLOYMENT="k3s/apps/__PROJECT_NAME__/deployment.yaml"
sed -i "s|image: localhost:5000/__PROJECT_NAME__:.*|image: localhost:5000/__PROJECT_NAME__:${IMAGE_TAG}|" "$DEPLOYMENT"
grep -q "localhost:5000/__PROJECT_NAME__:${IMAGE_TAG}" "$DEPLOYMENT" \
|| { echo "✗ image tag patch failed"; exit 1; }
if git diff --quiet "$DEPLOYMENT"; then
echo " image tag unchanged — skipping push"
else
git -c user.name="__PROJECT_NAME__ CI" \
-c user.email="ci@__PROJECT_NAME__.local" \
commit -m "chore(deploy): __PROJECT_NAME__ → ${IMAGE_TAG}" "$DEPLOYMENT"
git push origin main
echo "✓ pushed to infra repo"
fi
shred -u ~/.ssh/id_infra
- name: Trigger Flux reconcile
run: |
kubectl -n flux-system annotate gitrepository flux-system \
reconcile.fluxcd.io/requestedAt="$(date +%s)" --overwrite
kubectl -n flux-system annotate kustomization apps \
reconcile.fluxcd.io/requestedAt="$(date +%s)" --overwrite
- name: Verify rollout
run: |
kubectl rollout status deployment/__PROJECT_NAME__ \
--namespace __PROJECT_NAME__ \
--timeout=120s \
|| {
kubectl get pods -n __PROJECT_NAME__ -o wide
kubectl get events -n __PROJECT_NAME__ --sort-by='.lastTimestamp' | tail -20
exit 1
}

3
.gitignore vendored
View File

@@ -25,3 +25,6 @@ go.work.sum
# env file
.env
# Project-specific
bin/
*.templ.go

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM golang:1.26-alpine AS build
WORKDIR /src
RUN apk add --no-cache git
RUN go install github.com/a-h/templ/cmd/templ@latest
COPY go.mod ./
RUN go mod download
COPY . .
RUN templ generate && CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o /out/app ./cmd/__PROJECT_NAME__
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/app"]

View File

@@ -1,3 +1,13 @@
# template-go-web
# __PROJECT_NAME__
Go + Templ + HTMX + Tailwind project template
> Generated from `mathias/template-go-web`.
## Bootstrap
After creating from template, run:
```bash
go mod tidy # regenerate go.sum with real module path
task generate # generate templ files
task build # build the binary
```

26
Taskfile.yml Normal file
View File

@@ -0,0 +1,26 @@
version: '3'
tasks:
generate:
desc: Run templ generate
cmds: [templ generate]
build:
desc: Build the binary
deps: [generate]
cmds: [go build -o bin/__PROJECT_NAME__ ./cmd/__PROJECT_NAME__]
run:
deps: [build]
cmds: [./bin/__PROJECT_NAME__]
test:
desc: Run all tests
deps: [generate]
cmds: [go test ./... -race]
lint:
cmds: [golangci-lint run ./...]
check:
desc: Lint, vet, and test (used by CI)
deps: [generate]
cmds:
- golangci-lint run ./...
- go vet ./...
- go test ./... -race -count=1

View File

@@ -0,0 +1,27 @@
package main
import (
"log/slog"
"net/http"
"os"
"__MODULE_PATH__/internal/web"
)
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.Handle("/", web.NewHandler())
addr := ":8080"
logger.Info("__PROJECT_NAME__ starting", "addr", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
logger.Error("server stopped", "err", err)
os.Exit(1)
}
}

7
go.mod Normal file
View File

@@ -0,0 +1,7 @@
module __MODULE_PATH__
go 1.26
require (
github.com/a-h/templ v0.2.778
)

17
internal/web/handler.go Normal file
View File

@@ -0,0 +1,17 @@
package web
import (
"context"
"net/http"
)
func NewHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_ = Index().Render(context.Background(), w)
})
mux.HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
_ = Hello("world").Render(context.Background(), w)
})
return mux
}

16
internal/web/index.templ Normal file
View File

@@ -0,0 +1,16 @@
package web
templ Index() {
@Layout("__PROJECT_NAME__") {
<h1 class="text-3xl font-semibold mb-6">__PROJECT_NAME__</h1>
<button hx-get="/api/hello" hx-target="#out"
class="px-4 py-2 bg-slate-900 text-white rounded-md hover:bg-slate-700">
Say hello
</button>
<div id="out" class="mt-6 text-slate-700"></div>
}
}
templ Hello(name string) {
<p>Hello, { name }!</p>
}

19
internal/web/layout.templ Normal file
View File

@@ -0,0 +1,19 @@
package web
templ Layout(title string) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<title>{ title }</title>
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="min-h-screen bg-slate-50 text-slate-900 antialiased">
<main class="max-w-3xl mx-auto px-6 py-12">
{ children... }
</main>
</body>
</html>
}