fix: conditional client build
This commit is contained in:
@ -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, "/")
|
||||
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user