diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..0f14230 --- /dev/null +++ b/app/app.go @@ -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 +} diff --git a/app/setup.go b/app/setup.go new file mode 100644 index 0000000..d7f5ba9 --- /dev/null +++ b/app/setup.go @@ -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 +} diff --git a/initialrenderer/renderer.go b/initialrenderer/renderer.go index f7d25b4..2c412db 100644 --- a/initialrenderer/renderer.go +++ b/initialrenderer/renderer.go @@ -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, } diff --git a/main.go b/main.go index 55fd42b..0c4af29 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/params/params.go b/params/params.go index d01d2ab..42d52da 100644 --- a/params/params.go +++ b/params/params.go @@ -22,3 +22,7 @@ func Parse() ParamsType { return params } + +func PrintHelp(){ + flag.PrintDefaults() +}