1
0
forked from MTSR/mapserver
mapserver/web/serve.go

88 lines
2.0 KiB
Go
Raw Normal View History

2019-01-18 11:30:51 +03:00
package web
import (
2021-04-12 14:03:10 +03:00
"embed"
2019-01-18 12:11:40 +03:00
"mapserver/app"
2021-04-12 14:03:10 +03:00
"mapserver/public"
2019-01-18 12:11:40 +03:00
"net/http"
2021-04-12 14:03:10 +03:00
"os"
2019-01-18 12:11:40 +03:00
"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
2021-04-12 14:55:00 +03:00
api := NewApi(ctx)
2019-01-18 12:11:40 +03:00
mux := http.NewServeMux()
2019-01-18 11:30:51 +03:00
2021-04-12 14:03:10 +03:00
// static files
if ctx.Config.Webdev {
logrus.Print("using live mode")
fs := http.FileServer(http.FS(os.DirFS("public")))
mux.HandleFunc("/", fs.ServeHTTP)
} else {
logrus.Print("using embed mode")
fs := http.FileServer(http.FS(public.Files))
mux.HandleFunc("/", CachedServeFunc(fs.ServeHTTP))
}
2019-02-07 21:48:05 +03:00
tiles := &Tiles{ctx: ctx}
tiles.Init()
mux.Handle("/api/tile/", tiles)
2021-04-12 14:55:00 +03:00
mux.HandleFunc("/api/config", api.GetConfig)
mux.HandleFunc("/api/stats", api.GetStats)
mux.HandleFunc("/api/media/", api.GetMedia)
mux.HandleFunc("/api/minetest", api.PostMinetestData)
mux.HandleFunc("/api/mapobjects/", api.QueryMapobjects)
mux.HandleFunc("/api/colormapping", api.GetColorMapping)
mux.HandleFunc("/api/viewblock/", api.GetBlockData)
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())
}
if ctx.Config.Skins.EnableSkinsDB && len(ctx.Config.Skins.SkinsPath) > 0 {
mux.HandleFunc("/api/skins/", api.GetSkin)
}
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
2021-04-12 14:55:00 +03:00
mux.HandleFunc("/api/mapblock/", api.GetMapBlockData)
2019-01-18 13:04:37 +03:00
}
2019-01-18 12:11:40 +03:00
2021-04-12 14:55:00 +03:00
// main entry point
2021-04-12 14:03:10 +03:00
http.HandleFunc("/", mux.ServeHTTP)
2019-06-17 08:05:21 +03:00
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
}
2021-04-12 14:03:10 +03:00
func getFileSystem(useLocalfs bool, content embed.FS) http.FileSystem {
if useLocalfs {
log.Print("using live mode")
return http.FS(os.DirFS("public"))
}
log.Print("using embed mode")
return http.FS(content)
}