Compare commits
2 Commits
3e0d24f5f8
...
a438d5b665
Author | SHA1 | Date | |
---|---|---|---|
a438d5b665 | |||
8d159baad7 |
3
Makefile
3
Makefile
@ -39,3 +39,6 @@ migrate-up:
|
||||
|
||||
migrate-down:
|
||||
migrate -database 'sqlite3://gralias.db' -path migrations down
|
||||
|
||||
install-migrate:
|
||||
go install -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
|
||||
|
BIN
gralias.db
BIN
gralias.db
Binary file not shown.
@ -12,7 +12,6 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func abortWithError(w http.ResponseWriter, msg string) {
|
||||
@ -142,11 +141,12 @@ func makeCookie(username string, remote string) (*http.Cookie, error) {
|
||||
// Create a new random session token
|
||||
// sessionToken := xid.New().String()
|
||||
sessionToken := "sessionprefix_" + username
|
||||
expiresAt := time.Now().Add(time.Duration(cfg.SessionLifetime) * time.Second)
|
||||
// expiresAt := time.Now().Add(time.Duration(cfg.SessionLifetime) * time.Second)
|
||||
// Set the token in the session map, along with the session information
|
||||
session := &models.Session{
|
||||
Username: username,
|
||||
Expiry: expiresAt,
|
||||
Username: username,
|
||||
CookieToken: sessionToken,
|
||||
Lifetime: uint32(cfg.SessionLifetime / 60),
|
||||
}
|
||||
cookieName := "session_token"
|
||||
// hmac to protect cookies
|
||||
|
@ -21,15 +21,12 @@ var (
|
||||
func StartTurnTimer(roomID string, duration time.Duration) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if _, exists := timers[roomID]; exists {
|
||||
return // Timer already running
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
done := make(chan bool)
|
||||
timers[roomID] = &roomTimer{ticker: ticker, done: done}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
|
@ -20,7 +20,7 @@ CREATE TABLE rooms (
|
||||
CREATE TABLE players (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
room_id TEXT, -- nullable
|
||||
username TEXT NOT NULL,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
team TEXT NOT NULL DEFAULT '', -- 'red' or 'blue'
|
||||
role TEXT NOT NULL DEFAULT '', -- 'guesser' or 'mime'
|
||||
is_bot BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
@ -67,3 +67,12 @@ CREATE TABLE settings (
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (room_id) REFERENCES rooms(id)
|
||||
);
|
||||
|
||||
CREATE TABLE sessions(
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
lifetime INTEGER NOT NULL DEFAULT 3600,
|
||||
cookie_token TEXT NOT NULL DEFAULT '', -- encoded value
|
||||
username TEXT NOT NULL,
|
||||
FOREIGN KEY (username) REFERENCES players(username)
|
||||
);
|
||||
|
@ -6,20 +6,25 @@ import (
|
||||
|
||||
// each session contains the username of the user and the time at which it expires
|
||||
type Session struct {
|
||||
Username string
|
||||
CurrentRoom string
|
||||
Expiry time.Time
|
||||
ID uint32
|
||||
// CurrentRoom string
|
||||
// Expiry time.Time
|
||||
UpdatedAt time.Time
|
||||
Lifetime uint32 // minutes
|
||||
CookieToken string
|
||||
Username string // username is playerid
|
||||
}
|
||||
|
||||
// we'll use this method later to determine if the session has expired
|
||||
func (s Session) IsExpired() bool {
|
||||
return s.Expiry.Before(time.Now())
|
||||
return time.Now().After(s.UpdatedAt.Add(time.Minute * time.Duration(s.Lifetime)))
|
||||
// return s.Expiry.Before(time.Now())
|
||||
}
|
||||
|
||||
func ListUsernames(ss map[string]*Session) []string {
|
||||
resp := make([]string, 0, len(ss))
|
||||
for _, s := range ss {
|
||||
resp = append(resp, s.Username)
|
||||
}
|
||||
return resp
|
||||
}
|
||||
// func ListUsernames(ss map[string]*Session) []string {
|
||||
// resp := make([]string, 0, len(ss))
|
||||
// for _, s := range ss {
|
||||
// resp = append(resp, s.Username)
|
||||
// }
|
||||
// return resp
|
||||
// }
|
||||
|
@ -15,7 +15,8 @@ type AllRepos interface {
|
||||
}
|
||||
|
||||
type RepoProvider struct {
|
||||
DB *sqlx.DB
|
||||
DB *sqlx.DB
|
||||
Ext sqlx.Ext
|
||||
}
|
||||
|
||||
func NewRepoProvider(pathToDB string) *RepoProvider {
|
||||
|
61
repos/session.go
Normal file
61
repos/session.go
Normal file
@ -0,0 +1,61 @@
|
||||
package repos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gralias/models"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type SessionsRepo interface {
|
||||
SessionByToken(ctx context.Context, token string) (*models.Session, error)
|
||||
SessionCreate(ctx context.Context, session *models.Session) error
|
||||
SessionUpdate(ctx context.Context, session *models.Session) error
|
||||
SessionDelete(ctx context.Context, token string) error
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionByToken(ctx context.Context, token string) (*models.Session, error) {
|
||||
session := &models.Session{}
|
||||
// The lifetime in the DB is in seconds, but in the model it is in minutes.
|
||||
err := p.DB.GetContext(ctx, session, `SELECT id, updated_at, lifetime / 60 as lifetime, cookie_token, username FROM sessions WHERE cookie_token = ?`, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionCreate(ctx context.Context, session *models.Session) error {
|
||||
p.Ext = p.DB
|
||||
tx, ok := ctx.Value("sqltx").(*sqlx.Tx)
|
||||
if ok && tx != nil {
|
||||
// how to know if it is a final exec in chain?
|
||||
// or is it better to commit outside?
|
||||
tocommit, ok := ctx.Value("tocommit").(bool)
|
||||
if ok && tocommit {
|
||||
defer func() {
|
||||
if err := tx.Commit(); err != nil {
|
||||
// log
|
||||
// return err
|
||||
}
|
||||
}()
|
||||
}
|
||||
p.Ext = tx
|
||||
}
|
||||
// The lifetime in the model is in minutes, but in the DB it is in seconds.
|
||||
_, err := p.Ext.Exec(`INSERT INTO sessions (updated_at, lifetime, cookie_token, username) VALUES (?, ?, ?, ?)`,
|
||||
time.Now(), session.Lifetime*60, session.CookieToken, session.Username)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionUpdate(ctx context.Context, session *models.Session) error {
|
||||
// The lifetime in the model is in minutes, but in the DB it is in seconds.
|
||||
_, err := p.DB.ExecContext(ctx, `UPDATE sessions SET updated_at = ?, lifetime = ? WHERE cookie_token = ?`,
|
||||
time.Now(), session.Lifetime*60, session.CookieToken)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RepoProvider) SessionDelete(ctx context.Context, token string) error {
|
||||
_, err := p.DB.ExecContext(ctx, `DELETE FROM sessions WHERE cookie_token = ?`, token)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user