app bootstrap

This commit is contained in:
NatureFreshMilk 2019-01-18 08:47:38 +01:00
parent 7cff189b0b
commit 6943f10f59
5 changed files with 140 additions and 59 deletions

29
app/app.go Normal file
View File

@ -0,0 +1,29 @@
package app
import (
"mapserver/params"
"mapserver/worldconfig"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/colormapping"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"mapserver/tilerenderer"
)
const (
Version = "2.0-DEV"
)
type App struct {
Params params.ParamsType
Worldconfig worldconfig.WorldConfig
Blockdb db.DBAccessor
Tiledb tiledb.DBAccessor
BlockAccessor *mapblockaccessor.MapBlockAccessor
Colormapping *colormapping.ColorMapping
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
Tilerenderer *tilerenderer.TileRenderer
}

85
app/setup.go Normal file
View File

@ -0,0 +1,85 @@
package app
import (
"mapserver/params"
"mapserver/worldconfig"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/colormapping"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"mapserver/tilerenderer"
"mapserver/layerconfig"
"github.com/sirupsen/logrus"
"errors"
)
func Setup(p params.ParamsType) (*App, error) {
a := App{}
a.Params = p
//Parse world config
a.Worldconfig = worldconfig.Parse(a.Params.Worlddir + "world.mt")
logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 {
return nil, errors.New("no supported backend found!")
}
//create db accessor
var err error
a.Blockdb, err = db.NewSqliteAccessor("map.sqlite")
if err != nil {
return nil, err
}
//migrate block db
err = a.Blockdb.Migrate()
if err != nil {
return nil, err
}
//mapblock accessor
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb)
//color mapping
a.Colormapping = colormapping.NewColorMapping()
err = a.Colormapping.LoadVFSColors(false, "/colors.txt")
if err != nil {
return nil, err
}
//mapblock renderer
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping)
//tile database
a.Tiledb, err = tiledb.NewSqliteAccessor("tiles.sqlite")
if err != nil {
return nil, err
}
//migrate tile database
err = a.Tiledb.Migrate()
if err != nil {
return nil, err
}
//setup tile renderer
a.Tilerenderer = tilerenderer.NewTileRenderer(
a.Mapblockrenderer,
a.Tiledb,
a.Blockdb,
layerconfig.DefaultLayers,
)
return &a, nil
}

View File

@ -47,7 +47,7 @@ func Render(tr *tilerenderer.TileRenderer,
fields := logrus.Fields{
"x": x,
"y": y,
"progress%": progress,
"progress(%)": progress,
"layer": layer.Name,
"perf": perf_count,
}

79
main.go
View File

@ -1,82 +1,45 @@
package main
import (
"flag"
"fmt"
"mapserver/initialrenderer"
"mapserver/colormapping"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/params"
"mapserver/worldconfig"
"github.com/sirupsen/logrus"
"mapserver/tilerenderer"
"mapserver/tiledb"
"mapserver/layerconfig"
"mapserver/app"
"mapserver/params"
"fmt"
)
const (
Version = "2.0-DEV"
)
func main() {
logrus.SetLevel(logrus.InfoLevel)
p := params.Parse()
//Parse command line
if p.Help {
flag.PrintDefaults()
return
}
p := params.Parse()
if p.Version {
fmt.Print("Mapserver version: ")
fmt.Println(Version)
return
}
if p.Help {
params.PrintHelp()
return
}
worldcfg := worldconfig.Parse(p.Worlddir + "world.mt")
logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
if p.Version {
fmt.Print("Mapserver version: ")
fmt.Println(app.Version)
return
}
if worldcfg.Backend != worldconfig.BACKEND_SQLITE3 {
panic("no sqlite3 backend found!")
}
//setup app context
ctx, err := app.Setup(p)
a, err := db.NewSqliteAccessor("map.sqlite")
if err != nil {
//error case
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
//run initial rendering
cache := mapblockaccessor.NewMapBlockAccessor(a)
c := colormapping.NewColorMapping()
err = c.LoadVFSColors(false, "/colors.txt")
if err != nil {
panic(err)
}
r := mapblockrenderer.NewMapBlockRenderer(cache, c)
tdb, err := tiledb.NewSqliteAccessor("tiles.sqlite")
if err != nil {
panic(err)
}
err = tdb.Migrate()
if err != nil {
panic(err)
}
tr := tilerenderer.NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
initialrenderer.Render(tr, layerconfig.DefaultLayers)
initialrenderer.Render(ctx.Tilerenderer, layerconfig.DefaultLayers)
}

View File

@ -22,3 +22,7 @@ func Parse() ParamsType {
return params
}
func PrintHelp(){
flag.PrintDefaults()
}