Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ CORS_ORIGINS=http://localhost:3000
# Hytale Staging OAuth Credentials (optional, required for Hytale features)
HYTALE_USE_STAGING=false

# JWT Secret (REQUIRED - for JWT token generation)
JWT_SECRET="your-jwt-secret-here"

# Sentry Error Tracking (optional)
# DSN from: https://console.sentry.io/
# SENTRY_DSN=https://key@sentry.io/project
Expand Down
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.2.2] - unreleased

### Added
- **Unified Database CLI Tool** - Consolidated shell scripts into cross-platform Go CLI
- Supports Windows, macOS, and Linux without native shell dependencies
- `db init` - Initialize database schema with interactive schema selection
- `db migrate` - Run specific schema migrations with validation
- `db reset` - Complete database reset with confirmation prompt
- `db list` - Display all available schema files with status
- Makefile integration with environment variable loading from `.env`
- **Consolidated Next.js API Routes to Go Backend** - Complete migration of frontend API routes to backend
- Moved all `/api/admin/` endpoints from Next.js to Go Fiber backend
- Admin user management: `GET/POST /api/admin/users`, `POST /api/admin/users/roles`
- Admin settings: `GET/POST /api/admin/settings`, `POST /api/admin/settings/test`
- GitHub repositories: `GET/POST/PUT/DELETE /api/admin/settings/repos`
- Discord webhooks: `GET/POST/PUT/PATCH/DELETE /api/admin/settings/webhooks`
- Admin sync controls: `GET/POST /api/admin/sync`, `GET /api/admin/sync/logs`, `GET/POST /api/admin/sync/settings`
- Admin servers: `GET /api/admin/servers`
- Bearer token authentication middleware for all admin routes
- Consistent error response format across all endpoints
- **Admin User Management API** - Complete user listing and management endpoints
- `GET /api/admin/users` - Paginated user listing with filtering, sorting, search
- Query parameters: page, pageSize, sortField, sortOrder, filter, search
- Returns paginated response with user data and statistics
- `POST /api/admin/users/roles` - Update user role assignments
- User statistics: totalUsers, migratedUsers, adminCount, activeCount
- **Hytale Token Auto-Push to Pterodactyl** - Automatic environment variable updates for game servers
- Game sessions can be linked to specific Pterodactyl servers via `server_id` field
- Background worker automatically pushes refreshed tokens to Pterodactyl every 5 minutes
Expand All @@ -18,6 +41,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Graceful degradation: Logs warnings but continues if Pterodactyl push fails

### Fixed
- **Admin Users Data Type Handling** - Fixed TIMESTAMP column scanning from PostgreSQL
- Changed timestamp handling to use `time.Time` objects with RFC3339 formatting
- Properly convert database TIMESTAMP columns to ISO 8601 string format in API responses
- Fixed empty users array issue in admin panel users listing
- Proper null pointer handling for nullable timestamp fields (`lastLoginAt`, `emailVerifiedTime`)
- **Server-Allocation Relationship Sync** - Fixed missing foreign key population
- Added `Relationships` field to `PteroServer` struct to capture included allocations from API
- `syncServers()` now properly updates `server_id` foreign key in `allocations` table
Expand Down
54 changes: 53 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# NodeByte Backend Makefile
# Usage: make [target]

# Load environment variables from .env file
ifneq (,$(wildcard .env))
include .env
export $(shell sed 's/=.*//' .env)
endif

# Variables
BINARY_NAME=nodebyte-backend
MAIN_PATH=./cmd/api
Expand All @@ -18,7 +24,7 @@ YELLOW=
RED=
NC=

.PHONY: all build run dev clean test lint fmt vet deps tidy docker-build docker-up docker-down docker-logs swagger help
.PHONY: all build build-tools run dev clean test lint fmt vet deps tidy docker-build docker-up docker-down docker-logs swagger help db-init db-migrate db-reset db-list

