fix: conditional client build

This commit is contained in:
trev 2025-03-18 14:18:24 -04:00
parent 695e1d94fd
commit 9062609eec
8 changed files with 74 additions and 47 deletions

View File

@ -9,4 +9,5 @@ result
# Server
/server/client
/server/tmp
/server/tmp
/server/internal/handlers/client/client

3
.gitignore vendored
View File

@ -9,4 +9,5 @@ result
# Server
/server/client
/server/tmp
/server/tmp
/server/internal/handlers/client/client

View File

@ -69,6 +69,9 @@
# Svelte frontend
nodejs_22
# Nix
nix-update
# Helper scripts
(writeShellApplication {
name = "run";
@ -117,7 +120,7 @@
vendorHash = "sha256-FyqcKhJy58uL3UiGA9tg7pSt0OQ1NIZw+khTictyzcw=";
preBuild = ''
cp -r ${client} client
cp -r ${client} internal/handlers/client/client
'';
};
}

View File

@ -1,19 +0,0 @@
package handlers
import (
"embed"
"io/fs"
"log"
"net/http"
"github.com/spotdemo4/trevstack/server/internal/interceptors"
)
func NewClientHandler(client embed.FS, key string) http.Handler {
clientFs, err := fs.Sub(client, "client")
if err != nil {
log.Fatalf("failed to get sub filesystem: %v", err)
}
return interceptors.WithAuthRedirect(http.FileServer(http.FS(clientFs)), key)
}

View File

@ -0,0 +1,17 @@
package client
import (
"net/http"
"github.com/spotdemo4/trevstack/server/internal/interceptors"
)
var embedfs *http.FileSystem
func NewClientHandler(key string) http.Handler {
if embedfs != nil {
return interceptors.WithAuthRedirect(http.FileServer(*embedfs), key)
}
return http.NotFoundHandler()
}

View File

@ -0,0 +1,16 @@
// go:build !dev
package client
import (
"embed"
"net/http"
)
//go:embed client
var eclient embed.FS
func init() {
fs := http.FS(eclient)
embedfs = &fs
}

View File

@ -18,40 +18,51 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler {
pathItems := strings.Split(r.URL.Path, "/")
if len(pathItems) < 2 {
next.ServeHTTP(w, r)
http.Error(w, "Not found", http.StatusNotFound)
return
}
// Check if the user is authenticated
authenticated := false
cookies := getCookies(r.Header.Get("Cookie"))
for _, cookie := range cookies {
if cookie.Name == "token" {
subject, err := validateToken(cookie.Value, key)
if err == nil {
ctx, err := newUserContext(r.Context(), subject)
if err == nil {
r = r.WithContext(ctx)
authenticated = true
}
}
break
}
}
switch pathItems[1] {
case "auth":
fallthrough
if authenticated {
http.Redirect(w, r, "/", http.StatusFound)
return
}
next.ServeHTTP(w, r)
case "_app":
fallthrough
next.ServeHTTP(w, r)
case "favicon.png":
next.ServeHTTP(w, r)
return
default:
// Check if the request contains a valid cookie token
cookies := getCookies(r.Header.Get("Cookie"))
for _, cookie := range cookies {
if cookie.Name == "token" {
subject, err := validateToken(cookie.Value, key)
if err == nil {
ctx, err := newUserContext(r.Context(), subject)
if err == nil {
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
return
}
}
}
if authenticated {
next.ServeHTTP(w, r)
return
}
// Otherwise redirect
// Redirect if not authenticated
http.Redirect(w, r, "/auth", http.StatusFound)
return
}
})
}

View File

@ -2,7 +2,6 @@ package main
import (
"context"
"embed"
"fmt"
"log"
"net/http"
@ -20,11 +19,9 @@ import (
"github.com/spotdemo4/trevstack/server/internal/database"
"github.com/spotdemo4/trevstack/server/internal/handlers"
"github.com/spotdemo4/trevstack/server/internal/handlers/client"
)
//go:embed all:client
var client embed.FS
type env struct {
DBType string
DBUser string
@ -116,7 +113,7 @@ func main() {
// Serve web interface
mux := http.NewServeMux()
mux.Handle("/", handlers.NewClientHandler(client, env.Key))
mux.Handle("/", client.NewClientHandler(env.Key))
mux.Handle("/file/", handlers.NewFileHandler(db, env.Key))
mux.Handle("/grpc/", http.StripPrefix("/grpc", api))