2019-01-18 09:30:51 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2019-01-18 10:11:40 +01:00
|
|
|
"mapserver/app"
|
|
|
|
"mapserver/vfs"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-02-07 19:48:05 +01:00
|
|
|
|
2019-02-08 14:01:56 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2019-02-08 16:02:24 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-01-18 09:30:51 +01:00
|
|
|
)
|
|
|
|
|
2019-01-18 10:11:40 +01:00
|
|
|
func Serve(ctx *app.App) {
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"port": ctx.Config.Port,
|
|
|
|
"webdev": ctx.Config.Webdev,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Info("Starting http server")
|
2019-01-18 09:30:51 +01:00
|
|
|
|
2019-01-18 10:11:40 +01:00
|
|
|
mux := http.NewServeMux()
|
2019-01-18 09:30:51 +01:00
|
|
|
|
2019-01-18 10:11:40 +01:00
|
|
|
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
|
2019-02-07 19:48:05 +01:00
|
|
|
|
|
|
|
tiles := &Tiles{ctx: ctx}
|
|
|
|
tiles.Init()
|
|
|
|
mux.Handle("/api/tile/", tiles)
|
2019-01-18 11:09:16 +01:00
|
|
|
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
|
2019-06-14 11:21:07 +02:00
|
|
|
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
|
2019-01-29 07:53:59 +01:00
|
|
|
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
|
2019-01-28 18:30:59 +01:00
|
|
|
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
|
2019-01-22 16:36:50 +01:00
|
|
|
|
2019-03-13 08:55:59 +01:00
|
|
|
if ctx.Config.MapObjects.Areas {
|
|
|
|
mux.Handle("/api/areas", &AreasHandler{ctx: ctx})
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:01:56 +01:00
|
|
|
if ctx.Config.EnablePrometheus {
|
|
|
|
mux.Handle("/metrics", promhttp.Handler())
|
|
|
|
}
|
|
|
|
|
2019-01-22 16:36:50 +01:00
|
|
|
ws := NewWS(ctx)
|
|
|
|
mux.Handle("/api/ws", ws)
|
|
|
|
|
2019-01-28 13:31:48 +01:00
|
|
|
ctx.Tilerenderer.Eventbus.AddListener(ws)
|
2019-01-28 14:33:32 +01:00
|
|
|
ctx.WebEventbus.AddListener(ws)
|
2019-01-18 11:04:37 +01:00
|
|
|
|
|
|
|
if ctx.Config.WebApi.EnableMapblock {
|
2019-01-23 13:22:47 +01:00
|
|
|
//mapblock endpoint
|
2019-01-18 11:04:37 +01:00
|
|
|
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
|
|
|
|
}
|
2019-01-18 10:11:40 +01:00
|
|
|
|
2019-06-17 07:05:21 +02:00
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
uri := r.RequestURI
|
|
|
|
|
|
|
|
if len(uri) >= 3 {
|
|
|
|
suffix := uri[len(uri)-3:]
|
|
|
|
|
|
|
|
switch suffix {
|
|
|
|
case "css":
|
|
|
|
w.Header().Set("Content-Type", "text/css")
|
|
|
|
case ".js":
|
|
|
|
w.Header().Set("Content-Type", "application/javascript")
|
|
|
|
case "png":
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mux.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
err := http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), nil)
|
2019-01-18 10:11:40 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-18 09:30:51 +01:00
|
|
|
}
|