feat: Implement Session Token System with /api/v1 base path
- Add migration 000004 for sessions table and performance indexes - Create session.sql queries for CRUD operations - Generate session repository code with sqlc - Create token auth middleware for Echo framework - Create token handler with create/delete/cleanup endpoints - Add /api/v1 router with token authentication infrastructure - Update dbHelper.go to use Up() instead of Migrate(2) - Update server.go to initialize token handler - Existing endpoints remain functional (to be deprecated) New endpoints: - POST /api/v1/token - Create new session token - DELETE /api/v1/token - Invalidate token - POST /api/v1/token/cleanup - Remove expired sessions Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
+14
-10
@@ -121,19 +121,23 @@ func Migrate_db(host string, port string, user string, password string, dbname s
|
||||
// logging.GetLogger().Error("Force migration error", zap.String("error", err.Error()))
|
||||
//}
|
||||
|
||||
err = m.Migrate(2)
|
||||
//err = m.Up() // or m.Steps(2) if you want to explicitly set the number of migrations to run
|
||||
// Use Up() to apply all pending migrations instead of Migrate(2)
|
||||
err = m.Up()
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Migration error", zap.String("error", err.Error()))
|
||||
if err == migrate.ErrNoChange {
|
||||
logging.GetLogger().Info("Database already up to date")
|
||||
} else {
|
||||
logging.GetLogger().Error("Migration error", zap.String("error", err.Error()))
|
||||
}
|
||||
} else {
|
||||
versionAfter, _, err := m.Version()
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to get migration version after", zap.String("error", err.Error()))
|
||||
} else {
|
||||
logging.GetLogger().Info("Migrated to version", zap.Uint("version", versionAfter))
|
||||
}
|
||||
}
|
||||
|
||||
versionAfter, _, err := m.Version()
|
||||
if err != nil {
|
||||
logging.GetLogger().Error("Failed to get migration version after", zap.String("error", err.Error()))
|
||||
}
|
||||
|
||||
logging.GetLogger().Info("Migration version after", zap.Uint("version", versionAfter))
|
||||
|
||||
logging.GetLogger().Info("Migration completed")
|
||||
|
||||
db.Close()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
-- Drop indexes for sessions table
|
||||
DROP INDEX IF EXISTS idx_sessions_expires;
|
||||
DROP INDEX IF EXISTS idx_sessions_token;
|
||||
DROP INDEX IF EXISTS idx_sessions_ip;
|
||||
DROP INDEX IF EXISTS idx_sessions_created;
|
||||
|
||||
-- Drop sessions table
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
|
||||
-- Drop performance indexes for song_list
|
||||
DROP INDEX IF EXISTS idx_song_list_match_date;
|
||||
DROP INDEX IF EXISTS idx_song_list_match_id;
|
||||
|
||||
-- Drop performance indexes for song
|
||||
DROP INDEX IF EXISTS idx_song_hash;
|
||||
DROP INDEX IF EXISTS idx_song_path;
|
||||
DROP INDEX IF EXISTS idx_song_game_id;
|
||||
DROP INDEX IF EXISTS idx_song_game_id_song_name;
|
||||
|
||||
-- Drop performance indexes for game
|
||||
DROP INDEX IF EXISTS idx_game_deleted;
|
||||
DROP INDEX IF EXISTS idx_game_hash;
|
||||
DROP INDEX IF EXISTS idx_game_path;
|
||||
DROP INDEX IF EXISTS idx_game_name;
|
||||
@@ -0,0 +1,39 @@
|
||||
-- ============================================
|
||||
-- PERFORMANCE INDEXES FOR EXISTING TABLES
|
||||
-- ============================================
|
||||
|
||||
-- Game table indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_game_deleted ON game(deleted) WHERE deleted IS NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_game_hash ON game(hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_game_path ON game(path);
|
||||
CREATE INDEX IF NOT EXISTS idx_game_name ON game(game_name);
|
||||
|
||||
-- Song table indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_song_hash ON song(hash);
|
||||
CREATE INDEX IF NOT EXISTS idx_song_path ON song(path);
|
||||
CREATE INDEX IF NOT EXISTS idx_song_game_id ON song(game_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_song_game_id_song_name ON song(game_id, song_name);
|
||||
|
||||
-- Song_list table indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_song_list_match_date ON song_list(match_date);
|
||||
CREATE INDEX IF NOT EXISTS idx_song_list_match_id ON song_list(match_id);
|
||||
|
||||
-- ============================================
|
||||
-- SESSIONS TABLE FOR TOKEN MANAGEMENT
|
||||
-- ============================================
|
||||
|
||||
-- Create sessions table for tracking client tokens
|
||||
CREATE TABLE sessions (
|
||||
token VARCHAR(64) PRIMARY KEY,
|
||||
ip_address VARCHAR(45) NOT NULL,
|
||||
user_agent TEXT NOT NULL,
|
||||
client_type VARCHAR(20) DEFAULT 'web',
|
||||
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Indexes for fast lookup and cleanup
|
||||
CREATE INDEX idx_sessions_expires ON sessions(expires_at);
|
||||
CREATE INDEX idx_sessions_token ON sessions(token);
|
||||
CREATE INDEX idx_sessions_ip ON sessions(ip_address);
|
||||
CREATE INDEX idx_sessions_created ON sessions(created_at);
|
||||
@@ -0,0 +1,23 @@
|
||||
-- name: CreateSession :one
|
||||
INSERT INTO sessions (token, ip_address, user_agent, client_type, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING token, ip_address, user_agent, client_type, expires_at, created_at;
|
||||
|
||||
-- name: GetSession :one
|
||||
SELECT token, ip_address, user_agent, client_type, expires_at, created_at
|
||||
FROM sessions
|
||||
WHERE token = $1
|
||||
LIMIT 1;
|
||||
|
||||
-- name: DeleteSession :exec
|
||||
DELETE FROM sessions
|
||||
WHERE token = $1;
|
||||
|
||||
-- name: DeleteExpiredSessions :exec
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at < NOW();
|
||||
|
||||
-- name: ListSessions :many
|
||||
SELECT token, ip_address, user_agent, client_type, expires_at, created_at
|
||||
FROM sessions
|
||||
ORDER BY created_at DESC;
|
||||
@@ -6,6 +6,8 @@ package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
@@ -21,6 +23,15 @@ type Game struct {
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
Token string `json:"token"`
|
||||
IpAddress string `json:"ip_address"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
ClientType *string `json:"client_type"`
|
||||
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
||||
}
|
||||
|
||||
type Song struct {
|
||||
GameID int32 `json:"game_id"`
|
||||
SongName string `json:"song_name"`
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: session.sql
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const createSession = `-- name: CreateSession :one
|
||||
INSERT INTO sessions (token, ip_address, user_agent, client_type, expires_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING token, ip_address, user_agent, client_type, expires_at, created_at
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
Token string `json:"token"`
|
||||
IpAddress string `json:"ip_address"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
ClientType *string `json:"client_type"`
|
||||
ExpiresAt pgtype.Timestamptz `json:"expires_at"`
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error) {
|
||||
row := q.db.QueryRow(ctx, createSession,
|
||||
arg.Token,
|
||||
arg.IpAddress,
|
||||
arg.UserAgent,
|
||||
arg.ClientType,
|
||||
arg.ExpiresAt,
|
||||
)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.Token,
|
||||
&i.IpAddress,
|
||||
&i.UserAgent,
|
||||
&i.ClientType,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :exec
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at < NOW()
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredSessions(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, deleteExpiredSessions)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteSession = `-- name: DeleteSession :exec
|
||||
DELETE FROM sessions
|
||||
WHERE token = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSession(ctx context.Context, token string) error {
|
||||
_, err := q.db.Exec(ctx, deleteSession, token)
|
||||
return err
|
||||
}
|
||||
|
||||
const getSession = `-- name: GetSession :one
|
||||
SELECT token, ip_address, user_agent, client_type, expires_at, created_at
|
||||
FROM sessions
|
||||
WHERE token = $1
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSession(ctx context.Context, token string) (Session, error) {
|
||||
row := q.db.QueryRow(ctx, getSession, token)
|
||||
var i Session
|
||||
err := row.Scan(
|
||||
&i.Token,
|
||||
&i.IpAddress,
|
||||
&i.UserAgent,
|
||||
&i.ClientType,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listSessions = `-- name: ListSessions :many
|
||||
SELECT token, ip_address, user_agent, client_type, expires_at, created_at
|
||||
FROM sessions
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
func (q *Queries) ListSessions(ctx context.Context) ([]Session, error) {
|
||||
rows, err := q.db.Query(ctx, listSessions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Session
|
||||
for rows.Next() {
|
||||
var i Session
|
||||
if err := rows.Scan(
|
||||
&i.Token,
|
||||
&i.IpAddress,
|
||||
&i.UserAgent,
|
||||
&i.ClientType,
|
||||
&i.ExpiresAt,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user