dorfylegends/backend/server/server.go

219 lines
7.6 KiB
Go
Raw Normal View History

2022-04-26 10:24:16 +03:00
package server
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
2022-04-28 21:14:33 +03:00
"sort"
2022-04-30 10:45:31 +03:00
"strconv"
2022-04-26 10:24:16 +03:00
"github.com/gorilla/mux"
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
2022-04-27 22:44:39 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
2022-04-26 10:24:16 +03:00
)
2022-04-26 18:39:24 +03:00
type DfServerContext struct {
world *model.DfWorld
isLoading bool
2022-04-26 22:00:09 +03:00
progress *model.LoadProgress
2022-04-26 18:39:24 +03:00
}
2022-04-26 10:24:16 +03:00
type DfServer struct {
router *mux.Router
2022-04-26 22:00:09 +03:00
loader *loadHandler
2022-04-26 10:24:16 +03:00
templates *templates.Template
2022-04-26 18:39:24 +03:00
context *DfServerContext
2022-04-26 10:24:16 +03:00
}
func StartServer(world *model.DfWorld, static embed.FS) {
srv := &DfServer{
router: mux.NewRouter().StrictSlash(true),
2022-04-26 18:39:24 +03:00
context: &DfServerContext{
world: world,
isLoading: false,
2022-04-26 22:00:09 +03:00
progress: &model.LoadProgress{},
2022-04-26 18:39:24 +03:00
},
2022-04-26 10:24:16 +03:00
}
2022-04-26 22:00:09 +03:00
srv.loader = &loadHandler{server: srv}
2022-04-26 10:24:16 +03:00
srv.LoadTemplates()
2022-04-27 22:44:39 +03:00
srv.RegisterWorldPage("/entities", "entities.html", func(p Parms) any { return grouped(srv.context.world.Entities) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/entity/{id}", "entity.html", func(id int) any { return srv.context.world.Entities[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/entity/{id}", "popoverEntity.html", func(id int) any { return srv.context.world.Entities[id] })
2022-04-27 22:44:39 +03:00
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/regions", "regions.html", func(p Parms) any { return grouped(srv.context.world.Regions) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/region/{id}", "region.html", func(id int) any { return srv.context.world.Regions[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/region/{id}", "popoverRegion.html", func(id int) any { return srv.context.world.Regions[id] })
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/sites", "sites.html", func(p Parms) any { return grouped(srv.context.world.Sites) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/site/{id}", "site.html", func(id int) any { return srv.context.world.Sites[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/site/{id}", "popoverSite.html", func(id int) any { return srv.context.world.Sites[id] })
2022-04-28 22:24:55 +03:00
2022-04-29 15:21:27 +03:00
srv.RegisterWorldPage("/structures", "structures.html", func(p Parms) any {
return flatGrouped(srv.context.world.Sites, func(s *model.Site) []*model.Structure { return util.Values(s.Structures) })
})
2022-04-30 15:27:42 +03:00
srv.RegisterWorldPage("/site/{siteId}/structure/{id}", "structure.html", srv.findStructure)
srv.RegisterWorldPage("/popover/site/{siteId}/structure/{id}", "popoverStructure.html", srv.findStructure)
2022-04-29 15:21:27 +03:00
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/worldconstructions", "worldconstructions.html", func(p Parms) any { return grouped(srv.context.world.WorldConstructions) })
srv.RegisterWorldResourcePage("/worldconstruction/{id}", "worldconstruction.html", func(id int) any { return srv.context.world.WorldConstructions[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/worldconstruction/{id}", "popoverWorldconstruction.html", func(id int) any { return srv.context.world.WorldConstructions[id] })
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/artifacts", "artifacts.html", func(p Parms) any { return grouped(srv.context.world.Artifacts) })
2022-04-26 18:39:24 +03:00
srv.RegisterWorldResourcePage("/artifact/{id}", "artifact.html", func(id int) any { return srv.context.world.Artifacts[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/artifact/{id}", "popoverArtifact.html", func(id int) any { return srv.context.world.Artifacts[id] })
2022-04-26 18:39:24 +03:00
2022-04-28 22:24:55 +03:00
srv.RegisterWorldPage("/artforms", "artforms.html", func(p Parms) any {
2022-04-30 10:45:31 +03:00
return &struct {
2022-04-28 22:24:55 +03:00
DanceForms map[string][]*model.DanceForm
MusicalForms map[string][]*model.MusicalForm
PoeticForms map[string][]*model.PoeticForm
}{
DanceForms: grouped(srv.context.world.DanceForms),
MusicalForms: grouped(srv.context.world.MusicalForms),
PoeticForms: grouped(srv.context.world.PoeticForms),
}
})
srv.RegisterWorldPage("/writtencontents", "writtencontents.html", func(p Parms) any { return grouped(srv.context.world.WrittenContents) })
srv.RegisterWorldResourcePage("/writtencontent/{id}", "writtencontent.html", func(id int) any { return srv.context.world.WrittenContents[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/writtencontent/{id}", "popoverWrittencontent.html", func(id int) any { return srv.context.world.WrittenContents[id] })
2022-04-28 22:24:55 +03:00
srv.RegisterWorldResourcePage("/hf/{id}", "hf.html", func(id int) any { return srv.context.world.HistoricalFigures[id] })
2022-04-30 15:27:42 +03:00
srv.RegisterWorldResourcePage("/popover/hf/{id}", "popoverHf.html", func(id int) any { return srv.context.world.HistoricalFigures[id] })
2022-04-28 22:24:55 +03:00
2022-04-26 18:39:24 +03:00
srv.RegisterWorldPage("/", "eventTypes.html", func(p Parms) any { return srv.context.world.AllEventTypes() })
srv.RegisterWorldPage("/events", "eventTypes.html", func(p Parms) any { return srv.context.world.AllEventTypes() })
srv.RegisterWorldPage("/events/{type}", "eventType.html", func(p Parms) any { return srv.context.world.EventsOfType(p["type"]) })
2022-04-29 21:23:00 +03:00
srv.router.PathPrefix("/search").Handler(searchHandler{server: srv})
2022-04-26 22:00:09 +03:00
srv.router.PathPrefix("/load").Handler(srv.loader)
2022-04-26 10:24:16 +03:00
2022-04-29 12:39:39 +03:00
spa := spaHandler{server: srv, staticFS: static, staticPath: "static", indexPath: "index.html"}
2022-04-26 10:24:16 +03:00
srv.router.PathPrefix("/").Handler(spa)
OpenBrowser("http://localhost:8080")
fmt.Println("Serving at :8080")
http.ListenAndServe(":8080", srv.router)
}
2022-04-30 15:27:42 +03:00
func (srv *DfServer) findStructure(p Parms) any {
siteId, err := strconv.Atoi(p["siteId"])
if err != nil {
return nil
}
structureId, err := strconv.Atoi(p["id"])
if err != nil {
return nil
}
if site, ok := srv.context.world.Sites[siteId]; ok {
return site.Structures[structureId]
}
return nil
}
2022-04-26 10:24:16 +03:00
type spaHandler struct {
2022-04-29 12:39:39 +03:00
server *DfServer
2022-04-26 10:24:16 +03:00
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
_, 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 {
2022-04-30 10:45:31 +03:00
h.server.notFound(w)
2022-04-26 10:24:16 +03:00
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusAccepted)
w.Write(index)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// get the subdirectory of the static dir
statics, err := fs.Sub(h.staticFS, h.staticPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.FS(statics)).ServeHTTP(w, r)
}
2022-04-26 18:39:24 +03:00
2022-04-30 10:45:31 +03:00
func (srv *DfServer) notFound(w http.ResponseWriter) {
err := srv.templates.Render(w, "notFound.html", nil)
2022-04-27 22:44:39 +03:00
if err != nil {
httpError(w, err)
2022-04-26 22:00:09 +03:00
}
}
2022-04-27 22:44:39 +03:00
func httpError(w http.ResponseWriter, err error) {
fmt.Fprintln(w, err)
fmt.Println(err)
}
2022-04-28 21:14:33 +03:00
type namedTyped interface {
model.Named
model.Typed
}
2022-04-29 15:21:27 +03:00
func flatGrouped[K comparable, U any, V namedTyped](input map[K]U, mapper func(U) []V) map[string][]V {
output := make(map[string][]V)
for _, x := range input {
for _, v := range mapper(x) {
k := v.Type()
if v.Name() != "" {
output[k] = append(output[k], v)
}
}
}
for _, v := range output {
sort.Slice(v, func(i, j int) bool { return v[i].Name() < v[j].Name() })
}
return output
}
func grouped[K comparable, T namedTyped](input map[K]T) map[string][]T {
2022-04-27 22:44:39 +03:00
output := make(map[string][]T)
for _, v := range input {
k := v.Type()
2022-04-29 12:31:05 +03:00
if v.Name() != "" {
output[k] = append(output[k], v)
}
2022-04-27 22:44:39 +03:00
}
2022-04-28 21:14:33 +03:00
for _, v := range output {
sort.Slice(v, func(i, j int) bool { return v[i].Name() < v[j].Name() })
}
2022-04-27 22:44:39 +03:00
return output
}