From b049e33bb2c24b8e1d5fa40c2ff730174253ef7d Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 7 Feb 2019 08:02:13 +0100 Subject: [PATCH] go fmt & public config --- server/db/postgres/postgres.go | 6 +---- server/db/postgres/sql.go | 1 - server/db/sqlite/sql.go | 1 - server/db/sqlite/sqlite.go | 4 --- server/mapblockparser/mapblock.go | 14 +++++----- server/mapobjectdb/accessor_test.go | 28 +++++++++----------- server/mapobjectdb/sqlite/mapobjects_test.go | 2 +- server/mapobjectdb/sqlite/sql.go | 5 ---- server/mapobjectdb/sqlite/tiles.go | 4 --- server/params/params.go | 6 ++--- server/web/config.go | 12 ++++++++- server/worldconfig/parse.go | 13 +++++---- 12 files changed, 42 insertions(+), 54 deletions(-) diff --git a/server/db/postgres/postgres.go b/server/db/postgres/postgres.go index cdb2b6a..91e6fca 100644 --- a/server/db/postgres/postgres.go +++ b/server/db/postgres/postgres.go @@ -7,9 +7,8 @@ import ( "mapserver/db" ) - type PostgresAccessor struct { - db *sql.DB + db *sql.DB } func (db *PostgresAccessor) Migrate() error { @@ -21,7 +20,6 @@ func convertRows(pos int64, data []byte, mtime int64) *db.Block { return &db.Block{Pos: c, Data: data, Mtime: mtime} } - func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) { blocks := make([]*db.Block, 0) @@ -76,7 +74,6 @@ func (this *PostgresAccessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoor return blocks, nil } - func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) { rows, err := this.db.Query(countBlocksQuery, frommtime, tomtime) if err != nil { @@ -99,7 +96,6 @@ func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) return 0, nil } - func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) { rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z) if err != nil { diff --git a/server/db/postgres/sql.go b/server/db/postgres/sql.go index 2bead65..44bbd99 100644 --- a/server/db/postgres/sql.go +++ b/server/db/postgres/sql.go @@ -44,7 +44,6 @@ const countBlocksQuery = ` select count(*) from blocks b where b.mtime >= ? and b.mtime <= ? ` - const getBlockQuery = ` select pos,data,mtime from blocks b where b.posx = ? diff --git a/server/db/sqlite/sql.go b/server/db/sqlite/sql.go index 2e12ef0..36e8f16 100644 --- a/server/db/sqlite/sql.go +++ b/server/db/sqlite/sql.go @@ -36,7 +36,6 @@ const countBlocksQuery = ` select count(*) from blocks b where b.mtime >= ? and b.mtime <= ? ` - const getBlockQuery = ` select pos,data,mtime from blocks b where b.pos = ? ` diff --git a/server/db/sqlite/sqlite.go b/server/db/sqlite/sqlite.go index dcfca81..d78808b 100644 --- a/server/db/sqlite/sqlite.go +++ b/server/db/sqlite/sqlite.go @@ -55,7 +55,6 @@ func convertRows(pos int64, data []byte, mtime int64) *db.Block { return &db.Block{Pos: c, Data: data, Mtime: mtime} } - func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) { blocks := make([]*db.Block, 0) @@ -83,7 +82,6 @@ func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db. return blocks, nil } - func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoords, limit int) ([]*db.Block, error) { blocks := make([]*db.Block, 0) pc := coords.CoordToPlain(lastpos) @@ -112,8 +110,6 @@ func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoord return blocks, nil } - - func (db *Sqlite3Accessor) CountBlocks(frommtime, tomtime int64) (int, error) { rows, err := db.db.Query(countBlocksQuery, frommtime, tomtime) if err != nil { diff --git a/server/mapblockparser/mapblock.go b/server/mapblockparser/mapblock.go index 427339e..9e519fe 100644 --- a/server/mapblockparser/mapblock.go +++ b/server/mapblockparser/mapblock.go @@ -6,13 +6,13 @@ import ( type MapBlock struct { Pos *coords.MapBlockCoords `json:"pos"` - Size int `json:"size"` - Version byte `json:"version"` - Underground bool `json:"underground"` - Mapdata *MapData `json:"mapdata"` - Metadata *Metadata `json:"metadata"` - BlockMapping map[int]string `json:"blockmapping"` - Mtime int64 `json:"mtime"` + Size int `json:"size"` + Version byte `json:"version"` + Underground bool `json:"underground"` + Mapdata *MapData `json:"mapdata"` + Metadata *Metadata `json:"metadata"` + BlockMapping map[int]string `json:"blockmapping"` + Mtime int64 `json:"mtime"` } type MapData struct { diff --git a/server/mapobjectdb/accessor_test.go b/server/mapobjectdb/accessor_test.go index 3854bf0..0ce7da0 100644 --- a/server/mapobjectdb/accessor_test.go +++ b/server/mapobjectdb/accessor_test.go @@ -1,33 +1,31 @@ package mapobjectdb import ( + "fmt" "mapserver/coords" "testing" - "fmt" ) - func TestNewMapBlockCoords(t *testing.T) { attrs := make(map[string]string) attrs["X"] = "y" pos := coords.NewMapBlockCoords(1, 2, 3) - fmt.Println(pos) + fmt.Println(pos) - obj := NewMapObject(pos, 10, 12, 14, "xy") - fmt.Println(obj) + obj := NewMapObject(pos, 10, 12, 14, "xy") + fmt.Println(obj) - if obj.X != 26 { - t.Error("x coord off") - } + if obj.X != 26 { + t.Error("x coord off") + } - if obj.Y != 44 { - t.Error("Y coord off") - } - - if obj.Z != 62 { - t.Error("Z coord off") - } + if obj.Y != 44 { + t.Error("Y coord off") + } + if obj.Z != 62 { + t.Error("Z coord off") + } } diff --git a/server/mapobjectdb/sqlite/mapobjects_test.go b/server/mapobjectdb/sqlite/mapobjects_test.go index 359b51c..b0bd3e9 100644 --- a/server/mapobjectdb/sqlite/mapobjects_test.go +++ b/server/mapobjectdb/sqlite/mapobjects_test.go @@ -3,8 +3,8 @@ package sqlite import ( "fmt" "io/ioutil" - "mapserver/mapobjectdb" "mapserver/coords" + "mapserver/mapobjectdb" "os" "testing" ) diff --git a/server/mapobjectdb/sqlite/sql.go b/server/mapobjectdb/sqlite/sql.go index a465d2c..ee4c29e 100644 --- a/server/mapobjectdb/sqlite/sql.go +++ b/server/mapobjectdb/sqlite/sql.go @@ -1,6 +1,5 @@ package sqlite - const migrateScript = ` PRAGMA foreign_keys = ON; @@ -40,7 +39,6 @@ create table if not exists tiles( ); ` - const getMapDataPosQuery = ` select o.id, o.type, o.mtime, o.x, o.y, o.z, @@ -54,13 +52,10 @@ and o.posx <= ? and o.posy <= ? and o.posz <= ? order by o.id ` - const removeMapDataQuery = ` delete from objects where posx = ? and posy = ? and posz = ? ` - - const addMapDataQuery = ` insert into objects(x,y,z,posx,posy,posz,type,mtime) diff --git a/server/mapobjectdb/sqlite/tiles.go b/server/mapobjectdb/sqlite/tiles.go index ae502f6..5bde834 100644 --- a/server/mapobjectdb/sqlite/tiles.go +++ b/server/mapobjectdb/sqlite/tiles.go @@ -5,7 +5,6 @@ import ( "mapserver/mapobjectdb" ) - func (db *Sqlite3Accessor) GetTile(pos *coords.TileCoords) (*mapobjectdb.Tile, error) { rows, err := db.db.Query(getTileQuery, pos.LayerId, pos.X, pos.Y, pos.Zoom) if err != nil { @@ -39,14 +38,11 @@ func (db *Sqlite3Accessor) GetTile(pos *coords.TileCoords) (*mapobjectdb.Tile, e return nil, nil } - func (db *Sqlite3Accessor) SetTile(tile *mapobjectdb.Tile) error { _, err := db.db.Exec(setTileQuery, tile.Pos.X, tile.Pos.Y, tile.Pos.Zoom, tile.Pos.LayerId, tile.Data, tile.Mtime) return err } - - func (db *Sqlite3Accessor) RemoveTile(pos *coords.TileCoords) error { _, err := db.db.Exec(removeTileQuery, pos.X, pos.Y, pos.Zoom, pos.LayerId) return err diff --git a/server/params/params.go b/server/params/params.go index 336743a..96b2a29 100644 --- a/server/params/params.go +++ b/server/params/params.go @@ -5,9 +5,9 @@ import ( ) type ParamsType struct { - Help bool - Version bool - Debug bool + Help bool + Version bool + Debug bool } func Parse() ParamsType { diff --git a/server/web/config.go b/server/web/config.go index 7b526c8..7d1f895 100644 --- a/server/web/config.go +++ b/server/web/config.go @@ -3,14 +3,24 @@ package web import ( "encoding/json" "mapserver/app" + "mapserver/layer" "net/http" ) +//Public facing config +type PublicConfig struct { + Layers []layer.Layer `json:"layers"` +} + type ConfigHandler struct { ctx *app.App } func (h *ConfigHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { resp.Header().Add("content-type", "application/json") - json.NewEncoder(resp).Encode(h.ctx.Config) + + webcfg := PublicConfig{} + webcfg.Layers = h.ctx.Config.Layers + + json.NewEncoder(resp).Encode(webcfg) } diff --git a/server/worldconfig/parse.go b/server/worldconfig/parse.go index c4d57b1..4b36cad 100644 --- a/server/worldconfig/parse.go +++ b/server/worldconfig/parse.go @@ -13,10 +13,10 @@ const ( ) const ( - CONFIG_BACKEND string = "backend" - CONFIG_PLAYER_BACKEND string = "player_backend" - CONFIG_PSQL_CONNECTION string = "pgsql_connection" - CONFIG_PSQL_MAPSERVER string = "pgsql_mapserver_connection" + CONFIG_BACKEND string = "backend" + CONFIG_PLAYER_BACKEND string = "player_backend" + CONFIG_PSQL_CONNECTION string = "pgsql_connection" + CONFIG_PSQL_MAPSERVER string = "pgsql_mapserver_connection" ) type PsqlConfig struct { @@ -31,11 +31,10 @@ type WorldConfig struct { Backend string PlayerBackend string - PsqlConnection string - MapObjectConnection string + PsqlConnection string + MapObjectConnection string } - func Parse(filename string) WorldConfig { file, err := os.Open(filename) if err != nil {