mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-12 19:20:06 +00:00
unknown
d7445e0f0f
- loginMetadata is passed to shards through redis now - shards announce they're alive via redis.AnnounceShard() which just populates a hashset keyed 'shards' - login servers grab the 'shards' hashset and randomly picks a shard to pass the player to (for now) - ./service shard && ./service login - Many new environment variables, check config/config.go for more info. or for a tl;dr just read the Dockerfile for the required ones - Shard and login services now run in different processes ! (and containers?? wooaaah)
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package db
|
|
|
|
/*
|
|
This database has been based off of openfusion's. Databases should be completely interchangable between
|
|
openfusion and gopenfusion.
|
|
*/
|
|
|
|
import (
|
|
"database/sql"
|
|
_ "embed"
|
|
"fmt"
|
|
|
|
"github.com/CPunch/gopenfusion/config"
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type DBHandler struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
//go:embed migrations/new.sql
|
|
var createDBQuery string
|
|
|
|
func OpenPostgresDB(dbAddr string) (*DBHandler, error) {
|
|
fmt := fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", config.GetDBUser(), config.GetDBPass(), dbAddr, config.GetDBName())
|
|
|
|
db, err := sql.Open("postgres", fmt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DBHandler{db}, nil
|
|
}
|
|
|
|
func (db *DBHandler) Query(query string, args ...any) (*sql.Rows, error) {
|
|
return db.db.Query(query, args...)
|
|
}
|
|
|
|
func (db *DBHandler) Exec(query string, args ...any) (sql.Result, error) {
|
|
return db.db.Exec(query, args...)
|
|
}
|
|
|
|
func (db *DBHandler) Close() error {
|
|
return db.db.Close()
|
|
}
|
|
|
|
func (db *DBHandler) Setup() error {
|
|
// create db tables
|
|
_, err := db.db.Exec(createDBQuery)
|
|
return err
|
|
}
|
|
|
|
// calls transaction, if transaction returns a non-nil error the transaction is rolled back. otherwise the transaction is committed
|
|
func (db *DBHandler) Transaction(transaction func(*sql.Tx) error) (err error) {
|
|
tx, err := db.db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
// we panic'd ??? rollback and rethrow
|
|
tx.Rollback()
|
|
panic(p)
|
|
} else if err != nil {
|
|
tx.Rollback()
|
|
} else {
|
|
err = tx.Commit()
|
|
}
|
|
}()
|
|
|
|
err = transaction(tx)
|
|
return
|
|
}
|