feat: add total player count and total playtime logic
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m27s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 1m27s
- Implemented TotalPlayerCountLogic to retrieve the total number of unique players within a specified time range. - Implemented TotalPlayTimeLogic to calculate the total playtime of players within a specified time range. - Created ServiceContext to manage database connections and initialize necessary tables. - Added types for total player count and total playtime requests and responses. - Set up the main server file to start the application with the necessary configurations. - Updated go.mod and go.sum for new dependencies.
This commit is contained in:
64
app/internal/svc/servicecontext.go
Normal file
64
app/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/ServerStatistics/app/internal/config"
|
||||
|
||||
_ "github.com/lib/pq" // PostgreSQL driver
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
// Initialize database connection
|
||||
db, err := sql.Open("postgres", c.Database.DSN)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
// Test the connection
|
||||
if err := db.Ping(); err != nil {
|
||||
log.Fatalf("Failed to ping database: %v", err)
|
||||
}
|
||||
|
||||
log.Println("Database connection established successfully")
|
||||
|
||||
// Create tables if they don't exist
|
||||
if err := createTables(db); err != nil {
|
||||
log.Fatalf("Failed to create tables: %v", err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func createTables(db *sql.DB) error {
|
||||
// Create server table
|
||||
serverTableQuery := `
|
||||
CREATE TABLE IF NOT EXISTS server (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(64) NOT NULL,
|
||||
ip VARCHAR(64) NOT NULL,
|
||||
port INTEGER NOT NULL,
|
||||
category VARCHAR(64) NOT NULL,
|
||||
bot_count INTEGER NOT NULL,
|
||||
human_count INTEGER NOT NULL,
|
||||
mapname VARCHAR(64) NOT NULL,
|
||||
last_update TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (ip, port)
|
||||
);`
|
||||
|
||||
if _, err := db.Exec(serverTableQuery); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Database tables created successfully")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user