mapserver/web/serve.go

74 lines
1.6 KiB
Go
Raw Normal View History

2019-01-18 11:30:51 +03:00
package web
import (
2019-01-18 12:11:40 +03:00
"mapserver/app"
"mapserver/vfs"
"net/http"
"strconv"
2019-02-07 21:48:05 +03:00
2019-02-08 16:01:56 +03:00
"github.com/prometheus/client_golang/prometheus/promhttp"
2019-02-08 18:02:24 +03:00
"github.com/sirupsen/logrus"
2019-01-18 11:30:51 +03:00
)
2019-01-18 12:11:40 +03: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 11:30:51 +03:00
2019-01-18 12:11:40 +03:00
mux := http.NewServeMux()
2019-01-18 11:30:51 +03:00
2019-01-18 12:11:40 +03:00
mux.Handle("/", http.FileServer(vfs.FS(ctx.Config.Webdev)))
2019-02-07 21:48:05 +03:00
tiles := &Tiles{ctx: ctx}
tiles.Init()
mux.Handle("/api/tile/", tiles)
2019-01-18 13:09:16 +03:00
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
2019-06-14 12:21:07 +03:00
mux.Handle("/api/media/", &MediaHandler{ctx: ctx})
2019-01-29 09:53:59 +03:00
mux.Handle("/api/minetest", &Minetest{ctx: ctx})
2019-01-28 20:30:59 +03:00
mux.Handle("/api/mapobjects/", &MapObjects{ctx: ctx})
2019-01-22 18:36:50 +03:00
2019-03-13 10:55:59 +03:00
if ctx.Config.MapObjects.Areas {
mux.Handle("/api/areas", &AreasHandler{ctx: ctx})
}
2019-02-08 16:01:56 +03:00
if ctx.Config.EnablePrometheus {
mux.Handle("/metrics", promhttp.Handler())
}
2019-01-22 18:36:50 +03:00
ws := NewWS(ctx)
mux.Handle("/api/ws", ws)
2019-01-28 15:31:48 +03:00
ctx.Tilerenderer.Eventbus.AddListener(ws)
2019-01-28 16:33:32 +03:00
ctx.WebEventbus.AddListener(ws)
2019-01-18 13:04:37 +03:00
if ctx.Config.WebApi.EnableMapblock {
2019-01-23 15:22:47 +03:00
//mapblock endpoint
2019-01-18 13:04:37 +03:00
mux.Handle("/api/mapblock/", &MapblockHandler{ctx: ctx})
}
2019-01-18 12:11:40 +03:00
2019-06-17 08:05:21 +03: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 12:11:40 +03:00
if err != nil {
panic(err)
}
2019-01-18 11:30:51 +03:00
}