2019-01-18 10:47:38 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-01-18 11:13:37 +03:00
|
|
|
"mapserver/colormapping"
|
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-02-05 15:25:01 +03:00
|
|
|
sqliteobjdb "mapserver/mapobjectdb/sqlite"
|
2019-01-21 18:27:31 +03:00
|
|
|
"mapserver/params"
|
2019-01-18 11:13:37 +03:00
|
|
|
"mapserver/tilerenderer"
|
|
|
|
"mapserver/worldconfig"
|
2019-02-07 11:11:11 +03:00
|
|
|
"mapserver/settings"
|
2019-01-18 11:13:37 +03:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-02-08 09:40:40 +03:00
|
|
|
"os"
|
|
|
|
"io/ioutil"
|
|
|
|
|
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("world.mt")
|
|
|
|
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:
|
2019-02-05 15:13:34 +03:00
|
|
|
a.Blockdb, err = sqlite.New("map.sqlite")
|
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
|
|
|
}
|
|
|
|
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-01-18 10:47:38 +03:00
|
|
|
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb)
|
|
|
|
|
2019-01-18 11:13:37 +03:00
|
|
|
//color mapping
|
2019-01-18 10:47:38 +03:00
|
|
|
a.Colormapping = colormapping.NewColorMapping()
|
2019-02-08 09:40:40 +03:00
|
|
|
|
|
|
|
//load default colors
|
|
|
|
count, err := a.Colormapping.LoadVFSColors(false, "/colors.txt")
|
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-08 09:40:40 +03:00
|
|
|
logrus.WithFields(logrus.Fields{"count": count}).Info("Loaded default colors")
|
|
|
|
|
|
|
|
|
|
|
|
//load provided colors, if available
|
|
|
|
info, err := os.Stat("colors.txt")
|
|
|
|
if info != nil && err == nil {
|
|
|
|
logrus.WithFields(logrus.Fields{"filename": "colors.txt"}).Info("Loading colors from filesystem")
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile("colors.txt")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err = a.Colormapping.LoadBytes(data)
|
|
|
|
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
|
|
|
|
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, 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-01-28 16:47:53 +03:00
|
|
|
//TODO: Psql connection
|
|
|
|
|
|
|
|
} else {
|
2019-02-05 15:25:01 +03:00
|
|
|
a.Objectdb, err = sqliteobjdb.New("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-01-18 11:13:37 +03:00
|
|
|
//migrate tile 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-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-01-21 17:13:33 +03:00
|
|
|
a.Objectdb,
|
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-02-01 13:26:57 +03:00
|
|
|
return &a
|
2019-01-18 10:47:38 +03:00
|
|
|
}
|