func NewDbMap() *gorp.DbMap { dsn, _ := globalCfg.ReadString("dsn", "root:root@/tyrant") dbType, _ := globalCfg.ReadString("db", "mysql") if dbType != "mysql" && dbType != "sqlite3" { log.Fatal("db must be mysql or sqlite3") } db, err := sql.Open(dbType, dsn) if err != nil { log.Fatal(err) } var dbmap *gorp.DbMap if dbType == "mysql" { dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{}} } else { dbmap = &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}} } tbl := dbmap.AddTableWithName(Job{}, "jobs").SetKeys(true, "Id") tbl.ColMap("name").SetMaxSize(512).SetUnique(true) tbl.ColMap("executor").SetMaxSize(4096) tbl.ColMap("executor_flags").SetMaxSize(4096) tbl.ColMap("uris").SetMaxSize(2048) tbl = dbmap.AddTableWithName(Task{}, "tasks").SetKeys(true, "Id") tbl.ColMap("id").SetMaxSize(128).SetUnique(true) err = dbmap.CreateTablesIfNotExists() if err != nil { log.Fatal(err) } return dbmap }
// Serve starts the RPC server on listener. Serve blocks. func Serve(listener net.Listener, dbmap *gorp.DbMap) error { dbmap.AddTable(UniVar{}).SetKeys(false, "Name") err := dbmap.CreateTablesIfNotExists() if err != nil { return err } server := &Elvishd{dbmap} rpc.Register(server) rpc.Accept(listener) return nil }
// Called first when a host connects. // NOTE that this may be called multiple times as a host may drop and reconnect. func HostInit(playerId int, gameId string, gs GameService, ws *websocket.Conn, wsReadChan chan Message, db *gorp.DbMap) error { log.Printf("Host initing") // since host is always the first to connect, setup tables if they don't already exist db.AddTableWithName(TicTacToe_Board{}, "tictactoe_board").SetKeys(true, "Id") db.AddTableWithName(TicTacToe_Turn{}, "tictactoe_turn").SetKeys(true, "Id") err := db.CreateTablesIfNotExists() if err != nil { log.Printf("Unable to create TTT tables: %#v", err) return err } log.Printf("Tables created") // get the game so we know what state we should be in game, _, err := gs.GetGame(db, gameId, playerId) if err != nil { log.Printf("Host failed to get game: %#v", err) return err } log.Printf("got game") if game.State == "lobby" { log.Printf("Game is still in lobby") // update the lobby based on players that are currently connected pids := gs.GetConnectedPlayers(gameId) // angular wants an array of objects, it can't handle an array of ints players := []Message{} for _, pid := range pids { players = append(players, Message{"id": pid}) } ws.WriteJSON(Message{ "type": "players", "players": players, }) ws.WriteJSON(Message{ "type": "state", "state": game.State, }) } else { log.Printf("Host rejoining game in progress") // get the game board so we can send an update board, err := getBoard(gameId, db) if err != nil { log.Printf("Could not get board, this might not be an error: %#v", err) return err } log.Printf("Getting nicer board: %#v", board) niceBoard, err := board.getBoard() if err != nil { log.Printf("Can't init with board: %#v", err) return err } ws.WriteJSON(Message{ "type": "update", "board": niceBoard, "state": "start", }) } return nil }
) var InitDB func() = func() { dbConfig := new(DbConfig) config := dbConfig.getConnectionString() if db, err := sql.Open(config.Driver, config.ConnectionString); err != nil { revel.ERROR.Fatal(err) } else { Dbm = &gorp.DbMap{ Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}} } // Defines the table for use by GORP // This is a function we will create soon. // defineObjectTable(Dbm) if err := Dbm.CreateTablesIfNotExists(); err != nil { revel.ERROR.Fatal(err) } } // Set up tables here - a bit janky but it looks like you need to // define tables manually //func defineObjectTable(dbm *gorp.DbMap){ // // set "id" as primary key and autoincrement // t := dbm.AddTable(models.Object{}).SetKeys(true, "id") // // e.g. VARCHAR(25) // t.ColMap("oid").SetMaxSize(65) //} func getParamString(param string, defaultValue string) string { p, found := revel.Config.String(param)
func InitTables(dbmap *gorp.DbMap) error { dbmap.AddTableWithName(LogLine{}, "log_lines").SetKeys(true, "Id") return dbmap.CreateTablesIfNotExists() }