fix: conditional client build
This commit is contained in:
parent
695e1d94fd
commit
9062609eec
@ -9,4 +9,5 @@ result
|
|||||||
|
|
||||||
# Server
|
# Server
|
||||||
/server/client
|
/server/client
|
||||||
/server/tmp
|
/server/tmp
|
||||||
|
/server/internal/handlers/client/client
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -9,4 +9,5 @@ result
|
|||||||
|
|
||||||
# Server
|
# Server
|
||||||
/server/client
|
/server/client
|
||||||
/server/tmp
|
/server/tmp
|
||||||
|
/server/internal/handlers/client/client
|
@ -69,6 +69,9 @@
|
|||||||
# Svelte frontend
|
# Svelte frontend
|
||||||
nodejs_22
|
nodejs_22
|
||||||
|
|
||||||
|
# Nix
|
||||||
|
nix-update
|
||||||
|
|
||||||
# Helper scripts
|
# Helper scripts
|
||||||
(writeShellApplication {
|
(writeShellApplication {
|
||||||
name = "run";
|
name = "run";
|
||||||
@ -117,7 +120,7 @@
|
|||||||
vendorHash = "sha256-FyqcKhJy58uL3UiGA9tg7pSt0OQ1NIZw+khTictyzcw=";
|
vendorHash = "sha256-FyqcKhJy58uL3UiGA9tg7pSt0OQ1NIZw+khTictyzcw=";
|
||||||
|
|
||||||
preBuild = ''
|
preBuild = ''
|
||||||
cp -r ${client} client
|
cp -r ${client} internal/handlers/client/client
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -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)
|
|
||||||
}
|
|
17
server/internal/handlers/client/client.go
Normal file
17
server/internal/handlers/client/client.go
Normal 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()
|
||||||
|
}
|
16
server/internal/handlers/client/client_prod.go
Normal file
16
server/internal/handlers/client/client_prod.go
Normal 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
|
||||||
|
}
|
@ -18,40 +18,51 @@ func WithAuthRedirect(next http.Handler, key string) http.Handler {
|
|||||||
pathItems := strings.Split(r.URL.Path, "/")
|
pathItems := strings.Split(r.URL.Path, "/")
|
||||||
|
|
||||||
if len(pathItems) < 2 {
|
if len(pathItems) < 2 {
|
||||||
next.ServeHTTP(w, r)
|
http.Error(w, "Not found", http.StatusNotFound)
|
||||||
return
|
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] {
|
switch pathItems[1] {
|
||||||
|
|
||||||
case "auth":
|
case "auth":
|
||||||
fallthrough
|
if authenticated {
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
case "_app":
|
case "_app":
|
||||||
fallthrough
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
case "favicon.png":
|
case "favicon.png":
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Check if the request contains a valid cookie token
|
if authenticated {
|
||||||
cookies := getCookies(r.Header.Get("Cookie"))
|
next.ServeHTTP(w, r)
|
||||||
for _, cookie := range cookies {
|
return
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise redirect
|
// Redirect if not authenticated
|
||||||
http.Redirect(w, r, "/auth", http.StatusFound)
|
http.Redirect(w, r, "/auth", http.StatusFound)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -20,11 +19,9 @@ import (
|
|||||||
|
|
||||||
"github.com/spotdemo4/trevstack/server/internal/database"
|
"github.com/spotdemo4/trevstack/server/internal/database"
|
||||||
"github.com/spotdemo4/trevstack/server/internal/handlers"
|
"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 {
|
type env struct {
|
||||||
DBType string
|
DBType string
|
||||||
DBUser string
|
DBUser string
|
||||||
@ -116,7 +113,7 @@ func main() {
|
|||||||
|
|
||||||
// Serve web interface
|
// Serve web interface
|
||||||
mux := http.NewServeMux()
|
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("/file/", handlers.NewFileHandler(db, env.Key))
|
||||||
mux.Handle("/grpc/", http.StripPrefix("/grpc", api))
|
mux.Handle("/grpc/", http.StripPrefix("/grpc", api))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user