Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee3eba3cd9 | ||
|
|
b384c70c39 | ||
|
|
1c67eb0cff | ||
|
|
16e5107185 | ||
|
|
2b07701762 | ||
|
|
a27b4a66e6 | ||
|
|
bc900e1450 |
@@ -2,35 +2,40 @@
|
|||||||
|
|
||||||
Personal portfolio website for Schreifuchs, featuring photography, informatics, music, and video.
|
Personal portfolio website for Schreifuchs, featuring photography, informatics, music, and video.
|
||||||
|
|
||||||
The site is built with **Svelte 5** and **SvelteKit**, and it is powered by a dynamic **Nextcloud** backend via WebDAV.
|
The site is built with **Go**, **templ**, and **HTMX**, and it is powered by a dynamic **Nextcloud** backend via WebDAV.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Dynamic Content:** Pages are automatically discovered based on the folder structure in Nextcloud.
|
- **Dynamic Content:** Pages are automatically discovered based on the folder structure in Nextcloud.
|
||||||
- **Hidden Sites:** Folders starting with `_` are excluded from the navigation menu but remain accessible via direct links.
|
- **Hidden Sites:** Folders starting with `_` are excluded from the navigation menu but remain accessible via direct links.
|
||||||
- **Markdown Driven:** Content for each page is fetched from a corresponding Markdown file.
|
- **Markdown Driven:** Content for each page is fetched from a corresponding Markdown file and rendered via `gomarkdown`.
|
||||||
- **Image Optimization:** Automatic image resizing via Nextcloud's Preview API for optimized delivery and `srcset` support.
|
- **Image Optimization:** Automatic image resizing via Nextcloud's Preview API for optimized delivery and `srcset` support.
|
||||||
- **Asset Proxying:** Images and attachments are securely streamed from Nextcloud through a server-side proxy.
|
- **Asset Proxying:** Images and attachments are securely streamed from Nextcloud through a server-side proxy.
|
||||||
- **Performance:** In-memory caching for Nextcloud requests (15-minute TTL).
|
- **Performance:** Multi-layer caching using **Valkey** (Redis-compatible) for WebDAV requests and image scaling.
|
||||||
- **Responsive Design:** Styled with Tailwind CSS, optimized for all screen sizes.
|
- **Responsive Design:** Styled with **Tailwind CSS**, optimized for all screen sizes.
|
||||||
|
|
||||||
## Tech Stack
|
## Tech Stack
|
||||||
|
|
||||||
- **Framework:** Svelte 5 (Runes) & SvelteKit
|
- **Backend:** Go (Golang)
|
||||||
|
- **UI Components:** [templ](https://templ.guide/)
|
||||||
|
- **Frontend Interactivity:** [HTMX](https://htmx.org/)
|
||||||
- **Styling:** Tailwind CSS
|
- **Styling:** Tailwind CSS
|
||||||
- **Backend:** Node.js (via `@sveltejs/adapter-node`)
|
- **Caching:** Valkey
|
||||||
- **Integration:** `webdav` library for Nextcloud communication
|
- **Integration:** `gowebdav` for Nextcloud communication
|
||||||
- **Content:** `@ts-stack/markdown` for rendering
|
- **Content:** `github.com/gomarkdown/markdown` for rendering
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The application requires the following environment variables in a `.env` file:
|
The application requires the following environment variables in a `.env` file:
|
||||||
|
|
||||||
```env
|
```env
|
||||||
NEXTCLOUD_URL=https://your-nextcloud-instance.com
|
FILESYSTEM_URL=https://your-nextcloud-instance.com/remote.php/dav/files/user/
|
||||||
NEXTCLOUD_USER=your_username
|
FILESYSTEM_USERNAME=your_username
|
||||||
NEXTCLOUD_PASSWORD=your_app_password
|
FILESYSTEM_PASSWORD=your_app_password
|
||||||
NEXTCLOUD_BASE_DIR=WebsiteContent # Optional: Root folder for site content
|
CACHE_ADDRESS=localhost:6379
|
||||||
|
CACHE_PASSWORD=your_valkey_password
|
||||||
|
PORT=8080
|
||||||
|
LOG_LEVEL=DEBUG
|
||||||
```
|
```
|
||||||
|
|
||||||
## Content Management
|
## Content Management
|
||||||
@@ -45,28 +50,40 @@ To add or update content, manage files in your configured Nextcloud directory:
|
|||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
Install dependencies:
|
Install dependencies (Go, pnpm, and the `templ` binary):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
# Install Go dependencies
|
||||||
|
go mod tidy
|
||||||
|
# Install JS/CSS dependencies
|
||||||
pnpm install
|
pnpm install
|
||||||
```
|
```
|
||||||
|
|
||||||
Start the development server:
|
Start the development server with hot reloading:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm run dev
|
air
|
||||||
|
```
|
||||||
|
|
||||||
|
For CSS changes, run in a separate terminal:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm run watch:css
|
||||||
```
|
```
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
Build for production (Node.js):
|
Build the project for production:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pnpm run build
|
# Generate Go code from Templ files
|
||||||
|
go generate ./...
|
||||||
|
# Build CSS
|
||||||
|
pnpm run build:css
|
||||||
|
# Build Go binary
|
||||||
|
go build -o ./tmp/main ./cmd/schreifuchs-ch/main.go
|
||||||
```
|
```
|
||||||
|
|
||||||
The output will be in the `build/` directory, ready to be served by Node.
|
|
||||||
|
|
||||||
## Image Processing
|
## Image Processing
|
||||||
|
|
||||||
To optimize images before uploading to Nextcloud:
|
To optimize images before uploading to Nextcloud:
|
||||||
@@ -74,3 +91,4 @@ To optimize images before uploading to Nextcloud:
|
|||||||
```bash
|
```bash
|
||||||
convert input.png -resize 3000 -quality 80 output.webp
|
convert input.png -resize 3000 -quality 80 output.webp
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -3,20 +3,14 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
_ "net/http/pprof"
|
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go func() {
|
|
||||||
http.ListenAndServe("localhost:6060", nil)
|
|
||||||
}()
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx, cancle := context.WithCancel(ctx)
|
ctx, cancle := context.WithCancel(ctx)
|
||||||
defer cancle()
|
defer cancle()
|
||||||
@@ -29,13 +23,19 @@ func main() {
|
|||||||
cancle()
|
cancle()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
slog.SetLogLoggerLevel(slog.LevelDebug)
|
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
|
||||||
|
|
||||||
cfg, err := config.Read(ctx)
|
cfg, err := config.Read(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("could not read configuaration", "err", err)
|
slog.Error("could not read configuaration", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.SetDefault(
|
||||||
|
slog.New(
|
||||||
|
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
err = server.Start(ctx, cfg)
|
err = server.Start(ctx, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("error while serving", "err", err)
|
slog.Error("error while serving", "err", err)
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ require (
|
|||||||
github.com/sethvargo/go-envconfig v1.3.0
|
github.com/sethvargo/go-envconfig v1.3.0
|
||||||
github.com/studio-b12/gowebdav v0.12.0
|
github.com/studio-b12/gowebdav v0.12.0
|
||||||
github.com/valkey-io/valkey-go v1.0.72
|
github.com/valkey-io/valkey-go v1.0.72
|
||||||
|
github.com/valkey-io/valkey-go/mock v1.0.72
|
||||||
|
go.uber.org/mock v0.6.0
|
||||||
golang.org/x/image v0.36.0
|
golang.org/x/image v0.36.0
|
||||||
|
golang.org/x/sync v0.16.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -25,9 +28,8 @@ require (
|
|||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/natefinch/atomic v1.0.1 // indirect
|
github.com/natefinch/atomic v1.0.1 // indirect
|
||||||
github.com/yuin/gopher-lua v1.1.1 // indirect
|
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||||
golang.org/x/mod v0.26.0 // indirect
|
golang.org/x/mod v0.27.0 // indirect
|
||||||
golang.org/x/net v0.48.0 // indirect
|
golang.org/x/net v0.48.0 // indirect
|
||||||
golang.org/x/sync v0.16.0 // indirect
|
|
||||||
golang.org/x/sys v0.39.0 // indirect
|
golang.org/x/sys v0.39.0 // indirect
|
||||||
golang.org/x/tools v0.35.0 // indirect
|
golang.org/x/tools v0.36.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -39,14 +39,18 @@ github.com/studio-b12/gowebdav v0.12.0 h1:kFRtQECt8jmVAvA6RHBz3geXUGJHUZA6/IKpOV
|
|||||||
github.com/studio-b12/gowebdav v0.12.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
|
github.com/studio-b12/gowebdav v0.12.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE=
|
||||||
github.com/valkey-io/valkey-go v1.0.72 h1:iRWt1hJyOchcEgbHSkRY3aKkcBudxvMaVMsmxuYxuxE=
|
github.com/valkey-io/valkey-go v1.0.72 h1:iRWt1hJyOchcEgbHSkRY3aKkcBudxvMaVMsmxuYxuxE=
|
||||||
github.com/valkey-io/valkey-go v1.0.72/go.mod h1:VGhZ6fs68Qrn2+OhH+6waZH27bjpgQOiLyUQyXuYK5k=
|
github.com/valkey-io/valkey-go v1.0.72/go.mod h1:VGhZ6fs68Qrn2+OhH+6waZH27bjpgQOiLyUQyXuYK5k=
|
||||||
|
github.com/valkey-io/valkey-go/mock v1.0.72 h1:rE8K/sjlX0SRldI70Rt4/MCrYl224XD4A4vkYegP1Iw=
|
||||||
|
github.com/valkey-io/valkey-go/mock v1.0.72/go.mod h1:A4B8L3Wg85yAOl/GwNgkO/6aeGNXydwBl+86e20NQQY=
|
||||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
|
||||||
|
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||||
|
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||||
golang.org/x/image v0.36.0 h1:Iknbfm1afbgtwPTmHnS2gTM/6PPZfH+z2EFuOkSbqwc=
|
golang.org/x/image v0.36.0 h1:Iknbfm1afbgtwPTmHnS2gTM/6PPZfH+z2EFuOkSbqwc=
|
||||||
golang.org/x/image v0.36.0/go.mod h1:YsWD2TyyGKiIX1kZlu9QfKIsQ4nAAK9bdgdrIsE7xy4=
|
golang.org/x/image v0.36.0/go.mod h1:YsWD2TyyGKiIX1kZlu9QfKIsQ4nAAK9bdgdrIsE7xy4=
|
||||||
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
|
golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
|
||||||
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
|
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
|
||||||
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||||
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||||
@@ -57,7 +61,7 @@ golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
|||||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
|
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
|
||||||
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
|
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
|
||||||
golang.org/x/tools v0.35.0 h1:mBffYraMEf7aa0sB+NuKnuCy8qI/9Bughn8dC2Gu5r0=
|
golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg=
|
||||||
golang.org/x/tools v0.35.0/go.mod h1:NKdj5HkL/73byiZSJjqJgKn3ep7KjFkBOkR/Hps3VPw=
|
golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "golang.org/x/image/webp"
|
_ "golang.org/x/image/webp"
|
||||||
|
|
||||||
@@ -30,6 +29,7 @@ func (s *Service) getMime(path string) (mimeType string, err error) {
|
|||||||
|
|
||||||
if mimer, ok := info.(interface{ ContentType() string }); ok {
|
if mimer, ok := info.(interface{ ContentType() string }); ok {
|
||||||
mimeType = mimer.ContentType()
|
mimeType = mimer.ContentType()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -97,7 +97,7 @@ func (s *Service) GetImage(ctx context.Context, uid string, options Options) (im
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = s.cache.Do(ctx, s.cache.B().Set().Key("img:"+hash).Value(valkey.BinaryString(outBuff.Bytes())).Ex(time.Hour*48).Build()).Error(); err == nil {
|
if err = s.cache.Do(ctx, s.cache.B().Set().Key("img:"+hash).Value(valkey.BinaryString(outBuff.Bytes())).Ex(s.cfg.CacheLifetime).Build()).Error(); err == nil {
|
||||||
return outBuff, "image/jpeg", err
|
return outBuff, "image/jpeg", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package images_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/test/testdata"
|
||||||
|
"github.com/valkey-io/valkey-go"
|
||||||
|
"github.com/valkey-io/valkey-go/mock"
|
||||||
|
"go.uber.org/mock/gomock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Setup(ctx context.Context, ctrl *gomock.Controller) (*images.Service, *mock.Client) {
|
||||||
|
valkey := mock.NewClient(ctrl)
|
||||||
|
return images.New(testdata.FS(), &config.ImageConfig{Concurency: 1, Quality: 80}, valkey), valkey
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkService_GetImage(b *testing.B) {
|
||||||
|
srv, valkeyClient := Setup(b.Context(), gomock.NewController(b))
|
||||||
|
uid, err := images.UIDFromPath("cover.jpg")
|
||||||
|
if err != nil {
|
||||||
|
b.Error("could not get uid", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
for b.Loop() {
|
||||||
|
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.ErrorResult(errors.New("adf")))
|
||||||
|
valkeyClient.EXPECT().Do(gomock.Any(), gomock.Any()).Return(mock.Result(valkey.ValkeyMessage{}))
|
||||||
|
_, _, err = srv.GetImage(b.Context(), uid, images.Options{Width: 500, Quality: 80})
|
||||||
|
if err != nil {
|
||||||
|
b.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sourceSetSteps = 300
|
||||||
|
sourceSetMax = 1500
|
||||||
|
)
|
||||||
|
|
||||||
func UIDFromPath(path string) (uid string, err error) {
|
func UIDFromPath(path string) (uid string, err error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
// NewWriter with NoDict (nil) creates a raw DEFLATE compressor
|
// NewWriter with NoDict (nil) creates a raw DEFLATE compressor
|
||||||
@@ -57,12 +62,14 @@ func PathFromUID(uid string) (string, error) {
|
|||||||
func SourceSet(src string) string {
|
func SourceSet(src string) string {
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
|
|
||||||
for i := range 19 {
|
i := 1
|
||||||
sb.WriteString(fmt.Sprintf("%s?w=%d %dw, ", src, 100*(i+1), 100*(i+1)))
|
for {
|
||||||
}
|
width := i * sourceSetSteps
|
||||||
|
sb.WriteString(fmt.Sprintf("%s?w=%d %dw, ", src, width, width))
|
||||||
i := 20
|
if width >= sourceSetMax {
|
||||||
sb.WriteString(fmt.Sprintf("%s?w=%d %dw", src, 100*(i+1), 100*(i+1)))
|
|
||||||
|
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
sb.WriteRune(',')
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func scale(src image.Image, options Options) image.Image {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Resize:
|
// Resize:
|
||||||
draw.BiLinear.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
|
draw.ApproxBiLinear.Scale(dst, dst.Rect, src, src.Bounds(), draw.Over, nil)
|
||||||
|
|
||||||
// Encode to `output`:
|
// Encode to `output`:
|
||||||
return dst
|
return dst
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type PageHeader struct {
|
|||||||
type Page struct {
|
type Page struct {
|
||||||
PageHeader
|
PageHeader
|
||||||
Content string
|
Content string
|
||||||
|
Images []Image
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetPages(ctx context.Context) (pages []PageHeader, err error) {
|
func (s *Service) GetPages(ctx context.Context) (pages []PageHeader, err error) {
|
||||||
@@ -46,7 +47,7 @@ func (s *Service) GetPage(ctx context.Context, uid string) (page Page, err error
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
html, err := s.getHTML(ctx, p)
|
html, imgs, err := s.getHTML(ctx, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -54,6 +55,7 @@ func (s *Service) GetPage(ctx context.Context, uid string) (page Page, err error
|
|||||||
page = Page{
|
page = Page{
|
||||||
PageHeader: p,
|
PageHeader: p,
|
||||||
Content: string(html),
|
Content: string(html),
|
||||||
|
Images: imgs,
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,13 @@ import (
|
|||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Service) getHTML(ctx context.Context, page PageHeader) (out []byte, err error) {
|
// image can be used to preload
|
||||||
|
type Image struct {
|
||||||
|
SrcSet string
|
||||||
|
Src string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Service) getHTML(ctx context.Context, page PageHeader) (out []byte, imgs []Image, err error) {
|
||||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
||||||
p := parser.NewWithExtensions(extensions)
|
p := parser.NewWithExtensions(extensions)
|
||||||
|
|
||||||
@@ -24,7 +30,7 @@ func (s *Service) getHTML(ctx context.Context, page PageHeader) (out []byte, err
|
|||||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||||
opts := html.RendererOptions{
|
opts := html.RendererOptions{
|
||||||
Flags: htmlFlags,
|
Flags: htmlFlags,
|
||||||
RenderNodeHook: imageMiddleware(page),
|
RenderNodeHook: imageMiddleware(page, &imgs),
|
||||||
}
|
}
|
||||||
renderer := html.NewRenderer(opts)
|
renderer := html.NewRenderer(opts)
|
||||||
|
|
||||||
@@ -32,7 +38,7 @@ func (s *Service) getHTML(ctx context.Context, page PageHeader) (out []byte, err
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func imageMiddleware(page PageHeader) func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
func imageMiddleware(page PageHeader, imgs *[]Image) func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
if !entering {
|
if !entering {
|
||||||
return ast.GoToNext, false
|
return ast.GoToNext, false
|
||||||
@@ -53,14 +59,20 @@ func imageMiddleware(page PageHeader) func(w io.Writer, node ast.Node, entering
|
|||||||
slog.Error("could not get image url", "err", err)
|
slog.Error("could not get image url", "err", err)
|
||||||
return ast.GoToNext, false
|
return ast.GoToNext, false
|
||||||
}
|
}
|
||||||
|
image := Image{
|
||||||
|
SrcSet: images.SourceSet(src),
|
||||||
|
Src: src + "?w=1024",
|
||||||
|
}
|
||||||
|
|
||||||
img.Attribute = &ast.Attribute{Attrs: map[string][]byte{
|
img.Attribute = &ast.Attribute{Attrs: map[string][]byte{
|
||||||
"srcset": []byte(images.SourceSet(src)),
|
// "srcset": []byte(image.SrcSet),
|
||||||
"fetchpriority": []byte("low"),
|
"fetchpriority": []byte("low"),
|
||||||
"loading": []byte("lazy"),
|
"loading": []byte("lazy"),
|
||||||
"class": []byte("min-h-48"),
|
"class": []byte("min-h-48"),
|
||||||
"src": []byte(src),
|
"src": []byte(image.Src),
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
*imgs = append(*imgs, image)
|
||||||
// // img.Attrs["srcset"] =
|
// // img.Attrs["srcset"] =
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,6 @@ func Path(ctx context.Context, pp ...string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PathForLang(ctx context.Context, lang string, pp ...string) string {
|
func PathForLang(ctx context.Context, lang string, pp ...string) string {
|
||||||
slog.Info("path for lang", "path", pp)
|
|
||||||
s, ok := ctx.Value(serviceKey).(*Service)
|
s, ok := ctx.Value(serviceKey).(*Service)
|
||||||
if !ok {
|
if !ok {
|
||||||
slog.Error("could not extract translation service from context for translating path", "ctx", ctx, "path", pp)
|
slog.Error("could not extract translation service from context for translating path", "ctx", ctx, "path", pp)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package restutil
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package restutil
|
package rest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package rest
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Middeware = func(next http.Handler) http.Handler
|
||||||
|
|
||||||
|
func Use(handler http.Handler, middlewares ...Middeware) http.Handler {
|
||||||
|
for _, middleware := range middlewares {
|
||||||
|
handler = middleware(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package resthome
|
package resthome
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/pages"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/pages"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,6 +19,7 @@ func (h *Handler) home(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
uid := r.PathValue("uid")
|
uid := r.PathValue("uid")
|
||||||
|
|
||||||
page, err := h.src.GetPage(r.Context(), uid)
|
page, err := h.src.GetPage(r.Context(), uid)
|
||||||
@@ -25,5 +28,14 @@ func (h *Handler) page(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.r.Render(r.Context(), w, r, pages.ContentPage(page))
|
for i, image := range page.Images {
|
||||||
|
if i > 3 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
ctx = templer.AppendHeader(ctx, fmt.Sprintf(`<link rel="preload" href="%s" as="image" />`, image.Src))
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = templer.PageTitle(ctx, page.Title)
|
||||||
|
|
||||||
|
h.r.Render(ctx, w, r, pages.ContentPage(page))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
|
http.Handler
|
||||||
src pageService
|
src pageService
|
||||||
r renderer
|
r renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(mux *http.ServeMux, renderer renderer, srv pageService) *Handler {
|
func New(renderer renderer, srv pageService) *Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
h := &Handler{
|
h := &Handler{
|
||||||
|
Handler: mux,
|
||||||
src: srv,
|
src: srv,
|
||||||
r: renderer,
|
r: renderer,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restutil"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -14,20 +14,20 @@ func (h *Handler) getImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
options := images.Options{}
|
options := images.Options{}
|
||||||
options.Height, err = restutil.IntParam(r, "h")
|
options.Height, err = rest.IntParam(r, "h")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
restutil.SendErr(w, err)
|
rest.SendErr(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
options.Width, err = restutil.IntParam(r, "w")
|
options.Width, err = rest.IntParam(r, "w")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
restutil.SendErr(w, err)
|
rest.SendErr(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
options.Quality, err = restutil.IntParam(r, "q")
|
options.Quality, err = rest.IntParam(r, "q")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
restutil.SendErr(w, err)
|
rest.SendErr(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
|
http.Handler
|
||||||
img imageService
|
img imageService
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(mux *http.ServeMux, srv imageService) *Handler {
|
func New(srv imageService) *Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
h := &Handler{
|
h := &Handler{
|
||||||
|
Handler: mux,
|
||||||
img: srv,
|
img: srv,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
func Default() Cfg {
|
func Default() Cfg {
|
||||||
return Cfg{
|
return Cfg{
|
||||||
Image: &ImageConfig{
|
Image: &ImageConfig{
|
||||||
Concurency: 10,
|
Concurency: 10,
|
||||||
Quality: 80,
|
Quality: 80,
|
||||||
|
CacheLifetime: time.Hour * 24 * 31,
|
||||||
},
|
},
|
||||||
Tracking: &UmamiConfig{
|
Tracking: &UmamiConfig{
|
||||||
ScriptSrc: "https://umami.schreifuchs.ch/script.js",
|
ScriptSrc: "https://umami.schreifuchs.ch/script.js",
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sethvargo/go-envconfig"
|
"github.com/sethvargo/go-envconfig"
|
||||||
)
|
)
|
||||||
@@ -11,6 +13,8 @@ type Cfg struct {
|
|||||||
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
Cache *ValkeyConfig `env:", prefix=CACHE_"`
|
||||||
Image *ImageConfig `env:", prefix=IMAGE_"`
|
Image *ImageConfig `env:", prefix=IMAGE_"`
|
||||||
Tracking *UmamiConfig `env:", prefix=TRACKING_"`
|
Tracking *UmamiConfig `env:", prefix=TRACKING_"`
|
||||||
|
|
||||||
|
LogLevel slog.Level `env:"LOG_LEVEL, default=DEBUG"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebdavConfig struct {
|
type WebdavConfig struct {
|
||||||
@@ -35,6 +39,7 @@ type ValkeyConfig struct {
|
|||||||
type ImageConfig struct {
|
type ImageConfig struct {
|
||||||
Concurency int64 `env:"CONCURENCY"`
|
Concurency int64 `env:"CONCURENCY"`
|
||||||
Quality int `env:"QUALITY"`
|
Quality int `env:"QUALITY"`
|
||||||
|
CacheLifetime time.Duration `env:"CACHE_LIFETIME"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UmamiConfig struct {
|
type UmamiConfig struct {
|
||||||
@@ -51,6 +56,7 @@ func Read(ctx context.Context) (cfg Cfg, err error) {
|
|||||||
// overridden.
|
// overridden.
|
||||||
DefaultDelimiter: ";",
|
DefaultDelimiter: ";",
|
||||||
DefaultSeparator: "@",
|
DefaultSeparator: "@",
|
||||||
|
DefaultOverwrite: true,
|
||||||
|
|
||||||
Lookuper: envconfig.MultiLookuper(secretLookuper(), envconfig.OsLookuper()),
|
Lookuper: envconfig.MultiLookuper(secretLookuper(), envconfig.OsLookuper()),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package server
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -17,7 +17,7 @@ func (c *captureWriter) WriteHeader(statusCode int) {
|
|||||||
c.ResponseWriter.WriteHeader(statusCode)
|
c.ResponseWriter.WriteHeader(statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
func Logging(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
cw := &captureWriter{
|
cw := &captureWriter{
|
||||||
@@ -35,7 +35,7 @@ func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cacheMiddleware adds Cache-Control headers to the response.
|
// cacheMiddleware adds Cache-Control headers to the response.
|
||||||
func cacheMiddleware(maxAge time.Duration) func(next http.Handler) http.Handler {
|
func Cache(maxAge time.Duration) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
|
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.0f", maxAge.Seconds()))
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package templer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetHeader returns a slice of strings that should be added to the html head.
|
||||||
|
// Because strings are immutable a slice of strings is used.
|
||||||
|
func GetHeader(ctx context.Context) []string {
|
||||||
|
v := ctx.Value(headerKey)
|
||||||
|
if v == nil {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
header, ok := v.([]string)
|
||||||
|
if !ok {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
return header
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendHeader lets you add lines to the header.
|
||||||
|
func AppendHeader(ctx context.Context, v ...string) context.Context {
|
||||||
|
header := GetHeader(ctx)
|
||||||
|
|
||||||
|
header = append(header, v...)
|
||||||
|
|
||||||
|
return context.WithValue(ctx, headerKey, header)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendHeader lets you add lines to the header.
|
||||||
|
func PageTitle(ctx context.Context, title string) context.Context {
|
||||||
|
return context.WithValue(ctx, titleKey, title)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendHeader lets you add lines to the header.
|
||||||
|
func GetTitle(ctx context.Context, main string) string {
|
||||||
|
v := ctx.Value(titleKey)
|
||||||
|
if v == nil {
|
||||||
|
return main
|
||||||
|
}
|
||||||
|
|
||||||
|
title, ok := v.(string)
|
||||||
|
if !ok {
|
||||||
|
return main
|
||||||
|
}
|
||||||
|
return title + " | " + main
|
||||||
|
}
|
||||||
@@ -11,6 +11,8 @@ type ctxKey int
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
pathKey ctxKey = iota
|
pathKey ctxKey = iota
|
||||||
|
headerKey
|
||||||
|
titleKey
|
||||||
)
|
)
|
||||||
|
|
||||||
func Middleware(next http.Handler) http.HandlerFunc {
|
func Middleware(next http.Handler) http.HandlerFunc {
|
||||||
|
|||||||
@@ -1,40 +1,50 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/images"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/page"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/rest"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/resthome"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/handlers/restimage"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/filesystem"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/middleware"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/layouts"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web/layouts"
|
||||||
"github.com/studio-b12/gowebdav"
|
"github.com/studio-b12/gowebdav"
|
||||||
"github.com/valkey-io/valkey-go"
|
"github.com/valkey-io/valkey-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) RegisterRoutes() (h http.Handler) {
|
func (s *Server) allRoutes() (h http.Handler, err error) {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
registerStatic(mux)
|
registerStatic(mux)
|
||||||
mux.Handle("/", s.registerOther())
|
|
||||||
|
other, err := s.dynamicRoutes()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mux.Handle("/", other)
|
||||||
|
|
||||||
h = mux
|
h = mux
|
||||||
h = templer.Middleware(h)
|
h = templer.Middleware(h)
|
||||||
h = s.loggingMiddleware(h)
|
h = middleware.Logging(h)
|
||||||
return h
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) registerOther() (h http.Handler) {
|
func (s *Server) dynamicRoutes() (h http.Handler, err error) {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
webdavClient := gowebdav.NewClient(s.cfg.FileSystem.URL, s.cfg.FileSystem.User, s.cfg.FileSystem.Password)
|
webdavClient := gowebdav.NewClient(s.cfg.FileSystem.URL, s.cfg.FileSystem.User, s.cfg.FileSystem.Password)
|
||||||
if err := webdavClient.Connect(); err != nil {
|
err = webdavClient.Connect()
|
||||||
slog.Error("could not connect webdavClient", "err", err)
|
if err != nil {
|
||||||
|
err = fmt.Errorf("could not connect webdavClient: %w", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var fs filesystem.FS = webdavClient
|
var fs filesystem.FS = webdavClient
|
||||||
@@ -44,22 +54,27 @@ func (s *Server) registerOther() (h http.Handler) {
|
|||||||
Password: s.cfg.Cache.Password,
|
Password: s.cfg.Cache.Password,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to create valkey client", "err", err)
|
err = fmt.Errorf("failed to create valkey client: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
} else {
|
} else {
|
||||||
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, 5*time.Hour)
|
fs = filesystem.NewCachedClient(webdavClient, valkeyClient, 5*time.Hour)
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer := layouts.New(s.cfg)
|
mux.Handle("/images/", rest.Use(
|
||||||
|
restimage.New(images.New(fs, s.cfg.Image, valkeyClient)),
|
||||||
_ = resthome.New(mux, renderer, page.New(fs))
|
middleware.Cache(time.Hour*24*5),
|
||||||
_ = restimage.New(mux, images.New(fs, s.cfg.Image, valkeyClient))
|
))
|
||||||
|
|
||||||
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
|
||||||
|
|
||||||
translator := translate.New()
|
translator := translate.New()
|
||||||
|
renderer := layouts.New(s.cfg)
|
||||||
|
|
||||||
h = mux
|
mux.Handle("/", rest.Use(
|
||||||
h = translator.Middleware(h)
|
resthome.New(renderer, page.New(fs)),
|
||||||
h = cacheMiddleware(time.Minute * 5)(h)
|
translator.Middleware,
|
||||||
return h
|
middleware.Cache(time.Minute),
|
||||||
|
))
|
||||||
|
|
||||||
|
return mux, nil
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// Package server provides the http server for schreifuchs.ch.
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -17,6 +18,7 @@ type Server struct {
|
|||||||
cfg config.Cfg
|
cfg config.Cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start starts a new http server. When the context gets canceled, the server stops gracefully.
|
||||||
func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
||||||
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
@@ -27,10 +29,16 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
|||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hander, err := s.allRoutes()
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("could not setup routes: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Declare Server config
|
// Declare Server config
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: fmt.Sprintf(":%d", s.port),
|
Addr: fmt.Sprintf(":%d", s.port),
|
||||||
Handler: s.RegisterRoutes(),
|
Handler: hander,
|
||||||
IdleTimeout: time.Minute,
|
IdleTimeout: time.Minute,
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
@@ -45,7 +53,7 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
|||||||
// Shutdown server when ctx done
|
// Shutdown server when ctx done
|
||||||
if err = srv.Shutdown(context.Background()); err != nil {
|
if err = srv.Shutdown(context.Background()); err != nil {
|
||||||
// Error from closing listeners, or context timeout:
|
// Error from closing listeners, or context timeout:
|
||||||
err = fmt.Errorf("error whilse shutting down server: %w", err)
|
err = fmt.Errorf("error while shutting down server: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(idleConnsClosed)
|
close(idleConnsClosed)
|
||||||
@@ -53,7 +61,7 @@ func Start(ctx context.Context, cfg config.Cfg) (err error) {
|
|||||||
|
|
||||||
slog.Info("starting server", "address", srv.Addr)
|
slog.Info("starting server", "address", srv.Addr)
|
||||||
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
||||||
return fmt.Errorf("error while server ListenAndServe: %w", err)
|
return fmt.Errorf("error while serving: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-idleConnsClosed
|
<-idleConnsClosed
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/middleware"
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ func registerStatic(mux *http.ServeMux) {
|
|||||||
fileSystem := web.GetStaticFS()
|
fileSystem := web.GetStaticFS()
|
||||||
|
|
||||||
// we still use go's fileServer to avoid unnecessary implementation
|
// we still use go's fileServer to avoid unnecessary implementation
|
||||||
fileServer := cacheMiddleware(time.Hour * 24)(http.FileServer(http.FS(fileSystem)))
|
fileServer := middleware.Cache(time.Hour * 24)(http.FileServer(http.FS(fileSystem)))
|
||||||
|
|
||||||
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
|
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -26,7 +27,7 @@ func registerStatic(mux *http.ServeMux) {
|
|||||||
|
|
||||||
path = "/" + path
|
path = "/" + path
|
||||||
|
|
||||||
slog.Debug("registering file", "path", path)
|
slog.Debug("registered static file", "path", path)
|
||||||
|
|
||||||
mux.Handle(path, fileServer)
|
mux.Handle(path, fileServer)
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -4,9 +4,9 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build:css": "tailwindcss -i ./web/static/css/input.css -o ./web/static/css/output.css --minify",
|
"build:css": "tailwindcss -i ./web/src/css/input.css -o ./web/static/css/output.css --minify",
|
||||||
"watch:css": "tailwindcss -i ./web/static/css/input.css -o ./web/static/css/output.css --watch",
|
"watch:css": "tailwindcss -i ./web/src/css/input.css -o ./web/static/css/output.css --watch",
|
||||||
"build:js": "cp node_modules/htmx.org/dist/htmx.min.js web/static/js/htmx.min.js && cp ./node_modules/htmx-ext-preload/dist/preload.min.js ./web/static/js/preload.min.js"
|
"build:js": "node scripts/build.mjs"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
@@ -16,10 +16,12 @@
|
|||||||
"@tailwindcss/cli": "^4.1.18",
|
"@tailwindcss/cli": "^4.1.18",
|
||||||
"@tailwindcss/typography": "^0.5.19",
|
"@tailwindcss/typography": "^0.5.19",
|
||||||
"autoprefixer": "^10.4.24",
|
"autoprefixer": "^10.4.24",
|
||||||
|
"esbuild": "^0.27.4",
|
||||||
"postcss": "^8.5.6",
|
"postcss": "^8.5.6",
|
||||||
"tailwindcss": "^4.1.18"
|
"tailwindcss": "^4.1.18"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"htmx-ext-head-support": "^2.0.5",
|
||||||
"htmx-ext-preload": "^2.1.2",
|
"htmx-ext-preload": "^2.1.2",
|
||||||
"htmx.org": "^2.0.8"
|
"htmx.org": "^2.0.8"
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+281
@@ -8,6 +8,9 @@ importers:
|
|||||||
|
|
||||||
.:
|
.:
|
||||||
dependencies:
|
dependencies:
|
||||||
|
htmx-ext-head-support:
|
||||||
|
specifier: ^2.0.5
|
||||||
|
version: 2.0.5
|
||||||
htmx-ext-preload:
|
htmx-ext-preload:
|
||||||
specifier: ^2.1.2
|
specifier: ^2.1.2
|
||||||
version: 2.1.2
|
version: 2.1.2
|
||||||
@@ -24,6 +27,9 @@ importers:
|
|||||||
autoprefixer:
|
autoprefixer:
|
||||||
specifier: ^10.4.24
|
specifier: ^10.4.24
|
||||||
version: 10.4.24(postcss@8.5.6)
|
version: 10.4.24(postcss@8.5.6)
|
||||||
|
esbuild:
|
||||||
|
specifier: ^0.27.4
|
||||||
|
version: 0.27.4
|
||||||
postcss:
|
postcss:
|
||||||
specifier: ^8.5.6
|
specifier: ^8.5.6
|
||||||
version: 8.5.6
|
version: 8.5.6
|
||||||
@@ -33,6 +39,162 @@ importers:
|
|||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
|
'@esbuild/aix-ppc64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-cQPwL2mp2nSmHHJlCyoXgHGhbEPMrEEU5xhkcy3Hs/O7nGZqEpZ2sUtLaL9MORLtDfRvVl2/3PAuEkYZH0Ty8Q==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ppc64]
|
||||||
|
os: [aix]
|
||||||
|
|
||||||
|
'@esbuild/android-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-gdLscB7v75wRfu7QSm/zg6Rx29VLdy9eTr2t44sfTW7CxwAtQghZ4ZnqHk3/ogz7xao0QAgrkradbBzcqFPasw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/android-arm@0.27.4':
|
||||||
|
resolution: {integrity: sha512-X9bUgvxiC8CHAGKYufLIHGXPJWnr0OCdR0anD2e21vdvgCI8lIfqFbnoeOz7lBjdrAGUhqLZLcQo6MLhTO2DKQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/android-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-PzPFnBNVF292sfpfhiyiXCGSn9HZg5BcAz+ivBuSsl6Rk4ga1oEXAamhOXRFyMcjwr2DVtm40G65N3GLeH1Lvw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/darwin-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-b7xaGIwdJlht8ZFCvMkpDN6uiSmnxxK56N2GDTMYPr2/gzvfdQN8rTfBsvVKmIVY/X7EM+/hJKEIbbHs9oA4tQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [darwin]
|
||||||
|
|
||||||
|
'@esbuild/darwin-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-sR+OiKLwd15nmCdqpXMnuJ9W2kpy0KigzqScqHI3Hqwr7IXxBp3Yva+yJwoqh7rE8V77tdoheRYataNKL4QrPw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [darwin]
|
||||||
|
|
||||||
|
'@esbuild/freebsd-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-jnfpKe+p79tCnm4GVav68A7tUFeKQwQyLgESwEAUzyxk/TJr4QdGog9sqWNcUbr/bZt/O/HXouspuQDd9JxFSw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [freebsd]
|
||||||
|
|
||||||
|
'@esbuild/freebsd-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-2kb4ceA/CpfUrIcTUl1wrP/9ad9Atrp5J94Lq69w7UwOMolPIGrfLSvAKJp0RTvkPPyn6CIWrNy13kyLikZRZQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [freebsd]
|
||||||
|
|
||||||
|
'@esbuild/linux-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-7nQOttdzVGth1iz57kxg9uCz57dxQLHWxopL6mYuYthohPKEK0vU0C3O21CcBK6KDlkYVcnDXY099HcCDXd9dA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-arm@0.27.4':
|
||||||
|
resolution: {integrity: sha512-aBYgcIxX/wd5n2ys0yESGeYMGF+pv6g0DhZr3G1ZG4jMfruU9Tl1i2Z+Wnj9/KjGz1lTLCcorqE2viePZqj4Eg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-ia32@0.27.4':
|
||||||
|
resolution: {integrity: sha512-oPtixtAIzgvzYcKBQM/qZ3R+9TEUd1aNJQu0HhGyqtx6oS7qTpvjheIWBbes4+qu1bNlo2V4cbkISr8q6gRBFA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ia32]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-loong64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-8mL/vh8qeCoRcFH2nM8wm5uJP+ZcVYGGayMavi8GmRJjuI3g1v6Z7Ni0JJKAJW+m0EtUuARb6Lmp4hMjzCBWzA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [loong64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-mips64el@0.27.4':
|
||||||
|
resolution: {integrity: sha512-1RdrWFFiiLIW7LQq9Q2NES+HiD4NyT8Itj9AUeCl0IVCA459WnPhREKgwrpaIfTOe+/2rdntisegiPWn/r/aAw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [mips64el]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-ppc64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-tLCwNG47l3sd9lpfyx9LAGEGItCUeRCWeAx6x2Jmbav65nAwoPXfewtAdtbtit/pJFLUWOhpv0FpS6GQAmPrHA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ppc64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-riscv64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-BnASypppbUWyqjd1KIpU4AUBiIhVr6YlHx/cnPgqEkNoVOhHg+YiSVxM1RLfiy4t9cAulbRGTNCKOcqHrEQLIw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [riscv64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-s390x@0.27.4':
|
||||||
|
resolution: {integrity: sha512-+eUqgb/Z7vxVLezG8bVB9SfBie89gMueS+I0xYh2tJdw3vqA/0ImZJ2ROeWwVJN59ihBeZ7Tu92dF/5dy5FttA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [s390x]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-S5qOXrKV8BQEzJPVxAwnryi2+Iq5pB40gTEIT69BQONqR7JH1EPIcQ/Uiv9mCnn05jff9umq/5nqzxlqTOg9NA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/netbsd-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-xHT8X4sb0GS8qTqiwzHqpY00C95DPAq7nAwX35Ie/s+LO9830hrMd3oX0ZMKLvy7vsonee73x0lmcdOVXFzd6Q==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [netbsd]
|
||||||
|
|
||||||
|
'@esbuild/netbsd-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-RugOvOdXfdyi5Tyv40kgQnI0byv66BFgAqjdgtAKqHoZTbTF2QqfQrFwa7cHEORJf6X2ht+l9ABLMP0dnKYsgg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [netbsd]
|
||||||
|
|
||||||
|
'@esbuild/openbsd-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-2MyL3IAaTX+1/qP0O1SwskwcwCoOI4kV2IBX1xYnDDqthmq5ArrW94qSIKCAuRraMgPOmG0RDTA74mzYNQA9ow==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [openbsd]
|
||||||
|
|
||||||
|
'@esbuild/openbsd-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-u8fg/jQ5aQDfsnIV6+KwLOf1CmJnfu1ShpwqdwC0uA7ZPwFws55Ngc12vBdeUdnuWoQYx/SOQLGDcdlfXhYmXQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [openbsd]
|
||||||
|
|
||||||
|
'@esbuild/openharmony-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-JkTZrl6VbyO8lDQO3yv26nNr2RM2yZzNrNHEsj9bm6dOwwu9OYN28CjzZkH57bh4w0I2F7IodpQvUAEd1mbWXg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [openharmony]
|
||||||
|
|
||||||
|
'@esbuild/sunos-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-/gOzgaewZJfeJTlsWhvUEmUG4tWEY2Spp5M20INYRg2ZKl9QPO3QEEgPeRtLjEWSW8FilRNacPOg8R1uaYkA6g==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [sunos]
|
||||||
|
|
||||||
|
'@esbuild/win32-arm64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-Z9SExBg2y32smoDQdf1HRwHRt6vAHLXcxD2uGgO/v2jK7Y718Ix4ndsbNMU/+1Qiem9OiOdaqitioZwxivhXYg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
|
'@esbuild/win32-ia32@0.27.4':
|
||||||
|
resolution: {integrity: sha512-DAyGLS0Jz5G5iixEbMHi5KdiApqHBWMGzTtMiJ72ZOLhbu/bzxgAe8Ue8CTS3n3HbIUHQz/L51yMdGMeoxXNJw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ia32]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
|
'@esbuild/win32-x64@0.27.4':
|
||||||
|
resolution: {integrity: sha512-+knoa0BDoeXgkNvvV1vvbZX4+hizelrkwmGJBdT17t8FNPwG2lKemmuMZlmaNQ3ws3DKKCxpb4zRZEIp3UxFCg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
'@jridgewell/gen-mapping@0.3.13':
|
'@jridgewell/gen-mapping@0.3.13':
|
||||||
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
|
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
|
||||||
|
|
||||||
@@ -270,6 +432,11 @@ packages:
|
|||||||
resolution: {integrity: sha512-phv3E1Xl4tQOShqSte26C7Fl84EwUdZsyOuSSk9qtAGyyQs2s3jJzComh+Abf4g187lUUAvH+H26omrqia2aGg==}
|
resolution: {integrity: sha512-phv3E1Xl4tQOShqSte26C7Fl84EwUdZsyOuSSk9qtAGyyQs2s3jJzComh+Abf4g187lUUAvH+H26omrqia2aGg==}
|
||||||
engines: {node: '>=10.13.0'}
|
engines: {node: '>=10.13.0'}
|
||||||
|
|
||||||
|
esbuild@0.27.4:
|
||||||
|
resolution: {integrity: sha512-Rq4vbHnYkK5fws5NF7MYTU68FPRE1ajX7heQ/8QXXWqNgqqJ/GkmmyxIzUnf2Sr/bakf8l54716CcMGHYhMrrQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
escalade@3.2.0:
|
escalade@3.2.0:
|
||||||
resolution: {integrity: sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==}
|
resolution: {integrity: sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==}
|
||||||
engines: {node: '>=6'}
|
engines: {node: '>=6'}
|
||||||
@@ -280,6 +447,9 @@ packages:
|
|||||||
graceful-fs@4.2.11:
|
graceful-fs@4.2.11:
|
||||||
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==}
|
||||||
|
|
||||||
|
htmx-ext-head-support@2.0.5:
|
||||||
|
resolution: {integrity: sha512-fTzqnCw7OnMijiS48CCBFBHTuOMvycbX0nEFRL0lkRxyu5DFWX90IV1SwXpQ92fJf02IzkXcRNyjXn3j0QtYxA==}
|
||||||
|
|
||||||
htmx-ext-preload@2.1.2:
|
htmx-ext-preload@2.1.2:
|
||||||
resolution: {integrity: sha512-foD06XAITUUQApCUQnPexyJ6B7+4vFGMQ+t42unTVTGRu/CYHdAAt6oHg7eekPHT7NRnbM5hjnGVKtClryPabg==}
|
resolution: {integrity: sha512-foD06XAITUUQApCUQnPexyJ6B7+4vFGMQ+t42unTVTGRu/CYHdAAt6oHg7eekPHT7NRnbM5hjnGVKtClryPabg==}
|
||||||
|
|
||||||
@@ -430,6 +600,84 @@ packages:
|
|||||||
|
|
||||||
snapshots:
|
snapshots:
|
||||||
|
|
||||||
|
'@esbuild/aix-ppc64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-arm@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/darwin-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/darwin-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/freebsd-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/freebsd-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-arm@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-ia32@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-loong64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-mips64el@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-ppc64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-riscv64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-s390x@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/netbsd-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/netbsd-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openbsd-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openbsd-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openharmony-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/sunos-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-arm64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-ia32@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-x64@0.27.4':
|
||||||
|
optional: true
|
||||||
|
|
||||||
'@jridgewell/gen-mapping@0.3.13':
|
'@jridgewell/gen-mapping@0.3.13':
|
||||||
dependencies:
|
dependencies:
|
||||||
'@jridgewell/sourcemap-codec': 1.5.5
|
'@jridgewell/sourcemap-codec': 1.5.5
|
||||||
@@ -617,12 +865,45 @@ snapshots:
|
|||||||
graceful-fs: 4.2.11
|
graceful-fs: 4.2.11
|
||||||
tapable: 2.3.0
|
tapable: 2.3.0
|
||||||
|
|
||||||
|
esbuild@0.27.4:
|
||||||
|
optionalDependencies:
|
||||||
|
'@esbuild/aix-ppc64': 0.27.4
|
||||||
|
'@esbuild/android-arm': 0.27.4
|
||||||
|
'@esbuild/android-arm64': 0.27.4
|
||||||
|
'@esbuild/android-x64': 0.27.4
|
||||||
|
'@esbuild/darwin-arm64': 0.27.4
|
||||||
|
'@esbuild/darwin-x64': 0.27.4
|
||||||
|
'@esbuild/freebsd-arm64': 0.27.4
|
||||||
|
'@esbuild/freebsd-x64': 0.27.4
|
||||||
|
'@esbuild/linux-arm': 0.27.4
|
||||||
|
'@esbuild/linux-arm64': 0.27.4
|
||||||
|
'@esbuild/linux-ia32': 0.27.4
|
||||||
|
'@esbuild/linux-loong64': 0.27.4
|
||||||
|
'@esbuild/linux-mips64el': 0.27.4
|
||||||
|
'@esbuild/linux-ppc64': 0.27.4
|
||||||
|
'@esbuild/linux-riscv64': 0.27.4
|
||||||
|
'@esbuild/linux-s390x': 0.27.4
|
||||||
|
'@esbuild/linux-x64': 0.27.4
|
||||||
|
'@esbuild/netbsd-arm64': 0.27.4
|
||||||
|
'@esbuild/netbsd-x64': 0.27.4
|
||||||
|
'@esbuild/openbsd-arm64': 0.27.4
|
||||||
|
'@esbuild/openbsd-x64': 0.27.4
|
||||||
|
'@esbuild/openharmony-arm64': 0.27.4
|
||||||
|
'@esbuild/sunos-x64': 0.27.4
|
||||||
|
'@esbuild/win32-arm64': 0.27.4
|
||||||
|
'@esbuild/win32-ia32': 0.27.4
|
||||||
|
'@esbuild/win32-x64': 0.27.4
|
||||||
|
|
||||||
escalade@3.2.0: {}
|
escalade@3.2.0: {}
|
||||||
|
|
||||||
fraction.js@5.3.4: {}
|
fraction.js@5.3.4: {}
|
||||||
|
|
||||||
graceful-fs@4.2.11: {}
|
graceful-fs@4.2.11: {}
|
||||||
|
|
||||||
|
htmx-ext-head-support@2.0.5:
|
||||||
|
dependencies:
|
||||||
|
htmx.org: 2.0.8
|
||||||
|
|
||||||
htmx-ext-preload@2.1.2:
|
htmx-ext-preload@2.1.2:
|
||||||
dependencies:
|
dependencies:
|
||||||
htmx.org: 2.0.8
|
htmx.org: 2.0.8
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 439 KiB |
-1976
File diff suppressed because it is too large
Load Diff
|
Before Width: | Height: | Size: 112 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 439 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 439 KiB |
@@ -0,0 +1,69 @@
|
|||||||
|
import * as esbuild from "esbuild";
|
||||||
|
import fs from "node:fs/promises";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
const filesToCopy = [
|
||||||
|
"node_modules/htmx.org/dist/htmx.min.js",
|
||||||
|
"node_modules/htmx-ext-preload/dist/preload.min.js",
|
||||||
|
"node_modules/htmx-ext-head-support/dist/head-support.min.js",
|
||||||
|
];
|
||||||
|
|
||||||
|
const filesToMinifyRoot = "web/src/js";
|
||||||
|
|
||||||
|
const outputRoot = "web/static/js";
|
||||||
|
|
||||||
|
async function minifyFiles() {
|
||||||
|
try {
|
||||||
|
const files = (await fs.readdir(filesToMinifyRoot))
|
||||||
|
.filter((p) => p.endsWith(".js"))
|
||||||
|
.map((p) => {
|
||||||
|
return {
|
||||||
|
src: path.join(filesToMinifyRoot, p),
|
||||||
|
dest: path.join(outputRoot, p.substring(0, p.length - 3) + ".min.js"),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
await esbuild.build({
|
||||||
|
entryPoints: [file.src],
|
||||||
|
bundle: false,
|
||||||
|
minify: true,
|
||||||
|
outfile: file.dest,
|
||||||
|
});
|
||||||
|
console.log(`Minified ${file.src} to ${file.dest}`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("could not minify files", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function coppyFromNodemodules() {
|
||||||
|
try {
|
||||||
|
const fileMap = filesToCopy.map((file) => {
|
||||||
|
return {
|
||||||
|
src: file,
|
||||||
|
dest: path.join(outputRoot, path.basename(file)),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const file of fileMap) {
|
||||||
|
await fs.copyFile(file.src, file.dest);
|
||||||
|
console.log(`Copied ${path.basename(file.dest)}`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("JS build failed:", err);
|
||||||
|
process.exit(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function build() {
|
||||||
|
console.log("Starting JS build...");
|
||||||
|
|
||||||
|
await minifyFiles();
|
||||||
|
await coppyFromNodemodules();
|
||||||
|
|
||||||
|
console.log("JS build complete!");
|
||||||
|
}
|
||||||
|
|
||||||
|
build();
|
||||||
|
|
||||||
Vendored
BIN
Binary file not shown.
|
After Width: | Height: | Size: 4.2 MiB |
Vendored
+62
@@ -0,0 +1,62 @@
|
|||||||
|
package testdata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"embed"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed *
|
||||||
|
var fs embed.FS
|
||||||
|
|
||||||
|
func FS() mockFS {
|
||||||
|
return mockFS{
|
||||||
|
FS: fs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockFS struct {
|
||||||
|
embed.FS
|
||||||
|
}
|
||||||
|
|
||||||
|
type mockMimer struct{ os.FileInfo }
|
||||||
|
|
||||||
|
func (m *mockMimer) ContentType() string {
|
||||||
|
parts := strings.Split(m.Name(), ".")
|
||||||
|
|
||||||
|
suffix := parts[len(parts)-1]
|
||||||
|
|
||||||
|
switch suffix {
|
||||||
|
case "jpg":
|
||||||
|
return "image/jpeg"
|
||||||
|
default:
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mockFS) Read(path string) (b []byte, err error) {
|
||||||
|
file, err := m.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
buff := bytes.NewBuffer(make([]byte, 0, 1024))
|
||||||
|
|
||||||
|
_, err = io.Copy(buff, file)
|
||||||
|
|
||||||
|
return buff.Bytes(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mockFS) Stat(path string) (info os.FileInfo, err error) {
|
||||||
|
file, err := m.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err = file.Stat()
|
||||||
|
info = &mockMimer{info}
|
||||||
|
return
|
||||||
|
}
|
||||||
+23
-10
@@ -1,34 +1,47 @@
|
|||||||
package layouts
|
package layouts
|
||||||
|
|
||||||
import "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
import (
|
||||||
import "strings"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/components/translate"
|
||||||
import "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
import "git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
templ Base(cfg config.Cfg, title string) {
|
templ Head(cfg config.Cfg, title string) {
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang={ translate.Language(ctx) }>
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8"/>
|
<meta charset="UTF-8"/>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
<title>{ title }</title>
|
<title>{ title }</title>
|
||||||
<link rel="icon" type="image/png" href="/favicon.png"/>
|
<link rel="icon" type="image/png" href="/favicon.png"/>
|
||||||
<link fetchpriority="high" href="/css/output.css" rel="stylesheet"/>
|
<link fetchpriority="high" href="/css/output.css" rel="stylesheet"/>
|
||||||
<script fetchpriority="low" src="/js/htmx.min.js"></script>
|
<script src="/js/htmx.min.js"></script>
|
||||||
|
<script src="/js/head-support.min.js"></script>
|
||||||
<script src="/js/preload.min.js"></script>
|
<script src="/js/preload.min.js"></script>
|
||||||
|
<script src="/js/preload-links.min.js"></script>
|
||||||
<script src={ cfg.Tracking.ScriptSrc } data-website-id={ cfg.Tracking.WebsiteID }></script>
|
<script src={ cfg.Tracking.ScriptSrc } data-website-id={ cfg.Tracking.WebsiteID }></script>
|
||||||
|
for _, h := range templer.GetHeader(ctx) {
|
||||||
|
@templ.Raw(h)
|
||||||
|
}
|
||||||
</head>
|
</head>
|
||||||
<body class="bg-black dark" hx-boost="true" hx-target="main" hx-ext="preload">
|
{ children... }
|
||||||
|
}
|
||||||
|
|
||||||
|
templ Base(cfg config.Cfg, title string) {
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang={ translate.Language(ctx) }>
|
||||||
|
@Head(cfg, title)
|
||||||
|
<body class="bg-black dark" hx-boost="true" hx-target="main" hx-ext="head-support,preload,preload-links">
|
||||||
<header
|
<header
|
||||||
class="fixed z-50 mix-blend-difference p-1 w-full flex justify-between items-start"
|
class="fixed z-50 mix-blend-difference p-1 w-full flex justify-between items-start"
|
||||||
>
|
>
|
||||||
<a class="text-white text-2xl" href={ translate.Path(ctx, "") }>schreifuchs.ch</a>
|
<a class="text-white text-2xl" href={ translate.Path(ctx, "") } preload="mouseover" preload-images="true">schreifuchs.ch</a>
|
||||||
<nav class="flex gap-2 p-2">
|
<nav class="flex gap-2 p-2">
|
||||||
for _, lang := range translate.SupportedLanguages(ctx) {
|
for _, lang := range translate.SupportedLanguages(ctx) {
|
||||||
<a
|
<a
|
||||||
href={ translate.PathForLang(ctx, lang, strings.Split(templer.Path(ctx), "/")...) }
|
href={ translate.PathForLang(ctx, lang, strings.Split(templer.Path(ctx), "/")...) }
|
||||||
hreflang={ lang }
|
hreflang={ lang }
|
||||||
data-no-translate
|
data-no-translate
|
||||||
|
preload="mouseover"
|
||||||
class="text-white hover:underline uppercase"
|
class="text-white hover:underline uppercase"
|
||||||
>
|
>
|
||||||
{ lang }
|
{ lang }
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/config"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/schreifuchs.ch/internal/pkg/templer"
|
||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,10 +28,15 @@ func (l *Layouts) Render(ctx context.Context, w io.Writer, r *http.Request, comp
|
|||||||
withLayout := !r.URL.Query().Has("c")
|
withLayout := !r.URL.Query().Has("c")
|
||||||
fmt.Println("layout: ", withLayout)
|
fmt.Println("layout: ", withLayout)
|
||||||
|
|
||||||
|
title := templer.GetTitle(ctx, "Schreifuchs")
|
||||||
|
|
||||||
slog.Debug("HX-Request", "value", r.Header.Get("HX-Request"))
|
slog.Debug("HX-Request", "value", r.Header.Get("HX-Request"))
|
||||||
if r.Header.Get("HX-Request") != "true" {
|
if r.Header.Get("HX-Request") != "true" {
|
||||||
ctx = templ.WithChildren(ctx, component)
|
ctx = templ.WithChildren(ctx, component)
|
||||||
component = Base(l.cfg, "Schreifuchs")
|
component = Base(l.cfg, title)
|
||||||
|
} else {
|
||||||
|
ctx = templ.WithChildren(ctx, component)
|
||||||
|
component = Head(l.cfg, title)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := component.Render(ctx, w); err != nil {
|
if err := component.Render(ctx, w); err != nil {
|
||||||
|
|||||||
@@ -9,7 +9,12 @@ import (
|
|||||||
templ Home(pages []page.PageHeader) {
|
templ Home(pages []page.PageHeader) {
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
@components.ImageTile("/images/" + page.CoverImageUID) {
|
@components.ImageTile("/images/" + page.CoverImageUID) {
|
||||||
<a class="flex items-center justify-center h-full " href={ translate.Path(ctx, "/"+page.UID) } preload="mouseover">
|
<a
|
||||||
|
class="flex items-center justify-center h-full "
|
||||||
|
href={ translate.Path(ctx, "/"+page.UID) }
|
||||||
|
preload="mouseover"
|
||||||
|
hx-load-links="true"
|
||||||
|
>
|
||||||
<h2
|
<h2
|
||||||
class="text-3xl font-bold text-white hover:underline text-shadow-lg"
|
class="text-3xl font-bold text-white hover:underline text-shadow-lg"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* @file preload-links.js
|
||||||
|
* @description HTMX extension to extract <link rel="preload"> from preloaded HTML responses.
|
||||||
|
* @generated By opencode (AI Agent)
|
||||||
|
* @model gemini-3-flash-preview
|
||||||
|
* @date Thu Mar 19 2026
|
||||||
|
*/
|
||||||
|
(function () {
|
||||||
|
// Save the original XHR methods
|
||||||
|
const originalOpen = XMLHttpRequest.prototype.open;
|
||||||
|
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
|
||||||
|
const originalSend = XMLHttpRequest.prototype.send;
|
||||||
|
|
||||||
|
// We need to track which XHRs are preloads
|
||||||
|
XMLHttpRequest.prototype.open = function () {
|
||||||
|
this._isHtmxPreload = false;
|
||||||
|
return originalOpen.apply(this, arguments);
|
||||||
|
};
|
||||||
|
|
||||||
|
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
|
||||||
|
if (header.toLowerCase() === "hx-preloaded" && value === "true") {
|
||||||
|
this._isHtmxPreload = true;
|
||||||
|
}
|
||||||
|
return originalSetRequestHeader.apply(this, arguments);
|
||||||
|
};
|
||||||
|
|
||||||
|
XMLHttpRequest.prototype.send = function () {
|
||||||
|
if (this._isHtmxPreload) {
|
||||||
|
this.addEventListener("load", function () {
|
||||||
|
// Parse the response text as HTML
|
||||||
|
const parser = new DOMParser();
|
||||||
|
const doc = parser.parseFromString(this.responseText, "text/html");
|
||||||
|
|
||||||
|
// Find all preload links
|
||||||
|
const links = doc.querySelectorAll('link[rel="preload"]');
|
||||||
|
|
||||||
|
links.forEach((link) => {
|
||||||
|
const href = link.getAttribute("href");
|
||||||
|
// Only inject if the current document doesn't already have it
|
||||||
|
if (
|
||||||
|
href &&
|
||||||
|
!document.querySelector(`link[rel="preload"][href="${href}"]`)
|
||||||
|
) {
|
||||||
|
const newLink = document.createElement("link");
|
||||||
|
newLink.rel = "preload";
|
||||||
|
newLink.href = href;
|
||||||
|
|
||||||
|
// Copy relevant attributes
|
||||||
|
["as", "type", "media", "fetchpriority", "crossorigin"].forEach(
|
||||||
|
(attr) => {
|
||||||
|
const val = link.getAttribute(attr);
|
||||||
|
if (val) newLink.setAttribute(attr, val);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
document.head.appendChild(newLink);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return originalSend.apply(this, arguments);
|
||||||
|
};
|
||||||
|
})();
|
||||||
File diff suppressed because one or more lines are too long
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
(function(){var H=null;function M(){}function a(e,t){if(e&&e.indexOf("<head")>-1){const g=document.createElement("html");var r=e.replace(/<svg(\s[^>]*>|>)([\s\S]*?)<\/svg>/gim,"");var a=r.match(/(<head(\s[^>]*>|>)([\s\S]*?)<\/head>)/im);if(a){var n=[];var d=[];var o=[];var i=[];g.innerHTML=a;var h=g.querySelector("head");var m=document.head;if(h==null){return}else{var s=new Map;for(const p of h.children){s.set(p.outerHTML,p)}}var u=H.getAttributeValue(h,"hx-head")||t;for(const x of m.children){var l=s.has(x.outerHTML);var f=x.getAttribute("hx-head")==="re-eval";var v=H.getAttributeValue(x,"hx-preserve")==="true";if(l||v){if(f){d.push(x)}else{s.delete(x.outerHTML);o.push(x)}}else{if(u==="append"){if(f){d.push(x);i.push(x)}}else{if(H.triggerEvent(document.body,"htmx:removingHeadElement",{headElement:x})!==false){d.push(x)}}}}i.push(...s.values());M("to append: ",i);for(const E of i){M("adding: ",E);var c=document.createRange().createContextualFragment(E.outerHTML);M(c);if(H.triggerEvent(document.body,"htmx:addingHeadElement",{headElement:c})!==false){m.appendChild(c);n.push(c)}}for(const b of d){if(H.triggerEvent(document.body,"htmx:removingHeadElement",{headElement:b})!==false){m.removeChild(b)}}H.triggerEvent(document.body,"htmx:afterHeadMerge",{added:n,kept:o,removed:d})}}}htmx.defineExtension("head-support",{init:function(e){H=e;htmx.on("htmx:afterSwap",function(e){let t=e.detail.xhr;if(t){var r=t.response;if(H.triggerEvent(document.body,"htmx:beforeHeadMerge",e.detail)){a(r,e.detail.boosted?"merge":"append")}}});htmx.on("htmx:historyRestore",function(e){if(H.triggerEvent(document.body,"htmx:beforeHeadMerge",e.detail)){if(e.detail.cacheMiss){a(e.detail.serverResponse,"merge")}else{a(e.detail.item.head,"merge")}}});htmx.on("htmx:historyItemCreated",function(e){var t=e.detail.item;t.head=document.head.outerHTML})}})})();
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
(function(){const p=XMLHttpRequest.prototype.open,a=XMLHttpRequest.prototype.setRequestHeader,l=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(){return this._isHtmxPreload=!1,p.apply(this,arguments)},XMLHttpRequest.prototype.setRequestHeader=function(r,o){return r.toLowerCase()==="hx-preloaded"&&o==="true"&&(this._isHtmxPreload=!0),a.apply(this,arguments)},XMLHttpRequest.prototype.send=function(){return this._isHtmxPreload&&this.addEventListener("load",function(){new DOMParser().parseFromString(this.responseText,"text/html").querySelectorAll('link[rel="preload"]').forEach(n=>{const t=n.getAttribute("href");if(t&&!document.querySelector(`link[rel="preload"][href="${t}"]`)){const e=document.createElement("link");e.rel="preload",e.href=t,["as","type","media","fetchpriority","crossorigin"].forEach(s=>{const i=n.getAttribute(s);i&&e.setAttribute(s,i)}),document.head.appendChild(e)}})}),l.apply(this,arguments)}})();
|
||||||
Reference in New Issue
Block a user