2019-01-18 08:47:38 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-02-09 18:54:26 +01:00
|
|
|
"mapserver/db/postgres"
|
2019-02-05 13:13:34 +01:00
|
|
|
"mapserver/db/sqlite"
|
2019-01-28 14:33:32 +01:00
|
|
|
"mapserver/eventbus"
|
2019-01-18 09:13:37 +01:00
|
|
|
"mapserver/mapblockaccessor"
|
|
|
|
"mapserver/mapblockrenderer"
|
2019-03-21 15:07:09 +01:00
|
|
|
postgresobjdb "mapserver/mapobjectdb/postgres"
|
2019-02-05 13:25:01 +01:00
|
|
|
sqliteobjdb "mapserver/mapobjectdb/sqlite"
|
2019-06-14 11:21:07 +02:00
|
|
|
"mapserver/media"
|
2019-01-21 16:27:31 +01:00
|
|
|
"mapserver/params"
|
2019-02-08 16:02:24 +01:00
|
|
|
"mapserver/settings"
|
2019-02-09 18:05:40 +01:00
|
|
|
"mapserver/tiledb"
|
2019-01-18 09:13:37 +01:00
|
|
|
"mapserver/tilerenderer"
|
|
|
|
"mapserver/worldconfig"
|
2019-02-22 19:13:47 +01:00
|
|
|
"time"
|
2019-01-18 09:13:37 +01:00
|
|
|
|
2023-01-20 17:44:59 +01:00
|
|
|
"github.com/minetest-go/colormapping"
|
|
|
|
|
2019-02-08 16:02:24 +01:00
|
|
|
"os"
|
2023-09-18 06:47:29 +02:00
|
|
|
"path/filepath"
|
2019-02-08 07:40:40 +01:00
|
|
|
|
2019-02-22 19:13:47 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
"errors"
|
2019-01-18 08:47:38 +01:00
|
|
|
)
|
|
|
|
|
2019-02-01 11:26:57 +01:00
|
|
|
func Setup(p params.ParamsType, cfg *Config) *App {
|
2019-01-18 09:13:37 +01:00
|
|
|
a := App{}
|
|
|
|
a.Params = p
|
|
|
|
a.Config = cfg
|
2019-01-28 14:33:32 +01:00
|
|
|
a.WebEventbus = eventbus.New()
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//Parse world config
|
2023-09-18 06:47:29 +02:00
|
|
|
a.Worldconfig = worldconfig.Parse(filepath.Join(a.Config.WorldPath, "world.mt"))
|
2019-01-18 09:13:37 +01:00
|
|
|
logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
var err error
|
2019-01-25 13:14:49 +01:00
|
|
|
|
2019-02-08 14:57:36 +01:00
|
|
|
switch a.Worldconfig[worldconfig.CONFIG_BACKEND] {
|
2019-01-25 13:14:49 +01:00
|
|
|
case worldconfig.BACKEND_SQLITE3:
|
2023-09-18 06:47:29 +02:00
|
|
|
map_path := filepath.Join(a.Config.WorldPath, "map.sqlite")
|
2021-05-04 17:26:20 +02:00
|
|
|
|
|
|
|
// check if the database exists, otherwise abort (nothing to render/display)
|
|
|
|
_, err := os.Stat(map_path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
panic("world-map does not exist, aborting")
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a new sqlite-based blockdb instance
|
|
|
|
a.Blockdb, err = sqlite.New(map_path)
|
2019-01-25 13:14:49 +01:00
|
|
|
if err != nil {
|
2019-02-01 11:26:57 +01:00
|
|
|
panic(err)
|
2019-01-25 13:14:49 +01:00
|
|
|
}
|
2019-02-09 18:54:26 +01:00
|
|
|
|
|
|
|
case worldconfig.BACKEND_POSTGRES:
|
2021-05-04 17:26:20 +02:00
|
|
|
// create a new postgres based blockdb
|
2019-02-09 18:54:26 +01:00
|
|
|
a.Blockdb, err = postgres.New(a.Worldconfig[worldconfig.CONFIG_PSQL_CONNECTION])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-25 13:14:49 +01:00
|
|
|
default:
|
2019-02-08 14:57:36 +01:00
|
|
|
panic(errors.New("map-backend not supported: " + a.Worldconfig[worldconfig.CONFIG_BACKEND]))
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//migrate block db
|
2019-01-18 08:47:38 +01:00
|
|
|
err = a.Blockdb.Migrate()
|
|
|
|
if err != nil {
|
2019-02-01 11:26:57 +01:00
|
|
|
panic(err)
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//mapblock accessor
|
2019-02-22 19:13:47 +01:00
|
|
|
expireDuration, err := time.ParseDuration(cfg.MapBlockAccessorCfg.Expiretime)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
purgeDuration, err := time.ParseDuration(cfg.MapBlockAccessorCfg.Purgetime)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:43:57 +02:00
|
|
|
// mapblock accessor
|
2019-07-31 13:41:35 +02:00
|
|
|
a.MapBlockAccessor = mapblockaccessor.NewMapBlockAccessor(
|
2019-03-13 09:12:22 +01:00
|
|
|
a.Blockdb,
|
|
|
|
expireDuration, purgeDuration,
|
|
|
|
cfg.MapBlockAccessorCfg.MaxItems)
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//color mapping
|
2019-01-18 08:47:38 +01:00
|
|
|
a.Colormapping = colormapping.NewColorMapping()
|
2023-01-20 18:02:37 +01:00
|
|
|
err = a.Colormapping.LoadDefaults()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|
2019-02-08 07:40:40 +01:00
|
|
|
|
|
|
|
//load provided colors, if available
|
2023-09-28 07:00:04 +02:00
|
|
|
info, err := os.Stat(filepath.Join(a.Config.ColorsTxtPath, "colors.txt"))
|
2019-02-08 07:40:40 +01:00
|
|
|
if info != nil && err == nil {
|
|
|
|
logrus.WithFields(logrus.Fields{"filename": "colors.txt"}).Info("Loading colors from filesystem")
|
|
|
|
|
2023-09-28 07:00:04 +02:00
|
|
|
data, err := os.ReadFile(filepath.Join(a.Config.ColorsTxtPath, "colors.txt"))
|
2019-02-08 07:40:40 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:44:12 +02:00
|
|
|
count, err := a.Colormapping.LoadBytes(data)
|
2019-02-08 07:40:40 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{"count": count}).Info("Loaded custom colors")
|
|
|
|
|
|
|
|
}
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//mapblock renderer
|
2019-07-31 13:41:35 +02:00
|
|
|
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.MapBlockAccessor, a.Colormapping)
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-01-28 14:47:53 +01:00
|
|
|
//mapserver database
|
2019-02-08 14:57:36 +01:00
|
|
|
if a.Worldconfig[worldconfig.CONFIG_PSQL_MAPSERVER] != "" {
|
2019-03-21 15:07:09 +01:00
|
|
|
a.Objectdb, err = postgresobjdb.New(a.Worldconfig[worldconfig.CONFIG_PSQL_MAPSERVER])
|
2019-01-28 14:47:53 +01:00
|
|
|
} else {
|
2023-09-18 06:47:29 +02:00
|
|
|
a.Objectdb, err = sqliteobjdb.New(filepath.Join(a.Config.DataPath, "mapserver.sqlite"))
|
2019-01-28 14:47:53 +01:00
|
|
|
}
|
2019-01-18 08:47:38 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-01 11:26:57 +01:00
|
|
|
panic(err)
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:05:40 +01:00
|
|
|
//migrate object database
|
2019-01-21 15:13:33 +01:00
|
|
|
err = a.Objectdb.Migrate()
|
2019-01-18 08:47:38 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2019-02-01 11:26:57 +01:00
|
|
|
panic(err)
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-09 18:05:40 +01:00
|
|
|
//create tiledb
|
2023-09-18 06:47:29 +02:00
|
|
|
a.TileDB, err = tiledb.New(filepath.Join(a.Config.DataPath, "mapserver.tiles"))
|
2019-02-09 18:05:40 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-02-07 09:11:11 +01:00
|
|
|
//settings
|
|
|
|
a.Settings = settings.New(a.Objectdb)
|
|
|
|
|
2019-01-18 09:13:37 +01:00
|
|
|
//setup tile renderer
|
|
|
|
a.Tilerenderer = tilerenderer.NewTileRenderer(
|
|
|
|
a.Mapblockrenderer,
|
2019-02-09 18:05:40 +01:00
|
|
|
a.TileDB,
|
2019-01-18 09:13:37 +01:00
|
|
|
a.Blockdb,
|
2019-01-21 11:13:00 +01:00
|
|
|
a.Config.Layers,
|
2019-01-18 09:13:37 +01:00
|
|
|
)
|
2019-01-18 08:47:38 +01:00
|
|
|
|
2019-06-14 11:21:07 +02:00
|
|
|
//create media repo
|
|
|
|
repo := make(map[string][]byte)
|
2019-06-19 19:29:51 +02:00
|
|
|
|
|
|
|
if a.Config.EnableMediaRepository {
|
|
|
|
mediasize, _ := media.ScanDir(repo, ".", []string{"mapserver.tiles", ".git"})
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"count": len(repo),
|
|
|
|
"bytes": mediasize,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Info("Created media repository")
|
2019-06-14 13:13:21 +02:00
|
|
|
}
|
2019-06-14 11:21:07 +02:00
|
|
|
|
|
|
|
a.MediaRepo = repo
|
|
|
|
|
2019-02-01 11:26:57 +01:00
|
|
|
return &a
|
2019-01-18 08:47:38 +01:00
|
|
|
}
|