# Git Commit Message

refactor: add production features and improve architecture

- Add structured configuration with validation
- Implement Redis connection pooling
- Add database migrations system
- Change API methods: GET /set → POST /set, GET /delete → DELETE /delete
- Add health check endpoint
- Add graceful shutdown and structured logging
- Update frontend for new API methods
- Add open source attribution
This commit is contained in:
creations 2025-06-04 07:56:15 -04:00
parent ad6c9b7095
commit 6bfd298455
Signed by: creations
GPG key ID: 8F553AA4320FC711
21 changed files with 842 additions and 3124 deletions

View file

@ -7,8 +7,10 @@ A simple Rust-powered API service for managing and retrieving user timezones.
- Store user timezones via `/set` endpoint (requires Discord OAuth)
- Retrieve timezones by user ID via `/get`
- List all saved timezones
- Cookie-based session handling using Redis
- Cookie-based session handling using Redis connection pooling
- Built-in CORS support
- Structured configuration with validation
- Graceful shutdown support
- Fully containerized with PostgreSQL and DragonflyDB
## Requirements
@ -21,15 +23,27 @@ A simple Rust-powered API service for managing and retrieving user timezones.
Create a `.env` file with the following:
```env
# Server Configuration
HOST=0.0.0.0
PORT=3000
# Database Configuration
DATABASE_URL=postgres://postgres:postgres@postgres:5432/postgres
REDIS_URL=redis://dragonfly:6379
DB_MAX_CONNECTIONS=10
DB_CONNECT_TIMEOUT=30
# Redis Configuration
REDIS_URL=redis://dragonfly:6379
REDIS_POOL_SIZE=5
REDIS_CONNECT_TIMEOUT=10
# Discord OAuth Configuration
CLIENT_ID=your_discord_client_id
CLIENT_SECRET=your_discord_client_secret
REDIRECT_URI=https://your.domain/auth/discord/callback
# Logging (optional)
RUST_LOG=info,timezone_db=debug
```
## Setup
@ -40,16 +54,34 @@ REDIRECT_URI=https://your.domain/auth/discord/callback
docker compose up --build
```
### Run Manually
```bash
# Make sure PostgreSQL and Redis are running
cargo run
```
## API Endpoints
### `GET /get?id=<discord_user_id>`
Returns stored timezone and username for the given user ID.
**Response:**
```json
{
"user": {
"id": "123456789",
"username": "username"
},
"timezone": "America/New_York"
}
```
### `POST /set`
Stores timezone for the authenticated user. Requires Discord OAuth session.
Body: `application/x-www-form-urlencoded` with `timezone=<iana_timezone>`
**Body:** `application/x-www-form-urlencoded` with `timezone=<iana_timezone>`
### `DELETE /delete`
@ -59,14 +91,40 @@ Deletes the authenticated user's timezone entry. Requires Discord OAuth session.
Returns a JSON object of all stored timezones by user ID.
**Response:**
```json
{
"123456789": {
"username": "user1",
"timezone": "America/New_York"
},
"987654321": {
"username": "user2",
"timezone": "Europe/London"
}
}
```
### `GET /me`
Returns Discord profile info for the current session.
Returns Discord profile info and timezone for the current session.
### `GET /auth/discord`
Starts OAuth2 authentication flow.
Starts OAuth2 authentication flow. Supports optional `?redirect=` parameter.
### `GET /auth/discord/callback`
Handles OAuth2 redirect and sets a session cookie.
## Configuration
The application uses structured configuration with validation. All required environment variables must be provided, and the app will exit with helpful error messages if configuration is invalid.
### Optional Configuration Variables
- `DB_MAX_CONNECTIONS`: Maximum database connections (default: 10)
- `DB_CONNECT_TIMEOUT`: Database connection timeout in seconds (default: 30)
- `REDIS_POOL_SIZE`: Redis connection pool size (default: 5)
- `REDIS_CONNECT_TIMEOUT`: Redis connection timeout in seconds (default: 10)
- `RUST_LOG`: Logging level configuration