# Default target
all: build
Expand All @@ -42,6 +48,13 @@ build-all:
GOOS=darwin GOARCH=arm64 $(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 $(MAIN_PATH)
@echo "All builds complete"

# Build database tools
build-tools:
@echo "Building database tools..."
@if not exist "$(BUILD_DIR)" mkdir "$(BUILD_DIR)"
$(GO) build $(GOFLAGS) -o $(BUILD_DIR)/db ./cmd/db
@echo "Database tools built: $(BUILD_DIR)/db"

## Run Commands

# Run the application
Expand Down Expand Up @@ -169,6 +182,37 @@ docker-clean:

## Database Commands

# Build database migration tools
build-db-tools: build-tools

# Initialize fresh database with all schemas
db-init: build-tools
@echo "Initializing database..."
$(BUILD_DIR)/db init -database "$(DATABASE_URL)"

# Run interactive migration
db-migrate: build-tools
@echo "Running database migration..."
$(BUILD_DIR)/db migrate -database "$(DATABASE_URL)"

# Migrate specific schema
db-migrate-schema: build-tools
@if "$(SCHEMA)"=="" (
@echo "Usage: make db-migrate-schema SCHEMA=schema_name.sql"
@exit /b 1
)
@echo "Migrating $(SCHEMA)..."
$(BUILD_DIR)/db migrate -database "$(DATABASE_URL)" -schema "$(SCHEMA)"

# Reset database (DROP and recreate) - CAREFUL!
db-reset: build-tools
@echo "WARNING: This will DROP and recreate the database!"
$(BUILD_DIR)/db reset -database "$(DATABASE_URL)"

# List available schemas
db-list: build-tools
$(BUILD_DIR)/db list

# Generate sqlc code (if using sqlc)
sqlc:
@echo "Generating sqlc code..."
Expand Down Expand Up @@ -207,6 +251,7 @@ help:
@echo "Build:"
@echo " make build - Build the application"
@echo " make build-all - Build for all platforms"
@echo " make build-tools - Build database tools"
@echo ""
@echo "Run:"
@echo " make run - Build and run"
Expand All @@ -222,6 +267,13 @@ help:
@echo " make vet - Run go vet"
@echo " make check - Run all checks"
@echo ""
@echo "Database:"
@echo " make db-init - Initialize fresh database"
@echo " make db-migrate - Run interactive migration"
@echo " make db-migrate-schema SCHEMA=schema_name.sql - Migrate specific schema"
@echo " make db-reset - Reset database (DROP and recreate)"
@echo " make db-list - List available schemas"
@echo ""
@echo "Dependencies:"
@echo " make deps - Download dependencies"
@echo " make tidy - Tidy dependencies"
Expand Down
136 changes: 136 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,142 @@ docker-compose logs -f backend
- Tests for business logic and API handlers
- Code must pass: `gofmt`, `go vet`, `golangci-lint`

# Database Tools - Quick Reference

## One-Time Setup

```bash
cd backend
make build-tools
```

## Common Commands

### Fresh Database
```bash
make db-init
```

### Add New Schemas (Interactive)
```bash
make db-migrate
# Then select schema numbers from menu (e.g., 14,15)
```

### Single Schema
```bash
make db-migrate-schema SCHEMA=schema_15_careers.sql
```

### Start Fresh
```bash
make db-reset
# Confirm by typing database name
```

### See Available Schemas
```bash
make db-list
```

## With Environment Variable

```bash
export DATABASE_URL="postgresql://user:password@localhost:5432/nodebyte"

# Then use commands normally
make db-init
make db-migrate
make db-reset
```

## Direct Binary Usage

```bash
# All commands also work with the binary directly
./bin/db init -database "postgresql://user:password@localhost:5432/nodebyte"
./bin/db migrate -database "postgresql://user:password@localhost:5432/nodebyte"
./bin/db migrate -database "..." -schema schema_15_careers.sql
./bin/db reset -database "postgresql://user:password@localhost:5432/nodebyte"
./bin/db list
./bin/db help
```

## Development Workflow

```bash
# Start fresh
make db-reset
# Confirm: nodebyte
# ✅ Database is now reset and initialized

# Make changes, run tests
# ...

# Add new schema during development
make db-migrate-schema SCHEMA=schema_16_new_feature.sql

# Or choose from menu
make db-migrate
```

## Makefile Targets

```
db-init # Initialize fresh database
db-migrate # Interactive schema selection
db-migrate-schema # Migrate specific schema (SCHEMA=name)
db-reset # Drop and recreate database
db-list # List available schemas
build-tools # Build database tool
```

## Common Issues

**Tool not found?**
```bash
make build-tools
```

**Wrong database connected?**
```bash
export DATABASE_URL="postgresql://user:password@correct-host/correct-db"
make db-migrate
```

**Need to start over?**
```bash
make db-reset
# Type database name to confirm
# Database is now fresh with all 15 schemas
```

## Environment Variables

| Variable | Purpose | Default |
|----------|---------|---------|
| `DATABASE_URL` | PostgreSQL connection string | (none) |
| `SCHEMA` | Used with `db-migrate-schema` | (none) |

## More Information

- **Full Guide**: See `DATABASE_TOOLS.md`
- **Implementation**: See `DATABASE_IMPLEMENTATION.md`
- **Schema Details**: See `schemas/README.md`

---

**Quick Test:**
```bash
make build-tools && make db-list
```

**Help:**
```bash
make help
./bin/db help
```

### Pre-Commit Checks

Before pushing, ensure code passes all checks:
Expand Down
7 changes: 4 additions & 3 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ func main() {
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
}))
app.Use(cors.New(cors.Config{
AllowOrigins: cfg.CORSOrigins,
AllowHeaders: "Origin, Content-Type, Accept, Authorization, X-API-Key",
AllowMethods: "GET, POST, PUT, DELETE, OPTIONS",
AllowOrigins: cfg.CORSOrigins,
AllowHeaders: "Origin, Content-Type, Accept, Authorization, X-API-Key",
AllowMethods: "GET, POST, PUT, DELETE, OPTIONS, PATCH",
AllowCredentials: true,
}))

// API key middleware for protected routes
Expand Down
Loading
Loading