From 7d61f46b2ec35bd6af5f080a2133a7516c02b2da Mon Sep 17 00:00:00 2001 From: SeniuuS Date: Wed, 13 Apr 2022 11:43:38 +0200 Subject: [PATCH] [feature/static_serve] adding pathprefix app-array for front-end (using build folder of react build) --- main.go | 1 + muxroutersignalr.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 9ab022a..058daa5 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ func main() { Router: mux.NewRouter(), } router.AddHandledFunctions() + router.AddStaticFiles() router.RegisterSignalRRoute("/model", NewModelHub()) log.Println("SignalR server created") diff --git a/muxroutersignalr.go b/muxroutersignalr.go index d0e8991..d2bbd78 100644 --- a/muxroutersignalr.go +++ b/muxroutersignalr.go @@ -19,15 +19,25 @@ type MuxRouterSignalR struct { } func (router *MuxRouterSignalR) HandleFunc(path string, f func(w http.ResponseWriter, r *http.Request)) { - router.NewRoute().Path(path).HandlerFunc(f) + router.HandleFuncRoute(path, f) +} + +func (router *MuxRouterSignalR) HandleFuncRoute(path string, f func(w http.ResponseWriter, r *http.Request)) *mux.Route { + route := router.NewRoute().Path(path).HandlerFunc(f) router.paths = append(router.paths, path) log.Printf("Route %s registered\n", path) + return route } func (router *MuxRouterSignalR) Handle(path string, handler http.Handler) { - router.NewRoute().Path(path).Handler(handler) + router.HandleRoute(path, handler) +} + +func (router *MuxRouterSignalR) HandleRoute(path string, handler http.Handler) *mux.Route { + route := router.NewRoute().Path(path).Handler(handler) router.paths = append(router.paths, path) log.Printf("Route %s registered\n", path) + return route } func WithMuxRouter(router *MuxRouterSignalR) func() signalr.MappableRouter { @@ -36,6 +46,10 @@ func WithMuxRouter(router *MuxRouterSignalR) func() signalr.MappableRouter { } } +func (router *MuxRouterSignalR) AddStaticFiles() { + router.PathPrefix("/app-array/").Handler(http.StripPrefix("/app-array/", http.FileServer(http.Dir("./build/")))) +} + func (router *MuxRouterSignalR) AddHandledFunctions() { router.HandleFunc("/models/{id}", func(writer http.ResponseWriter, request *http.Request) { vars := mux.Vars(request)