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{ fields := logrus.Fields{
"x": x, "x": x,
"y": y, "y": y,
"progress%": progress, "progress(%)": progress,
"layer": layer.Name, "layer": layer.Name,
"perf": perf_count, "perf": perf_count,
} }

79
main.go
View File

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

View File

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