From cc54271ee14ee13d98c2685caba1cd1a72ac5a8e Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Thu, 8 Aug 2024 22:01:03 -0400 Subject: [PATCH] Add root route --- Makefile | 1 + client/client.go | 7 +++++++ pkg/gordio/server/routes.go | 28 ++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 client/client.go diff --git a/Makefile b/Makefile index a31a040..1d685d1 100644 --- a/Makefile +++ b/Makefile @@ -5,3 +5,4 @@ all: generate: sqlc generate -f sql/sqlc.yaml protoc -I=pkg/pb/ --go_out=pkg/ pkg/pb/stillbox.proto + diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..78300c1 --- /dev/null +++ b/client/client.go @@ -0,0 +1,7 @@ +package client + +import ( + "embed" +) + +var Calls embed.FS diff --git a/pkg/gordio/server/routes.go b/pkg/gordio/server/routes.go index c96a560..f5ffb23 100644 --- a/pkg/gordio/server/routes.go +++ b/pkg/gordio/server/routes.go @@ -1,9 +1,12 @@ package server import ( + "io/fs" "net/http" + "strings" "time" + "dynatron.me/x/stillbox/client" "dynatron.me/x/stillbox/pkg/gordio/database" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" @@ -12,6 +15,11 @@ import ( ) func (s *Server) setupRoutes() { + clientRoot, err := fs.Sub(client.Calls, "calls/build/web") + if err != nil { + panic(err) + } + r := s.r r.Use(middleware.WithValue(database.DBCTXKeyValue, s.db)) @@ -34,7 +42,7 @@ func (s *Server) setupRoutes() { // optional auth routes - r.Get("/", s.routeIndex) + s.clientRoute(r, clientRoot) }) } @@ -42,9 +50,17 @@ func rateLimiter() func(http.Handler) http.Handler { return httprate.LimitByRealIP(100, 1*time.Minute) } -func (s *Server) routeIndex(w http.ResponseWriter, r *http.Request) { - if cl, authenticated := s.auth.Authenticated(r); authenticated { - w.Write([]byte("Hello " + cl["user"].(string) + "\n")) - } - w.Write([]byte("Welcome to gordio\n")) +func (s *Server) clientRoute(r chi.Router, clientRoot fs.FS) { + r.Get("/*", func(w http.ResponseWriter, r *http.Request) { + rctx := chi.RouteContext(r.Context()) + pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*") + fs := http.StripPrefix(pathPrefix, http.FileServer(http.FS(clientRoot))) + fs.ServeHTTP(w, r) + /* + if cl, authenticated := s.auth.Authenticated(r); authenticated { + w.Write([]byte("Hello " + cl["user"].(string) + "\n")) + } + w.Write([]byte("Welcome to gordio\n")) + */ + }) }