mapserver/server/web/serve.go

44 lines
911 B
Go
Raw Normal View History

2019-01-18 11:30:51 +03:00
package web
import (
2019-01-18 12:11:40 +03:00
"github.com/sirupsen/logrus"
"mapserver/app"
"mapserver/vfs"
"net/http"
"strconv"
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-01-18 13:04:37 +03:00
mux.Handle("/api/tile/", &Tiles{ctx: ctx})
2019-01-18 13:09:16 +03:00
mux.Handle("/api/config", &ConfigHandler{ctx: ctx})
2019-01-28 12:42:07 +03:00
mux.Handle("/api/mapobjects", &MapObjects{ctx: ctx})
2019-01-22 18:36:50 +03:00
ws := NewWS(ctx)
mux.Handle("/api/ws", ws)
2019-01-23 15:13:32 +03:00
ctx.Events = ws
2019-01-22 18:36:50 +03:00
ctx.Tilerenderer.AddListener(ws)
2019-01-18 13:04:37 +03:00
if ctx.Config.WebApi.EnableMapblock {
2019-01-22 18:36:50 +03:00
//websocket listener
ctx.BlockAccessor.AddListener(ws)
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
err := http.ListenAndServe(":"+strconv.Itoa(ctx.Config.Port), mux)
if err != nil {
panic(err)
}
2019-01-18 11:30:51 +03:00
}