From 9d4af37c222db7f68907a377bd24b41eeafe3846 Mon Sep 17 00:00:00 2001 From: Robert Janetzko Date: Fri, 15 Apr 2022 14:46:51 +0000 Subject: [PATCH] embed frontend --- .gitignore | 3 +- backend/.gitignore | 1 + backend/go.mod | 1 - backend/go.sum | 2 - backend/main.go | 65 ++++++++++++++++--- backend/server/servestatic.go | 33 ---------- frontend/angular.json | 2 +- .../app/pages/entity/entity.component.html | 2 +- 8 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 backend/.gitignore delete mode 100644 backend/server/servestatic.go diff --git a/.gitignore b/.gitignore index ca41d04..42df474 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ legendsbrowser *.pprof *.png -/*.json \ No newline at end of file +/*.json +.DS_Store \ No newline at end of file diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..b78fa79 --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1 @@ +resources/frontend/* \ No newline at end of file diff --git a/backend/go.mod b/backend/go.mod index 20eaaae..8e81a77 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -4,6 +4,5 @@ go 1.18 require ( github.com/gorilla/mux v1.8.0 - github.com/iancoleman/strcase v0.2.0 github.com/pkg/profile v1.6.0 ) diff --git a/backend/go.sum b/backend/go.sum index ff24b51..f561cef 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,6 +1,4 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= -github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM= github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= diff --git a/backend/main.go b/backend/main.go index afc7f8c..efa8751 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,8 +1,10 @@ package main import ( + "embed" "flag" "fmt" + "io/fs" "net/http" _ "net/http/pprof" "os" @@ -16,10 +18,15 @@ import ( var world *model.DfWorld +//go:embed resources/frontend +var frontend embed.FS + func main() { f := flag.String("f", "", "open a file") flag.Parse() + router := mux.NewRouter().StrictSlash(true) + if len(*f) > 0 { defer profile.Start(profile.MemProfile).Stop() go func() { @@ -55,8 +62,6 @@ func main() { // model.ListOtherElements("poeticForm", &world.PoeticForms) // model.ListOtherElements("written", &world.WrittenContents) - router := mux.NewRouter().StrictSlash(true) - server.RegisterResource(router, "region", world.Regions) // server.RegisterResource(router, "undergroundRegion", world.UndergroundRegions) server.RegisterResource(router, "landmass", world.Landmasses) @@ -72,12 +77,56 @@ func main() { server.RegisterResource(router, "musicalForm", world.MusicalForms) server.RegisterResource(router, "poeticForm", world.PoeticForms) // server.RegisterResource(router, "written", world.WrittenContents) - - spa := server.SpaHandler{StaticPath: "frontend/dist/legendsbrowser", IndexPath: "index.html"} - router.PathPrefix("/").Handler(spa) - - fmt.Println("Serving at :8080") - http.ListenAndServe(":8080", router) } + spa := spaHandler{staticFS: frontend, staticPath: "resources/frontend", indexPath: "index.html"} + router.PathPrefix("/").Handler(spa) + + fmt.Println("Serving at :8080") + http.ListenAndServe(":8080", router) + +} + +type spaHandler struct { + staticFS embed.FS + staticPath string + indexPath string +} + +func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // get the absolute path to prevent directory traversal + path := r.URL.Path + // if err != nil { + // // if we failed to get the absolute path respond with a 400 bad request and stop + // http.Error(w, err.Error(), http.StatusBadRequest) + // return + // } + // prepend the path with the path to the static directory + path = h.staticPath + path + fmt.Println(path) + + _, err := h.staticFS.Open(path) + if os.IsNotExist(err) { + // file does not exist, serve index.html + fmt.Println(path) + index, err := h.staticFS.ReadFile(h.staticPath + "/" + h.indexPath) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusAccepted) + w.Write(index) + return + } else if err != nil { + // if we got an error (that wasn't that the file doesn't exist) stating the + // file, return a 500 internal server error and stop + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // get the subdirectory of the static dir + statics, err := fs.Sub(h.staticFS, h.staticPath) + // otherwise, use http.FileServer to serve the static dir + http.FileServer(http.FS(statics)).ServeHTTP(w, r) } diff --git a/backend/server/servestatic.go b/backend/server/servestatic.go deleted file mode 100644 index 69f606b..0000000 --- a/backend/server/servestatic.go +++ /dev/null @@ -1,33 +0,0 @@ -package server - -import ( - "net/http" - "os" - "path/filepath" -) - -type SpaHandler struct { - StaticPath string - IndexPath string -} - -func (h SpaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - path, err := filepath.Abs(r.URL.Path) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - path = filepath.Join(h.StaticPath, path) - - _, err = os.Stat(path) - if os.IsNotExist(err) { - http.ServeFile(w, r, filepath.Join(h.StaticPath, h.IndexPath)) - return - } else if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - http.FileServer(http.Dir(h.StaticPath)).ServeHTTP(w, r) -} diff --git a/frontend/angular.json b/frontend/angular.json index 471c7fc..16225ed 100644 --- a/frontend/angular.json +++ b/frontend/angular.json @@ -20,7 +20,7 @@ "build": { "builder": "@angular-devkit/build-angular:browser", "options": { - "outputPath": "dist/legendsbrowser", + "outputPath": "../backend/resources/frontend", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", diff --git a/frontend/src/app/pages/entity/entity.component.html b/frontend/src/app/pages/entity/entity.component.html index 023b14d..cb2333c 100644 --- a/frontend/src/app/pages/entity/entity.component.html +++ b/frontend/src/app/pages/entity/entity.component.html @@ -1 +1 @@ -

{{entity.name}}

\ No newline at end of file +

Entity {{entity.name}}

\ No newline at end of file