dorfylegends/backend/server/resource.go

43 lines
859 B
Go
Raw Normal View History

2022-04-09 19:37:44 +03:00
package server
import (
"fmt"
"net/http"
2022-04-30 10:45:31 +03:00
"reflect"
2022-04-09 19:37:44 +03:00
"strconv"
"github.com/gorilla/mux"
)
2022-04-26 10:24:16 +03:00
type Parms map[string]string
2022-04-09 19:37:44 +03:00
2022-04-26 18:39:24 +03:00
func (srv *DfServer) RegisterWorldPage(path string, template string, accessor func(Parms) any) {
2022-04-09 19:37:44 +03:00
get := func(w http.ResponseWriter, r *http.Request) {
2022-04-26 18:39:24 +03:00
if srv.context.world == nil {
srv.renderLoading(w, r)
return
}
2022-04-30 10:45:31 +03:00
data := accessor(mux.Vars(r))
if reflect.ValueOf(data).IsNil() {
srv.notFound(w)
return
}
err := srv.templates.Render(w, template, data)
2022-04-09 19:37:44 +03:00
if err != nil {
2022-04-26 10:24:16 +03:00
fmt.Fprintln(w, err)
2022-04-09 19:37:44 +03:00
fmt.Println(err)
}
}
2022-04-26 10:24:16 +03:00
srv.router.HandleFunc(path, get).Methods("GET")
}
2022-04-26 18:39:24 +03:00
func (srv *DfServer) RegisterWorldResourcePage(path string, template string, accessor func(int) any) {
srv.RegisterWorldPage(path, template, func(params Parms) any {
2022-04-26 10:24:16 +03:00
id, _ := strconv.Atoi(params["id"])
return accessor(id)
})
2022-04-09 19:37:44 +03:00
}