1
0
forked from MTSR/mapserver
mapserver/app/setup.go

172 lines
3.8 KiB
Go
Raw Normal View History

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