embed frontend
This commit is contained in:
parent
729585ae2f
commit
9d4af37c22
|
@ -3,4 +3,5 @@
|
||||||
legendsbrowser
|
legendsbrowser
|
||||||
*.pprof
|
*.pprof
|
||||||
*.png
|
*.png
|
||||||
/*.json
|
/*.json
|
||||||
|
.DS_Store
|
|
@ -0,0 +1 @@
|
||||||
|
resources/frontend/*
|
|
@ -4,6 +4,5 @@ go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/iancoleman/strcase v0.2.0
|
|
||||||
github.com/pkg/profile v1.6.0
|
github.com/pkg/profile v1.6.0
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
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 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM=
|
||||||
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
|
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
|
@ -16,10 +18,15 @@ import (
|
||||||
|
|
||||||
var world *model.DfWorld
|
var world *model.DfWorld
|
||||||
|
|
||||||
|
//go:embed resources/frontend
|
||||||
|
var frontend embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
f := flag.String("f", "", "open a file")
|
f := flag.String("f", "", "open a file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
router := mux.NewRouter().StrictSlash(true)
|
||||||
|
|
||||||
if len(*f) > 0 {
|
if len(*f) > 0 {
|
||||||
defer profile.Start(profile.MemProfile).Stop()
|
defer profile.Start(profile.MemProfile).Stop()
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -55,8 +62,6 @@ func main() {
|
||||||
// model.ListOtherElements("poeticForm", &world.PoeticForms)
|
// model.ListOtherElements("poeticForm", &world.PoeticForms)
|
||||||
// model.ListOtherElements("written", &world.WrittenContents)
|
// model.ListOtherElements("written", &world.WrittenContents)
|
||||||
|
|
||||||
router := mux.NewRouter().StrictSlash(true)
|
|
||||||
|
|
||||||
server.RegisterResource(router, "region", world.Regions)
|
server.RegisterResource(router, "region", world.Regions)
|
||||||
// server.RegisterResource(router, "undergroundRegion", world.UndergroundRegions)
|
// server.RegisterResource(router, "undergroundRegion", world.UndergroundRegions)
|
||||||
server.RegisterResource(router, "landmass", world.Landmasses)
|
server.RegisterResource(router, "landmass", world.Landmasses)
|
||||||
|
@ -72,12 +77,56 @@ func main() {
|
||||||
server.RegisterResource(router, "musicalForm", world.MusicalForms)
|
server.RegisterResource(router, "musicalForm", world.MusicalForms)
|
||||||
server.RegisterResource(router, "poeticForm", world.PoeticForms)
|
server.RegisterResource(router, "poeticForm", world.PoeticForms)
|
||||||
// server.RegisterResource(router, "written", world.WrittenContents)
|
// 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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -20,7 +20,7 @@
|
||||||
"build": {
|
"build": {
|
||||||
"builder": "@angular-devkit/build-angular:browser",
|
"builder": "@angular-devkit/build-angular:browser",
|
||||||
"options": {
|
"options": {
|
||||||
"outputPath": "dist/legendsbrowser",
|
"outputPath": "../backend/resources/frontend",
|
||||||
"index": "src/index.html",
|
"index": "src/index.html",
|
||||||
"main": "src/main.ts",
|
"main": "src/main.ts",
|
||||||
"polyfills": "src/polyfills.ts",
|
"polyfills": "src/polyfills.ts",
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
<h1>{{entity.name}}</h1>
|
<h1>Entity {{entity.name}}</h1>
|
Loading…
Reference in New Issue