Add root route

This commit is contained in:
Daniel 2024-08-08 22:01:03 -04:00
parent 1d4eac2a89
commit cc54271ee1
3 changed files with 30 additions and 6 deletions

View file

@ -5,3 +5,4 @@ all:
generate:
sqlc generate -f sql/sqlc.yaml
protoc -I=pkg/pb/ --go_out=pkg/ pkg/pb/stillbox.proto

7
client/client.go Normal file
View file

@ -0,0 +1,7 @@
package client
import (
"embed"
)
var Calls embed.FS

View file

@ -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) {
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"))
*/
})
}