forked from MTSR/mapserver
api/areas endpoint and config
This commit is contained in:
parent
1fde419e6b
commit
0440dd11e1
@ -30,6 +30,7 @@ type MapBlockAccessorConfig struct {
|
||||
}
|
||||
|
||||
type MapObjectConfig struct {
|
||||
Areas bool `json:"areas"`
|
||||
Bones bool `json:"bones"`
|
||||
Protector bool `json:"protector"`
|
||||
XPProtector bool `json:"xpprotector"`
|
||||
@ -107,6 +108,7 @@ func ParseConfig(filename string) (*Config, error) {
|
||||
}
|
||||
|
||||
mapobjs := MapObjectConfig{
|
||||
Areas: true,
|
||||
Bones: true,
|
||||
Protector: true,
|
||||
XPProtector: true,
|
||||
|
56
server/web/areas.go
Normal file
56
server/web/areas.go
Normal file
@ -0,0 +1,56 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"mapserver/app"
|
||||
"mapserver/areasparser"
|
||||
"net/http"
|
||||
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AreasHandler struct {
|
||||
ctx *app.App
|
||||
cache []*areasparser.Area
|
||||
lasttime int64
|
||||
}
|
||||
|
||||
var mutex = &sync.Mutex{}
|
||||
|
||||
const AREAS_FILENAME = "areas.dat"
|
||||
|
||||
func (h *AreasHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
|
||||
info, err := os.Stat(AREAS_FILENAME)
|
||||
if info == nil || err != nil {
|
||||
// no areas file
|
||||
resp.Header().Add("content-type", "application/json")
|
||||
resp.Write([]byte("[]"))
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
diff := now - h.lasttime
|
||||
|
||||
if diff > 5 {
|
||||
mutex.Lock()
|
||||
|
||||
h.lasttime = now
|
||||
areas, err := areasparser.ParseFile(AREAS_FILENAME)
|
||||
|
||||
if err != nil {
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
h.cache = areas
|
||||
|
||||
mutex.Unlock()
|
||||
}
|
||||
|
||||
resp.Header().Add("content-type", "application/json")
|
||||
json.NewEncoder(resp).Encode(h.cache)
|
||||
|
||||
}
|
@ -28,6 +28,10 @@ func Serve(ctx *app.App) {
|
||||
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
|
||||
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
|
||||
|
||||
if ctx.Config.MapObjects.Areas {
|
||||
mux.Handle("/api/areas", &AreasHandler{ctx: ctx})
|
||||
}
|
||||
|
||||
if ctx.Config.EnablePrometheus {
|
||||
mux.Handle("/metrics", promhttp.Handler())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user