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

88 lines
2.0 KiB
Go
Raw Normal View History

2019-01-18 09:30:51 +01:00
package web
import (
2021-04-12 13:03:10 +02:00
"embed"
2019-01-18 10:11:40 +01:00
"mapserver/app"
2021-04-12 13:03:10 +02:00
"mapserver/public"
2019-01-18 10:11:40 +01:00
"net/http"
2021-04-12 13:03:10 +02:00
"os"
2019-01-18 10:11:40 +01:00
"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
2021-04-12 13:55:00 +02:00
api := NewApi(ctx)
2019-01-18 10:11:40 +01:00
mux := http.NewServeMux()
2019-01-18 09:30:51 +01:00
2021-04-12 13:03:10 +02: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 19:48:05 +01:00
tiles := &Tiles{ctx: ctx}
tiles.Init()
mux.Handle("/api/tile/", tiles)
2021-04-12 13:55:00 +02: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 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())
}
if ctx.Config.Skins.EnableSkinsDB && len(ctx.Config.Skins.SkinsPath) > 0 {
mux.HandleFunc("/api/skins/", api.GetSkin)
}
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
2021-04-12 13:55:00 +02:00
mux.HandleFunc("/api/mapblock/", api.GetMapBlockData)
2019-01-18 11:04:37 +01:00
}
2019-01-18 10:11:40 +01:00
2021-04-12 13:55:00 +02:00
// main entry point
2021-04-12 13:03:10 +02:00
http.HandleFunc("/", mux.ServeHTTP)
2019-06-17 07:05:21 +02:00
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
}
2021-04-12 13:03:10 +02: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)
}