From 49bf320b3ff62aa24bef3cf9355d4b7a3f1fbd55 Mon Sep 17 00:00:00 2001 From: Vishnu Bharathi Date: Sat, 21 Dec 2024 12:40:03 +0530 Subject: [PATCH] finally! --- cmd/server.go | 5 ++++- webapp/webapp.go | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index a922a95..507ae24 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -50,7 +50,10 @@ func runServer(cmd *cobra.Command, args []string) { go func() { webServerAddr := ":5555" // TODO make this come from a config log.Println("Starting webapp server at", webServerAddr) - webServer := webapp.New(webServerAddr) + webServer, err := webapp.New(webServerAddr) + if err != nil { + log.Fatalln("Error creating web app server", err) + } if err := webServer.ListenAndServe(); err != nil { log.Fatalln("Error starting web server", err) } diff --git a/webapp/webapp.go b/webapp/webapp.go index 2c3a09e..6bd7860 100644 --- a/webapp/webapp.go +++ b/webapp/webapp.go @@ -1,7 +1,9 @@ package webapp import ( + "embed" "fmt" + "io/fs" "net/http" ) @@ -9,17 +11,23 @@ type Server struct { *http.Server } -func New(addr string) *Server { +//go:embed dist +var WebAssets embed.FS + +func New(addr string) (*Server, error) { routes := http.NewServeMux() - routes.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "TODO") - }) + reactApp, err := fs.Sub(WebAssets, "dist") + if err != nil { + return nil, fmt.Errorf("error finding the dist folder: %w", err) + } + + routes.Handle("/", http.FileServerFS(reactApp)) return &Server{ Server: &http.Server{ Addr: addr, Handler: routes, }, - } + }, nil }