go fmt & public config

This commit is contained in:
NatureFreshMilk 2019-02-07 08:02:13 +01:00
parent 4919714e35
commit b049e33bb2
12 changed files with 42 additions and 54 deletions

View File

@ -7,9 +7,8 @@ import (
"mapserver/db" "mapserver/db"
) )
type PostgresAccessor struct { type PostgresAccessor struct {
db *sql.DB db *sql.DB
} }
func (db *PostgresAccessor) Migrate() error { 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} return &db.Block{Pos: c, Data: data, Mtime: mtime}
} }
func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) { func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0) blocks := make([]*db.Block, 0)
@ -76,7 +74,6 @@ func (this *PostgresAccessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoor
return blocks, nil return blocks, nil
} }
func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) { func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) {
rows, err := this.db.Query(countBlocksQuery, frommtime, tomtime) rows, err := this.db.Query(countBlocksQuery, frommtime, tomtime)
if err != nil { if err != nil {
@ -99,7 +96,6 @@ func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error)
return 0, nil return 0, nil
} }
func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) { func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) {
rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z) rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z)
if err != nil { if err != nil {

View File

@ -44,7 +44,6 @@ const countBlocksQuery = `
select count(*) from blocks b where b.mtime >= ? and b.mtime <= ? select count(*) from blocks b where b.mtime >= ? and b.mtime <= ?
` `
const getBlockQuery = ` const getBlockQuery = `
select pos,data,mtime from blocks b select pos,data,mtime from blocks b
where b.posx = ? where b.posx = ?

View File

@ -36,7 +36,6 @@ const countBlocksQuery = `
select count(*) from blocks b where b.mtime >= ? and b.mtime <= ? select count(*) from blocks b where b.mtime >= ? and b.mtime <= ?
` `
const getBlockQuery = ` const getBlockQuery = `
select pos,data,mtime from blocks b where b.pos = ? select pos,data,mtime from blocks b where b.pos = ?
` `

View File

@ -55,7 +55,6 @@ func convertRows(pos int64, data []byte, mtime int64) *db.Block {
return &db.Block{Pos: c, Data: data, Mtime: mtime} return &db.Block{Pos: c, Data: data, Mtime: mtime}
} }
func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) { func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0) blocks := make([]*db.Block, 0)
@ -83,7 +82,6 @@ func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.
return blocks, nil return blocks, nil
} }
func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoords, limit int) ([]*db.Block, error) { func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoords, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0) blocks := make([]*db.Block, 0)
pc := coords.CoordToPlain(lastpos) pc := coords.CoordToPlain(lastpos)
@ -112,8 +110,6 @@ func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoord
return blocks, nil return blocks, nil
} }
func (db *Sqlite3Accessor) CountBlocks(frommtime, tomtime int64) (int, error) { func (db *Sqlite3Accessor) CountBlocks(frommtime, tomtime int64) (int, error) {
rows, err := db.db.Query(countBlocksQuery, frommtime, tomtime) rows, err := db.db.Query(countBlocksQuery, frommtime, tomtime)
if err != nil { if err != nil {

View File

@ -6,13 +6,13 @@ import (
type MapBlock struct { type MapBlock struct {
Pos *coords.MapBlockCoords `json:"pos"` Pos *coords.MapBlockCoords `json:"pos"`
Size int `json:"size"` Size int `json:"size"`
Version byte `json:"version"` Version byte `json:"version"`
Underground bool `json:"underground"` Underground bool `json:"underground"`
Mapdata *MapData `json:"mapdata"` Mapdata *MapData `json:"mapdata"`
Metadata *Metadata `json:"metadata"` Metadata *Metadata `json:"metadata"`
BlockMapping map[int]string `json:"blockmapping"` BlockMapping map[int]string `json:"blockmapping"`
Mtime int64 `json:"mtime"` Mtime int64 `json:"mtime"`
} }
type MapData struct { type MapData struct {

View File

@ -1,33 +1,31 @@
package mapobjectdb package mapobjectdb
import ( import (
"fmt"
"mapserver/coords" "mapserver/coords"
"testing" "testing"
"fmt"
) )
func TestNewMapBlockCoords(t *testing.T) { func TestNewMapBlockCoords(t *testing.T) {
attrs := make(map[string]string) attrs := make(map[string]string)
attrs["X"] = "y" attrs["X"] = "y"
pos := coords.NewMapBlockCoords(1, 2, 3) pos := coords.NewMapBlockCoords(1, 2, 3)
fmt.Println(pos) fmt.Println(pos)
obj := NewMapObject(pos, 10, 12, 14, "xy") obj := NewMapObject(pos, 10, 12, 14, "xy")
fmt.Println(obj) fmt.Println(obj)
if obj.X != 26 { if obj.X != 26 {
t.Error("x coord off") t.Error("x coord off")
} }
if obj.Y != 44 { if obj.Y != 44 {
t.Error("Y coord off") t.Error("Y coord off")
} }
if obj.Z != 62 {
t.Error("Z coord off")
}
if obj.Z != 62 {
t.Error("Z coord off")
}
} }

View File

@ -3,8 +3,8 @@ package sqlite
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"mapserver/mapobjectdb"
"mapserver/coords" "mapserver/coords"
"mapserver/mapobjectdb"
"os" "os"
"testing" "testing"
) )

View File

@ -1,6 +1,5 @@
package sqlite package sqlite
const migrateScript = ` const migrateScript = `
PRAGMA foreign_keys = ON; PRAGMA foreign_keys = ON;
@ -40,7 +39,6 @@ create table if not exists tiles(
); );
` `
const getMapDataPosQuery = ` const getMapDataPosQuery = `
select o.id, o.type, o.mtime, select o.id, o.type, o.mtime,
o.x, o.y, o.z, o.x, o.y, o.z,
@ -54,13 +52,10 @@ and o.posx <= ? and o.posy <= ? and o.posz <= ?
order by o.id order by o.id
` `
const removeMapDataQuery = ` const removeMapDataQuery = `
delete from objects where posx = ? and posy = ? and posz = ? delete from objects where posx = ? and posy = ? and posz = ?
` `
const addMapDataQuery = ` const addMapDataQuery = `
insert into insert into
objects(x,y,z,posx,posy,posz,type,mtime) objects(x,y,z,posx,posy,posz,type,mtime)

View File

@ -5,7 +5,6 @@ import (
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
) )
func (db *Sqlite3Accessor) GetTile(pos *coords.TileCoords) (*mapobjectdb.Tile, error) { func (db *Sqlite3Accessor) GetTile(pos *coords.TileCoords) (*mapobjectdb.Tile, error) {
rows, err := db.db.Query(getTileQuery, pos.LayerId, pos.X, pos.Y, pos.Zoom) rows, err := db.db.Query(getTileQuery, pos.LayerId, pos.X, pos.Y, pos.Zoom)
if err != nil { if err != nil {
@ -39,14 +38,11 @@ func (db *Sqlite3Accessor) GetTile(pos *coords.TileCoords) (*mapobjectdb.Tile, e
return nil, nil return nil, nil
} }
func (db *Sqlite3Accessor) SetTile(tile *mapobjectdb.Tile) error { 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) _, err := db.db.Exec(setTileQuery, tile.Pos.X, tile.Pos.Y, tile.Pos.Zoom, tile.Pos.LayerId, tile.Data, tile.Mtime)
return err return err
} }
func (db *Sqlite3Accessor) RemoveTile(pos *coords.TileCoords) error { func (db *Sqlite3Accessor) RemoveTile(pos *coords.TileCoords) error {
_, err := db.db.Exec(removeTileQuery, pos.X, pos.Y, pos.Zoom, pos.LayerId) _, err := db.db.Exec(removeTileQuery, pos.X, pos.Y, pos.Zoom, pos.LayerId)
return err return err

View File

@ -5,9 +5,9 @@ import (
) )
type ParamsType struct { type ParamsType struct {
Help bool Help bool
Version bool Version bool
Debug bool Debug bool
} }
func Parse() ParamsType { func Parse() ParamsType {

View File

@ -3,14 +3,24 @@ package web
import ( import (
"encoding/json" "encoding/json"
"mapserver/app" "mapserver/app"
"mapserver/layer"
"net/http" "net/http"
) )
//Public facing config
type PublicConfig struct {
Layers []layer.Layer `json:"layers"`
}
type ConfigHandler struct { type ConfigHandler struct {
ctx *app.App ctx *app.App
} }
func (h *ConfigHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { func (h *ConfigHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("content-type", "application/json") 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)
} }

View File

@ -13,10 +13,10 @@ const (
) )
const ( const (
CONFIG_BACKEND string = "backend" CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend" CONFIG_PLAYER_BACKEND string = "player_backend"
CONFIG_PSQL_CONNECTION string = "pgsql_connection" CONFIG_PSQL_CONNECTION string = "pgsql_connection"
CONFIG_PSQL_MAPSERVER string = "pgsql_mapserver_connection" CONFIG_PSQL_MAPSERVER string = "pgsql_mapserver_connection"
) )
type PsqlConfig struct { type PsqlConfig struct {
@ -31,11 +31,10 @@ type WorldConfig struct {
Backend string Backend string
PlayerBackend string PlayerBackend string
PsqlConnection string PsqlConnection string
MapObjectConnection string MapObjectConnection string
} }
func Parse(filename string) WorldConfig { func Parse(filename string) WorldConfig {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {