mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-11-05 04:14:12 +00:00
Not Sam
b5599eb055
Co-authored-by: Sam Therapy <sam@samtherapy.net> Reviewed-on: https://git.freecumextremist.com/grumbulon/pomme/pulls/13
39 lines
760 B
Go
39 lines
760 B
Go
// Package frontend is where the frontend served by pomme comes from.
|
|
package frontend
|
|
|
|
import (
|
|
"embed"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
//go:generate npm run build
|
|
//go:embed all:build
|
|
var files embed.FS
|
|
|
|
// SvelteKitHandler -- I stole this lol.
|
|
func SvelteKitHandler(path string) http.Handler {
|
|
fsys, err := fs.Sub(files, "build")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
filesystem := http.FS(fsys)
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.TrimPrefix(r.URL.Path, path) //nolint: govet
|
|
|
|
_, err := filesystem.Open(path)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
path = fmt.Sprintf("%s.html", path)
|
|
}
|
|
|
|
r.URL.Path = path
|
|
http.FileServer(filesystem).ServeHTTP(w, r)
|
|
})
|
|
}
|