gofmt, rid code of typescript confusion, little bit of db code
This commit is contained in:
parent
25e51fb2d5
commit
e59f3e1609
4 changed files with 140 additions and 121 deletions
|
@ -1,47 +1,47 @@
|
||||||
package gametable
|
package gametable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"nhooyr.io/websocket"
|
"errors"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"time"
|
"log"
|
||||||
"sync"
|
"net/http"
|
||||||
"net/http"
|
"nhooyr.io/websocket"
|
||||||
"log"
|
"sync"
|
||||||
"errors"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Subscriber struct {
|
type Subscriber struct {
|
||||||
msgs chan []byte
|
msgs chan []byte
|
||||||
closeSlow func()
|
closeSlow func()
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameTableServer struct {
|
type GameTableServer struct {
|
||||||
subscribeMessageBuffer int
|
subscribeMessageBuffer int
|
||||||
publishLimiter *rate.Limiter
|
publishLimiter *rate.Limiter
|
||||||
logf func(f string, v ...interface{})
|
logf func(f string, v ...interface{})
|
||||||
serveMux http.ServeMux
|
serveMux http.ServeMux
|
||||||
subscribersLock sync.Mutex
|
subscribersLock sync.Mutex
|
||||||
subscribers map[*Subscriber]struct{}
|
subscribers map[*Subscriber]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *GameTableServer {
|
func New() *GameTableServer {
|
||||||
srvr := &GameTableServer {
|
srvr := &GameTableServer{
|
||||||
subscribeMessageBuffer: 16,
|
subscribeMessageBuffer: 16,
|
||||||
logf: log.Printf,
|
logf: log.Printf,
|
||||||
subscribers: make(map[*Subscriber]struct{}),
|
subscribers: make(map[*Subscriber]struct{}),
|
||||||
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
|
publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8),
|
||||||
}
|
}
|
||||||
srvr.serveMux.Handle("/", http.FileServer(http.Dir("./static")))
|
srvr.serveMux.Handle("/", http.FileServer(http.Dir("./static")))
|
||||||
srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler)
|
srvr.serveMux.HandleFunc("/subscribe", srvr.subscribeHandler)
|
||||||
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
|
srvr.serveMux.HandleFunc("/publish", srvr.publishHandler)
|
||||||
|
|
||||||
return srvr
|
return srvr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *GameTableServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (self *GameTableServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
self.serveMux.ServeHTTP(w, r)
|
self.serveMux.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *GameTableServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
|
func (self *GameTableServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -112,7 +112,7 @@ func (self *GameTableServer) publish(msg []byte) {
|
||||||
self.subscribersLock.Lock()
|
self.subscribersLock.Lock()
|
||||||
defer self.subscribersLock.Unlock()
|
defer self.subscribersLock.Unlock()
|
||||||
|
|
||||||
// decode message and store in DB
|
// decode message and store in DB
|
||||||
|
|
||||||
self.publishLimiter.Wait(context.Background())
|
self.publishLimiter.Wait(context.Background())
|
||||||
|
|
||||||
|
|
22
main.go
22
main.go
|
@ -1,14 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"nilfm.cc/git/felt/gametable"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"nilfm.cc/git/felt/gametable"
|
||||||
"os/signal"
|
"os"
|
||||||
"time"
|
"os/signal"
|
||||||
"log"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -26,9 +26,9 @@ func run() error {
|
||||||
|
|
||||||
gt := gametable.New()
|
gt := gametable.New()
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Handler: gt,
|
Handler: gt,
|
||||||
ReadTimeout: time.Second * 10,
|
ReadTimeout: time.Second * 10,
|
||||||
WriteTimeout: time.Second * 10,
|
WriteTimeout: time.Second * 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TableKey struct {
|
type TableKey struct {
|
||||||
name: string
|
Name string
|
||||||
passcode: string
|
Passcode string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiceRoll struct {
|
type DiceRoll struct {
|
||||||
faces: uint8
|
Faces uint8
|
||||||
roll: uint8[]
|
Roll []uint8
|
||||||
player: string
|
Player string
|
||||||
note: string
|
Note string
|
||||||
timestamp: time.Time
|
Timestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Token struct {
|
type Token struct {
|
||||||
id: string
|
Id string
|
||||||
spriteUri: string
|
SpriteUri string
|
||||||
x: int
|
X int
|
||||||
y: int
|
Y int
|
||||||
}
|
}
|
|
@ -1,76 +1,95 @@
|
||||||
package dbengine
|
package dbengine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"nilfm.cc/git/felt/models"
|
||||||
"nilfm.cc/git/felt/models"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
interface DbAdapter {
|
type DbAdapter interface {
|
||||||
Init(mongoUri: string): error
|
Init(mongoUri string) error
|
||||||
|
|
||||||
CreateTable(table: models.TableKey): error
|
CreateTable(table models.TableKey) error
|
||||||
DestroyTable(table: models.TableKey): error
|
DestroyTable(table models.TableKey) error
|
||||||
|
|
||||||
InsertDiceRoll(table: models.TableKey, diceRoll: models.DiceRoll): error
|
InsertDiceRoll(table models.TableKey, diceRoll models.DiceRoll) error
|
||||||
GetDiceRolls(table: models.TableKey): models.DiceRoll[], error
|
GetDiceRolls(table models.TableKey) ([]models.DiceRoll, error)
|
||||||
|
|
||||||
SetMapImageUrl(table: models.TableKey, url: string): error
|
SetMapImageUrl(table models.TableKey, url string) error
|
||||||
GetMapImageUrl(table: models.TableKey): string, error
|
GetMapImageUrl(table models.TableKey) (string, error)
|
||||||
|
|
||||||
AddToken(table: models.TableKey, token: models.Token): error
|
AddToken(table models.TableKey, token models.Token) error
|
||||||
RemoveToken(table: models.TableKey, tokenId: string): error
|
RemoveToken(table models.TableKey, tokenId string) error
|
||||||
ModifyToken(table: models.TableKey, token: models.Token): error
|
ModifyToken(table models.TableKey, token models.Token) error
|
||||||
GetTokens(table: models.TableKey): models.Token[], error
|
GetTokens(table models.TableKey) ([]models.Token, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbEngine struct {
|
type DbEngine struct {
|
||||||
client: mongo.Client
|
client mongo.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DbEngine) Init(mongoUri: string) error {
|
func (self *DbEngine) mkCtx(timeoutSec int) context.Context {
|
||||||
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
|
return context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
self.client = client
|
|
||||||
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
|
|
||||||
err = client.Connect(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer client.Disconnect(ctx)
|
|
||||||
|
|
||||||
db := client.Database("felt")
|
|
||||||
|
|
||||||
err = self.ensureCollections(db)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *DbEngine) ensureCollections(db: mongo.Database) error {
|
func (self *DbEngine) Init(mongoUri string) error {
|
||||||
tables := db.Collection("tables")
|
client, err := mongo.NewClient(options.Client().ApplyURI(mongoUri))
|
||||||
if tables == nil {
|
if err != nil {
|
||||||
createCmd := bson.D{
|
return err
|
||||||
{"create", "tables"},
|
}
|
||||||
{"clusteredIndex", {
|
self.client = client
|
||||||
{"key", {"name"}},
|
ctx, _ := self.mkCtx(10)
|
||||||
{"unique", true},
|
|
||||||
{"name", "idx_tables_unique_names"}
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
var createResult bson.M
|
err = client.Connect(ctx)
|
||||||
err := db.RunCommand(
|
if err != nil {
|
||||||
context.WithTimeout(context.Background(), 10*time.Second),
|
return err
|
||||||
createCmd).Decode(&createResult)
|
}
|
||||||
|
defer client.Disconnect(ctx)
|
||||||
|
|
||||||
if err != nil {
|
db := client.Database("felt")
|
||||||
return err
|
|
||||||
}
|
err = self.ensureCollections(db)
|
||||||
}
|
return err
|
||||||
return nil
|
}
|
||||||
|
|
||||||
|
func (self *DbEngine) ensureCollections(db mongo.Database) error {
|
||||||
|
tables := db.Collection("tables")
|
||||||
|
if tables == nil {
|
||||||
|
createCmd := bson.D{
|
||||||
|
{"create", "tables"},
|
||||||
|
{"clusteredIndex", {
|
||||||
|
{"key", {"name"}},
|
||||||
|
{"unique", true},
|
||||||
|
{"name", "idx_tables_unique_names"},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
var createResult bson.M
|
||||||
|
err := db.RunCommand(
|
||||||
|
self.mkCtx(10),
|
||||||
|
createCmd).Decode(&createResult)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *DbEngine) CreateTable(table models.TableKey) error {
|
||||||
|
tables := self.db.Collection("tables")
|
||||||
|
if tables != nil {
|
||||||
|
_, err := tables.insertOne(self.mkCtx(10), bson.D{
|
||||||
|
{"name", table.Name},
|
||||||
|
{"passcode", table.Passcode},
|
||||||
|
{"mapUri", ""},
|
||||||
|
{"diceRolls", bson.A{}},
|
||||||
|
{"tokens", bson.A{}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue