feat: major API restructure and Vue.js frontend integration

- Centralize API endpoints in dedicated api app with v1 versioning
- Remove individual API modules from parks and rides apps
- Add event tracking system with analytics functionality
- Integrate Vue.js frontend with Tailwind CSS v4 and TypeScript
- Add comprehensive database migrations for event tracking
- Implement user authentication and social provider setup
- Add API schema documentation and serializers
- Configure development environment with shared scripts
- Update project structure for monorepo with frontend/backend separation
This commit is contained in:
pacnpal
2025-08-24 16:42:20 -04:00
parent 92f4104d7a
commit e62646bcf9
127 changed files with 27734 additions and 1867 deletions

465
shared/docs/api/README.md Normal file
View File

@@ -0,0 +1,465 @@
# ThrillWiki API Documentation
Complete API reference for the ThrillWiki Django REST API backend.
## Overview
The ThrillWiki API provides comprehensive access to theme park and roller coaster data through a RESTful interface designed specifically for the Vue.js frontend. The API uses Django REST Framework with custom frontend-compatible serializers that convert Django's snake_case to JavaScript's camelCase convention.
**Base URL:** `http://localhost:8000/api/`
## Authentication
The API currently supports anonymous access for read operations. Authentication will be added in future versions for write operations and user-specific features.
## API Endpoints
### Parks
#### List Parks
```http
GET /api/parks/
```
**Response Format:**
```json
{
"count": 150,
"next": "http://localhost:8000/api/parks/?page=2",
"previous": null,
"results": [
{
"id": 1,
"name": "Cedar Point",
"slug": "cedar-point",
"location": "Sandusky, Ohio",
"operator": "Cedar Fair",
"openingYear": 1870,
"status": "open",
"description": "America's Roller Coast",
"website": "https://www.cedarpoint.com",
"imageUrl": "/placeholder-park.jpg"
}
]
}
```
**Query Parameters:**
- `page` - Page number for pagination
- `search` - Search parks by name or location
- `status` - Filter by park status (`open`, `closed`, `seasonal`)
- `operator` - Filter by operator name
#### Get Park Details
```http
GET /api/parks/{id}/
```
**Response Format:**
```json
{
"id": 1,
"name": "Cedar Point",
"slug": "cedar-point",
"location": "Sandusky, Ohio",
"operator": "Cedar Fair",
"openingYear": 1870,
"status": "open",
"description": "America's Roller Coast",
"website": "https://www.cedarpoint.com",
"imageUrl": "/placeholder-park.jpg",
"averageRating": 4.5,
"rideCount": 72,
"coasterCount": 17,
"sizeAcres": 364,
"operatingSeason": "May-October"
}
```
### Rides
#### List Rides
```http
GET /api/rides/
```
**Response Format:**
```json
{
"count": 500,
"next": "http://localhost:8000/api/rides/?page=2",
"previous": null,
"results": [
{
"id": 1,
"name": "Millennium Force",
"slug": "millennium-force",
"parkId": 1,
"parkName": "Cedar Point",
"parkSlug": "cedar-point",
"category": "roller_coaster",
"manufacturer": "Intamin",
"designer": "Werner Stengel",
"openingYear": 2000,
"height": 310,
"speed": 93,
"length": 6595,
"inversions": 0,
"status": "operating",
"description": "World's tallest complete-circuit roller coaster",
"imageUrl": "/placeholder-ride.jpg",
"thrillLevel": "extreme"
}
]
}
```
**Query Parameters:**
- `page` - Page number for pagination
- `search` - Search rides by name
- `park` - Filter by park ID
- `category` - Filter by ride category (`roller_coaster`, `dark_ride`, `flat_ride`, `water_ride`, `transport`, `other`)
- `status` - Filter by ride status (`operating`, `closed`, `sbno`, `under_construction`)
- `manufacturer` - Filter by manufacturer name
- `thrill_level` - Filter by thrill level (`family`, `moderate`, `intense`, `extreme`)
#### Get Ride Details
```http
GET /api/rides/{id}/
```
**Response Format:**
```json
{
"id": 1,
"name": "Millennium Force",
"slug": "millennium-force",
"parkId": 1,
"parkName": "Cedar Point",
"parkSlug": "cedar-point",
"category": "roller_coaster",
"manufacturer": "Intamin",
"designer": "Werner Stengel",
"openingYear": 2000,
"height": 310,
"speed": 93,
"length": 6595,
"inversions": 0,
"status": "operating",
"description": "World's tallest complete-circuit roller coaster",
"imageUrl": "/placeholder-ride.jpg",
"thrillLevel": "extreme",
"minHeightIn": 48,
"maxHeightIn": 78,
"capacityPerHour": 1300,
"rideDurationSeconds": 120,
"averageRating": 4.8,
"closingDate": null,
"statusSince": "2000-05-13",
"coasterStats": {
"trackMaterial": "steel",
"coasterType": "hypercoaster",
"launchType": "chain_lift",
"maxDropHeightFt": 300,
"rideTimeSeconds": 120,
"trainsCount": 2,
"carsPerTrain": 9,
"seatsPerCar": 4,
"trainStyle": "open_air",
"trackType": "complete_circuit"
}
}
```
#### Get Rides by Park
```http
GET /api/parks/{park_id}/rides/
```
Returns all rides for a specific park using the same format as the main rides endpoint.
## Data Models
### Park Data Structure
| Field | Type | Description |
|-------|------|-------------|
| `id` | integer | Unique park identifier |
| `name` | string | Park name |
| `slug` | string | URL-friendly park identifier |
| `location` | string | Formatted location (e.g., "Sandusky, Ohio") |
| `operator` | string | Park operating company |
| `openingYear` | integer | Year park opened |
| `status` | string | Park status: `open`, `closed`, `seasonal` |
| `description` | string | Park description |
| `website` | string | Official website URL |
| `imageUrl` | string | Primary park image URL |
| `averageRating` | float | Average user rating (detail view only) |
| `rideCount` | integer | Total number of rides (detail view only) |
| `coasterCount` | integer | Number of roller coasters (detail view only) |
| `sizeAcres` | float | Park size in acres (detail view only) |
| `operatingSeason` | string | Operating season description (detail view only) |
### Ride Data Structure
| Field | Type | Description |
|-------|------|-------------|
| `id` | integer | Unique ride identifier |
| `name` | string | Ride name |
| `slug` | string | URL-friendly ride identifier |
| `parkId` | integer | Parent park ID |
| `parkName` | string | Parent park name |
| `parkSlug` | string | Parent park slug |
| `category` | string | Ride category |
| `manufacturer` | string | Ride manufacturer |
| `designer` | string | Ride designer |
| `openingYear` | integer | Year ride opened |
| `height` | float | Height in feet (coasters only) |
| `speed` | float | Speed in mph (coasters only) |
| `length` | float | Length in feet (coasters only) |
| `inversions` | integer | Number of inversions (coasters only) |
| `status` | string | Ride status |
| `description` | string | Ride description |
| `imageUrl` | string | Primary ride image URL |
| `thrillLevel` | string | Calculated thrill level |
| `minHeightIn` | integer | Minimum height requirement (detail view only) |
| `maxHeightIn` | integer | Maximum height requirement (detail view only) |
| `capacityPerHour` | integer | Hourly capacity (detail view only) |
| `rideDurationSeconds` | integer | Ride duration in seconds (detail view only) |
| `averageRating` | float | Average user rating (detail view only) |
| `closingDate` | date | Date ride closed (detail view only) |
| `statusSince` | date | Date status changed (detail view only) |
| `coasterStats` | object | Detailed coaster statistics (detail view only) |
### Coaster Statistics Structure
| Field | Type | Description |
|-------|------|-------------|
| `trackMaterial` | string | Track material (steel, wood) |
| `coasterType` | string | Coaster type (hypercoaster, inverted, etc.) |
| `launchType` | string | Launch mechanism |
| `maxDropHeightFt` | float | Maximum drop height in feet |
| `rideTimeSeconds` | integer | Total ride time in seconds |
| `trainsCount` | integer | Number of trains |
| `carsPerTrain` | integer | Cars per train |
| `seatsPerCar` | integer | Seats per car |
| `trainStyle` | string | Train style (open_air, enclosed) |
| `trackType` | string | Track configuration |
## Field Mappings
### Django to Frontend Field Conversion
The API automatically converts Django's snake_case field names to JavaScript's camelCase convention:
| Django Model | Frontend API |
|--------------|--------------|
| `opening_date` | `openingYear` |
| `min_height_in` | `minHeightIn` |
| `max_height_in` | `maxHeightIn` |
| `capacity_per_hour` | `capacityPerHour` |
| `ride_duration_seconds` | `rideDurationSeconds` |
| `coaster_stats` | `coasterStats` |
| `size_acres` | `sizeAcres` |
| `ride_count` | `rideCount` |
| `coaster_count` | `coasterCount` |
| `average_rating` | `averageRating` |
### Status Mappings
#### Park Status
| Django | Frontend |
|--------|----------|
| `OPERATING` | `open` |
| `CLOSED_TEMP` | `seasonal` |
| `CLOSED_PERM` | `closed` |
| `UNDER_CONSTRUCTION` | `closed` |
| `DEMOLISHED` | `closed` |
| `RELOCATED` | `closed` |
#### Ride Status
| Django | Frontend |
|--------|----------|
| `OPERATING` | `operating` |
| `CLOSED_TEMP` | `closed` |
| `SBNO` | `sbno` |
| `CLOSING` | `closed` |
| `CLOSED_PERM` | `closed` |
| `UNDER_CONSTRUCTION` | `under_construction` |
| `DEMOLISHED` | `closed` |
| `RELOCATED` | `closed` |
### Category Mappings
#### Ride Categories
| Django | Frontend |
|--------|----------|
| `RC` | `roller_coaster` |
| `DR` | `dark_ride` |
| `FR` | `flat_ride` |
| `WR` | `water_ride` |
| `TR` | `transport` |
| `OT` | `other` |
## Error Handling
The API returns standard HTTP status codes with detailed error information:
### Error Response Format
```json
{
"error": {
"code": "NOT_FOUND",
"message": "Park with id 999 not found",
"details": {}
}
}
```
### Common HTTP Status Codes
- `200 OK` - Successful request
- `201 Created` - Resource created successfully
- `400 Bad Request` - Invalid request data
- `401 Unauthorized` - Authentication required
- `403 Forbidden` - Insufficient permissions
- `404 Not Found` - Resource not found
- `429 Too Many Requests` - Rate limit exceeded
- `500 Internal Server Error` - Server error
## Pagination
List endpoints support pagination with the following format:
```json
{
"count": 150,
"next": "http://localhost:8000/api/parks/?page=2",
"previous": null,
"results": [...]
}
```
**Query Parameters:**
- `page` - Page number (default: 1)
- `page_size` - Items per page (default: 20, max: 100)
## Rate Limiting
The API implements rate limiting to prevent abuse:
- **Anonymous users:** 100 requests per hour
- **Authenticated users:** 1000 requests per hour
Rate limit headers are included in responses:
- `X-RateLimit-Limit` - Request limit per hour
- `X-RateLimit-Remaining` - Remaining requests
- `X-RateLimit-Reset` - Time until reset (Unix timestamp)
## CORS Configuration
The API is configured to work with the Vue.js frontend:
- **Allowed origins:** `http://localhost:5174` (development)
- **Allowed methods:** `GET`, `POST`, `PUT`, `DELETE`, `OPTIONS`
- **Allowed headers:** `Content-Type`, `Authorization`, `X-Requested-With`
## Frontend Integration
### Vue.js Service Layer
The frontend uses dedicated service functions for API communication:
```typescript
// services/parkService.ts
export const parkService = {
async getParks(params?: ParkQueryParams): Promise<PaginatedResponse<Park>> {
const response = await apiClient.get('/parks/', { params });
return response.data;
},
async getPark(id: number): Promise<ParkDetail> {
const response = await apiClient.get(`/parks/${id}/`);
return response.data;
}
};
```
### Pinia Store Integration
API responses are managed through Pinia stores:
```typescript
// stores/parks.ts
export const useParksStore = defineStore('parks', () => {
const parks = ref<Park[]>([]);
const loading = ref(false);
const fetchParks = async () => {
loading.value = true;
try {
const response = await parkService.getParks();
parks.value = response.results;
} finally {
loading.value = false;
}
};
return { parks, loading, fetchParks };
});
```
## Development & Testing
### API Testing with curl
```bash
# Get list of parks
curl "http://localhost:8000/api/parks/"
# Get specific park
curl "http://localhost:8000/api/parks/1/"
# Search parks
curl "http://localhost:8000/api/parks/?search=cedar"
# Filter by status
curl "http://localhost:8000/api/parks/?status=open"
```
### Django REST Framework Browsable API
When `DEBUG=True`, the API provides a browsable interface at each endpoint URL. This interface allows:
- Interactive API browsing
- Form-based testing
- Authentication testing
- Request/response inspection
## Future Enhancements
### Planned Features
- **Authentication & Authorization** - JWT-based user authentication
- **User Preferences** - Personalized park/ride recommendations
- **Image Upload** - User-contributed photos
- **Review System** - User ratings and reviews
- **Social Features** - Following parks/rides, activity feeds
- **Advanced Search** - Full-text search with filters
- **Real-time Updates** - WebSocket support for live data
### API Versioning
Future API versions will be supported via URL versioning:
- `/api/v1/parks/` - Version 1 (current)
- `/api/v2/parks/` - Version 2 (future)
## Support
For API-related questions or issues:
- Check the [Django REST Framework documentation](https://www.django-rest-framework.org/)
- Review the [frontend integration guide](../frontend/README.md)
- Create an issue in the project repository
## Changelog
### Version 1.0.0
- Initial API release
- Parks and rides endpoints
- Frontend-compatible serialization
- Pagination and filtering support
- CORS configuration for Vue.js integration

View File

@@ -0,0 +1,649 @@
# Development Workflow
Comprehensive guide to daily development processes, Git workflow, and team collaboration for the ThrillWiki monorepo.
## Overview
This document outlines the development workflow for the ThrillWiki Django + Vue.js monorepo. Following these practices ensures consistent code quality, smooth collaboration, and reliable deployments.
## 🏗️ Project Structure
```
thrillwiki-monorepo/
├── backend/ # Django REST API
├── frontend/ # Vue.js SPA
├── shared/ # Shared resources and scripts
├── architecture/ # Architecture documentation
└── profiles/ # Development profiles
```
## 🚀 Daily Development Workflow
### 1. Environment Setup
#### First Time Setup
```bash
# Clone the repository
git clone <repository-url>
cd thrillwiki-monorepo
# Run the automated setup script
./shared/scripts/dev/setup-dev.sh
# Or set up manually
cd backend && uv sync && cd ..
pnpm install
```
#### Daily Setup
```bash
# Start all development servers
./shared/scripts/dev/start-all.sh
# Or start individually
pnpm run dev:backend # Django on :8000
pnpm run dev:frontend # Vue.js on :5174
```
### 2. Development Process
#### Feature Development
1. **Create a feature branch**
```bash
git checkout -b feature/your-feature-name
```
2. **Make your changes**
- Follow the coding standards for your language
- Write tests for new functionality
- Update documentation as needed
3. **Test your changes**
```bash
# Backend tests
cd backend && uv run manage.py test
# Frontend tests
cd frontend && pnpm test:unit
cd frontend && pnpm test:e2e
# Full test suite
pnpm run test
```
4. **Code quality checks**
```bash
# Backend
cd backend && uv run black . && uv run flake8 .
# Frontend
cd frontend && pnpm lint && pnpm type-check
```
#### Bug Fixes
1. **Create a bug fix branch**
```bash
git checkout -b bugfix/issue-description
```
2. **Reproduce the issue**
- Add test cases that reproduce the bug
- Verify the fix resolves the issue
3. **Implement the fix**
- Keep changes minimal and focused
- Ensure no regressions
### 3. Code Review Process
#### Before Submitting
- [ ] All tests pass
- [ ] Code follows style guidelines
- [ ] Documentation updated
- [ ] No linting errors
- [ ] TypeScript types correct (frontend)
- [ ] Database migrations created (backend)
#### Pull Request Process
1. **Create Pull Request**
- Use descriptive title
- Fill out PR template
- Link related issues
- Add screenshots for UI changes
2. **Code Review**
- At least one approval required
- Address all review comments
- Rebase and resolve conflicts
3. **Merge Process**
- Use "Squash and merge" for feature branches
- Use "Rebase and merge" for maintenance branches
- Delete branch after merge
## 🔄 Git Workflow
### Branch Naming Convention
```
feature/feature-name # New features
bugfix/issue-description # Bug fixes
hotfix/critical-issue # Critical production fixes
refactor/component-name # Code refactoring
docs/update-documentation # Documentation updates
test/add-test-coverage # Test improvements
```
### Commit Message Format
```
type(scope): description
[optional body]
[optional footer]
```
**Types:**
- `feat` - New feature
- `fix` - Bug fix
- `docs` - Documentation
- `style` - Code style changes
- `refactor` - Code refactoring
- `test` - Adding tests
- `chore` - Maintenance tasks
**Examples:**
```
feat: add park search functionality
fix: resolve ride detail page crash
docs: update API documentation
refactor: simplify park service layer
```
### Git Workflow
#### Feature Development
```bash
# Start from main
git checkout main
git pull origin main
# Create feature branch
git checkout -b feature/new-park-search
# Make changes and commit
git add .
git commit -m "feat: implement park search with filters"
# Push and create PR
git push origin feature/new-park-search
```
#### Handling Conflicts
```bash
# Update your branch with latest main
git fetch origin
git rebase origin/main
# Resolve conflicts if any
# Test your changes
# Force push if needed
git push --force-with-lease origin feature/new-park-search
```
## 🧪 Testing Strategy
### Backend Testing
#### Unit Tests
```bash
cd backend
# Run all tests
uv run manage.py test
# Run specific app tests
uv run manage.py test apps.parks
# Run with coverage
uv run coverage run manage.py test
uv run coverage report
```
#### Test Structure
```
backend/tests/
├── test_models.py # Model tests
├── test_views.py # View tests
├── test_serializers.py # Serializer tests
└── test_services.py # Service layer tests
```
### Frontend Testing
#### Unit Tests (Vitest)
```bash
cd frontend
# Run unit tests
pnpm test:unit
# Run with coverage
pnpm test:unit --coverage
# Watch mode for development
pnpm test:unit --watch
```
#### End-to-End Tests (Playwright)
```bash
cd frontend
# Run E2E tests
pnpm test:e2e
# Run specific test
pnpm test:e2e tests/park-search.spec.ts
# Debug mode
pnpm test:e2e --debug
```
#### Test Structure
```
frontend/src/__tests__/
├── components/ # Component tests
├── views/ # Page tests
├── services/ # Service tests
└── stores/ # Store tests
```
### Test Coverage Requirements
- **Backend:** Minimum 80% coverage
- **Frontend:** Minimum 70% coverage
- **Critical paths:** 90%+ coverage
## 🔧 Code Quality
### Backend Standards
#### Python Code Style
```bash
cd backend
# Format code
uv run black .
# Check style
uv run flake8 .
# Sort imports
uv run isort .
```
#### Django Best Practices
- Use Django REST Framework best practices
- Implement proper error handling
- Add database indexes for performance
- Use select_related and prefetch_related for optimization
### Frontend Standards
#### Vue.js Best Practices
```bash
cd frontend
# Lint code
pnpm lint
# Type checking
pnpm type-check
# Format code
pnpm format
```
#### Code Organization
- Use Composition API with `<script setup>`
- Implement proper component structure
- Follow Vue.js style guide
- Use TypeScript for type safety
## 🚀 Deployment Process
### Development Environment
#### Automated Deployment
```bash
# Deploy to development
./shared/scripts/deploy/deploy.sh dev
# Check deployment status
./shared/scripts/deploy/check-status.sh dev
```
#### Manual Deployment
1. **Build frontend**
```bash
cd frontend
pnpm build
```
2. **Run backend migrations**
```bash
cd backend
uv run manage.py migrate
```
3. **Collect static files**
```bash
cd backend
uv run manage.py collectstatic
```
### Staging Environment
#### Deployment Checklist
- [ ] All tests pass
- [ ] Code review completed
- [ ] Documentation updated
- [ ] Database migrations tested
- [ ] Environment variables configured
#### Deployment Steps
```bash
# Build for staging
pnpm run build:staging
# Deploy to staging
./shared/scripts/deploy/deploy.sh staging
# Run smoke tests
./shared/scripts/deploy/smoke-test.sh staging
```
### Production Environment
#### Pre-Deployment
1. **Create release branch**
```bash
git checkout -b release/v1.2.3
```
2. **Update version numbers**
- Update `package.json`
- Update `pyproject.toml`
- Tag the release
3. **Final testing**
```bash
# Run full test suite
pnpm run test
# Performance testing
./shared/scripts/test/performance-test.sh
```
#### Production Deployment
```bash
# Build for production
pnpm run build:production
# Deploy to production
./shared/scripts/deploy/deploy.sh production
# Verify deployment
./shared/scripts/deploy/health-check.sh production
# Rollback if needed
./shared/scripts/deploy/rollback.sh production
```
## 🔍 Monitoring and Debugging
### Development Debugging
#### Backend Debugging
```bash
cd backend
# Django shell for debugging
uv run manage.py shell
# Django debug toolbar (development only)
# Access at http://localhost:8000/__debug__/
# Logging
tail -f logs/django.log
```
#### Frontend Debugging
```bash
cd frontend
# Vue DevTools browser extension
# Access at http://localhost:5174/__devtools__/
# Browser developer tools
# - Console for errors
# - Network tab for API calls
# - Vue tab for component inspection
```
### Production Monitoring
#### Health Checks
```bash
# API health check
curl http://localhost:8000/api/health/
# Database connectivity
./shared/scripts/monitor/db-check.sh
# Background job status
./shared/scripts/monitor/job-status.sh
```
#### Error Tracking
- Check application logs
- Monitor error rates
- Review performance metrics
- User feedback and bug reports
## 📚 Documentation
### Documentation Updates
#### When to Update Documentation
- New features implemented
- API changes
- Configuration changes
- Deployment process changes
#### Documentation Standards
- Use clear, concise language
- Include code examples
- Provide step-by-step instructions
- Update README files
- Maintain API documentation
### Documentation Files
- `README.md` - Main project documentation
- `backend/README.md` - Backend-specific documentation
- `frontend/README.md` - Frontend-specific documentation
- `shared/docs/api/README.md` - API documentation
- `shared/docs/development/workflow.md` - This file
## 🤝 Team Collaboration
### Communication
#### Daily Standup
- What did you work on yesterday?
- What are you working on today?
- Any blockers or issues?
#### Code Reviews
- Be constructive and respectful
- Focus on code quality and best practices
- Suggest improvements, don't demand changes
- Explain reasoning behind suggestions
### Knowledge Sharing
#### Documentation
- Update README files
- Document complex business logic
- Create troubleshooting guides
#### Pair Programming
- Share knowledge through pair programming
- Rotate team members on different parts of the system
- Cross-train on both backend and frontend
## 🛠️ Development Tools
### Essential Tools
#### Version Control
- **Git** - Version control system
- **GitHub** - Repository hosting and collaboration
- **GitHub Actions** - CI/CD pipeline
#### Code Quality
- **ESLint** - JavaScript/TypeScript linting
- **Prettier** - Code formatting
- **Black** - Python code formatting
- **Flake8** - Python linting
#### Testing
- **Vitest** - Frontend unit testing
- **Playwright** - E2E testing
- **pytest** - Backend testing
### Development Environment
#### IDE Configuration
- **VSCode** with recommended extensions
- **Vue Language Features** extension
- **Python** extension
- **Prettier** extension
#### Local Development
- **Docker** for consistent environments
- **direnv** for environment variables
- **nodenv** for Node.js version management
## 🚨 Emergency Procedures
### Critical Issues
#### Production Outage
1. **Assess the situation**
- Identify the scope of the issue
- Determine impact on users
2. **Communication**
- Notify team members
- Update status page if applicable
3. **Resolution**
```bash
# Quick rollback if needed
./shared/scripts/deploy/rollback.sh production
# Or hotfix
git checkout -b hotfix/critical-issue
# Implement fix
./shared/scripts/deploy/deploy.sh production
```
#### Security Issues
1. **Assess vulnerability**
- Determine severity and impact
- Check for exploitation
2. **Response**
- Implement temporary mitigation
- Develop permanent fix
- Deploy security patch
3. **Post-mortem**
- Document the incident
- Update security procedures
- Improve monitoring
## 📈 Performance Optimization
### Frontend Performance
#### Bundle Optimization
```bash
# Analyze bundle size
cd frontend
pnpm build --analyze
# Code splitting
const ParkDetail = () => import('./views/ParkDetail.vue')
```
#### Image Optimization
- Use WebP format for images
- Implement lazy loading
- Optimize image sizes
### Backend Performance
#### Database Optimization
```python
# Use select_related for foreign keys
parks = Park.objects.select_related('operator')
# Use prefetch_related for many-to-many
parks = Park.objects.prefetch_related('rides')
```
#### Caching Strategy
- Cache frequently accessed data
- Use Redis for session storage
- Implement database query caching
## 🔒 Security Best Practices
### Code Security
- Regular dependency updates
- Security scanning with tools like Snyk
- Code review security checklist
- Input validation and sanitization
### Infrastructure Security
- HTTPS everywhere
- Secure headers implementation
- Regular security audits
- Access control and authentication
## 📋 Checklist
### Before Committing Code
- [ ] Tests pass
- [ ] Code follows style guidelines
- [ ] No linting errors
- [ ] Documentation updated
- [ ] Commit message follows convention
### Before Creating Pull Request
- [ ] Feature branch up to date with main
- [ ] All tests pass
- [ ] Code review checklist completed
- [ ] Documentation updated
- [ ] No merge conflicts
### Before Merging to Main
- [ ] CI/CD pipeline passes
- [ ] Code review approved
- [ ] Tests pass in CI environment
- [ ] No critical security issues
This workflow ensures consistent, high-quality development across the ThrillWiki monorepo while maintaining excellent collaboration and deployment practices.

494
shared/scripts/deploy/deploy.sh Executable file
View File

@@ -0,0 +1,494 @@
#!/bin/bash
# ThrillWiki Deployment Script
# Deploys the application to various environments
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../" && pwd)"
# Configuration
DEPLOY_ENV="production"
DEPLOY_DIR="$PROJECT_ROOT/deploy"
BACKUP_DIR="$PROJECT_ROOT/backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check deployment requirements
check_deployment_requirements() {
print_status "Checking deployment requirements..."
local missing_deps=()
# Check if deployment artifacts exist
if [ ! -d "$DEPLOY_DIR" ]; then
missing_deps+=("deployment_artifacts")
fi
if [ ! -f "$DEPLOY_DIR/manifest.json" ]; then
missing_deps+=("deployment_manifest")
fi
# Check for deployment tools
if [ "$DEPLOY_METHOD" = "docker" ]; then
if ! command_exists docker; then
missing_deps+=("docker")
fi
fi
if [ "$DEPLOY_METHOD" = "rsync" ]; then
if ! command_exists rsync; then
missing_deps+=("rsync")
fi
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "Missing deployment requirements: ${missing_deps[*]}"
exit 1
fi
print_success "Deployment requirements met!"
}
# Function to create backup
create_backup() {
print_status "Creating backup before deployment..."
mkdir -p "$BACKUP_DIR"
local backup_path="$BACKUP_DIR/backup_$TIMESTAMP"
# Create backup directory
mkdir -p "$backup_path"
# Backup current deployment if it exists
if [ -d "$DEPLOY_TARGET" ]; then
print_status "Backing up current deployment..."
cp -r "$DEPLOY_TARGET" "$backup_path/current"
fi
# Backup database if requested
if [ "$BACKUP_DATABASE" = true ]; then
print_status "Backing up database..."
# This would depend on your database setup
# For SQLite:
if [ -f "$PROJECT_ROOT/backend/db.sqlite3" ]; then
cp "$PROJECT_ROOT/backend/db.sqlite3" "$backup_path/database.sqlite3"
fi
fi
# Backup environment files
if [ -f "$PROJECT_ROOT/.env" ]; then
cp "$PROJECT_ROOT/.env" "$backup_path/.env.backup"
fi
print_success "Backup created: $backup_path"
}
# Function to prepare deployment artifacts
prepare_artifacts() {
print_status "Preparing deployment artifacts..."
# Check if build artifacts exist
if [ ! -d "$DEPLOY_DIR" ]; then
print_error "No deployment artifacts found. Please run build-all.sh first."
exit 1
fi
# Validate manifest
if [ -f "$DEPLOY_DIR/manifest.json" ]; then
print_status "Validating deployment manifest..."
# You could add more validation here
cat "$DEPLOY_DIR/manifest.json" | grep -q "build_timestamp" || {
print_error "Invalid deployment manifest"
exit 1
}
fi
print_success "Deployment artifacts ready!"
}
# Function to deploy to local development
deploy_local() {
print_status "Deploying to local development environment..."
local target_dir="$PROJECT_ROOT/deployment"
# Create target directory
mkdir -p "$target_dir"
# Copy artifacts
print_status "Copying frontend artifacts..."
cp -r "$DEPLOY_DIR/frontend" "$target_dir/"
print_status "Copying backend artifacts..."
mkdir -p "$target_dir/backend"
cp -r "$DEPLOY_DIR/backend/staticfiles" "$target_dir/backend/"
# Copy deployment configuration
cp "$DEPLOY_DIR/manifest.json" "$target_dir/"
print_success "Local deployment completed!"
print_status "Deployment available at: $target_dir"
}
# Function to deploy via rsync
deploy_rsync() {
print_status "Deploying via rsync..."
if [ -z "$DEPLOY_HOST" ]; then
print_error "DEPLOY_HOST not set for rsync deployment"
exit 1
fi
local target=""
if [ -n "$DEPLOY_USER" ]; then
target="$DEPLOY_USER@$DEPLOY_HOST:$DEPLOY_PATH"
else
target="$DEPLOY_HOST:$DEPLOY_PATH"
fi
print_status "Syncing files to $target..."
# Rsync options:
# -a: archive mode (recursive, preserves attributes)
# -v: verbose
# -z: compress during transfer
# --delete: delete files not in source
# --exclude: exclude certain files
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='__pycache__' \
--exclude='*.log' \
"$DEPLOY_DIR/" "$target"
print_success "Rsync deployment completed!"
}
# Function to deploy via Docker
deploy_docker() {
print_status "Deploying via Docker..."
local image_name="thrillwiki-$DEPLOY_ENV"
local container_name="thrillwiki-$DEPLOY_ENV"
# Build Docker image
print_status "Building Docker image: $image_name"
docker build -t "$image_name" \
--build-arg DEPLOY_ENV="$DEPLOY_ENV" \
-f "$PROJECT_ROOT/Dockerfile" \
"$PROJECT_ROOT"
# Stop existing container
if docker ps -q -f name="$container_name" | grep -q .; then
print_status "Stopping existing container..."
docker stop "$container_name"
fi
# Remove existing container
if docker ps -a -q -f name="$container_name" | grep -q .; then
print_status "Removing existing container..."
docker rm "$container_name"
fi
# Run new container
print_status "Starting new container..."
docker run -d \
--name "$container_name" \
-p 8080:80 \
-e DEPLOY_ENV="$DEPLOY_ENV" \
"$image_name"
print_success "Docker deployment completed!"
print_status "Container: $container_name"
print_status "URL: http://localhost:8080"
}
# Function to run post-deployment checks
run_post_deploy_checks() {
print_status "Running post-deployment checks..."
local health_url=""
case $DEPLOY_METHOD in
"local")
health_url="http://localhost:8080/health"
;;
"docker")
health_url="http://localhost:8080/health"
;;
"rsync")
if [ -n "$DEPLOY_HOST" ]; then
health_url="http://$DEPLOY_HOST/health"
fi
;;
esac
if [ -n "$health_url" ]; then
print_status "Checking health endpoint: $health_url"
if curl -s -f "$health_url" > /dev/null 2>&1; then
print_success "Health check passed!"
else
print_warning "Health check failed. Please verify deployment."
fi
fi
print_success "Post-deployment checks completed!"
}
# Function to generate deployment report
generate_deployment_report() {
print_status "Generating deployment report..."
local report_file="$PROJECT_ROOT/deployment-report-$DEPLOY_ENV-$TIMESTAMP.txt"
cat > "$report_file" << EOF
ThrillWiki Deployment Report
============================
Deployment Information:
- Deployment Date: $(date)
- Environment: $DEPLOY_ENV
- Method: $DEPLOY_METHOD
- Project Root: $PROJECT_ROOT
Deployment Details:
- Source Directory: $DEPLOY_DIR
- Target: $DEPLOY_TARGET
- Backup Created: $([ "$CREATE_BACKUP" = true ] && echo "Yes" || echo "No")
Build Information:
$(if [ -f "$DEPLOY_DIR/manifest.json" ]; then
cat "$DEPLOY_DIR/manifest.json"
else
echo "No manifest found"
fi)
System Information:
- Hostname: $(hostname)
- User: $(whoami)
- OS: $(uname -s) $(uname -r)
Deployment Status: SUCCESS
Post-Deployment:
- Health Check: $([ "$RUN_CHECKS" = true ] && echo "Run" || echo "Skipped")
- Backup Location: $([ "$CREATE_BACKUP" = true ] && echo "$BACKUP_DIR/backup_$TIMESTAMP" || echo "None")
EOF
print_success "Deployment report generated: $report_file"
}
# Function to show usage
show_usage() {
cat << EOF
Usage: $0 [ENVIRONMENT] [OPTIONS]
Deploy ThrillWiki to the specified environment.
Environments:
dev Development environment
staging Staging environment
production Production environment
Options:
-h, --help Show this help message
-m, --method METHOD Deployment method (local, rsync, docker)
--no-backup Skip backup creation
--no-checks Skip post-deployment checks
--no-report Skip deployment report generation
Examples:
$0 production # Deploy to production using default method
$0 staging --method docker # Deploy to staging using Docker
$0 dev --no-backup # Deploy to dev without backup
Environment Variables:
DEPLOY_METHOD Deployment method (default: local)
DEPLOY_HOST Target host for rsync deployment
DEPLOY_USER SSH user for rsync deployment
DEPLOY_PATH Target path for rsync deployment
CREATE_BACKUP Create backup before deployment (default: true)
BACKUP_DATABASE Backup database (default: false)
EOF
}
# Parse command line arguments
DEPLOY_METHOD="local"
CREATE_BACKUP=true
RUN_CHECKS=true
SKIP_REPORT=false
# Get environment from first argument
if [ $# -gt 0 ]; then
case $1 in
dev|staging|production)
DEPLOY_ENV="$1"
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Invalid environment: $1"
show_usage
exit 1
;;
esac
fi
# Parse remaining arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-m|--method)
DEPLOY_METHOD="$2"
shift 2
;;
--no-backup)
CREATE_BACKUP=false
shift
;;
--no-checks)
RUN_CHECKS=false
shift
;;
--no-report)
SKIP_REPORT=true
shift
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Override from environment variables
if [ ! -z "$DEPLOY_METHOD_ENV" ]; then
DEPLOY_METHOD=$DEPLOY_METHOD_ENV
fi
if [ "$CREATE_BACKUP_ENV" = "false" ]; then
CREATE_BACKUP=false
fi
# Set deployment target based on method
case $DEPLOY_METHOD in
"local")
DEPLOY_TARGET="$PROJECT_ROOT/deployment"
;;
"rsync")
DEPLOY_TARGET="${DEPLOY_USER:-}$(if [ -n "$DEPLOY_USER" ]; then echo "@"; fi)${DEPLOY_HOST:-localhost}:${DEPLOY_PATH:-/var/www/thrillwiki}"
;;
"docker")
DEPLOY_TARGET="docker_container"
;;
*)
print_error "Unsupported deployment method: $DEPLOY_METHOD"
exit 1
;;
esac
# Print banner
echo -e "${GREEN}"
echo "=========================================="
echo " ThrillWiki Deployment"
echo "=========================================="
echo -e "${NC}"
print_status "Environment: $DEPLOY_ENV"
print_status "Method: $DEPLOY_METHOD"
print_status "Target: $DEPLOY_TARGET"
print_status "Create backup: $CREATE_BACKUP"
# Check deployment requirements
check_deployment_requirements
# Prepare deployment artifacts
prepare_artifacts
# Create backup if requested
if [ "$CREATE_BACKUP" = true ]; then
create_backup
else
print_warning "Skipping backup creation as requested"
fi
# Deploy based on method
case $DEPLOY_METHOD in
"local")
deploy_local
;;
"rsync")
deploy_rsync
;;
"docker")
deploy_docker
;;
*)
print_error "Unsupported deployment method: $DEPLOY_METHOD"
exit 1
;;
esac
# Run post-deployment checks
if [ "$RUN_CHECKS" = true ]; then
run_post_deploy_checks
else
print_warning "Skipping post-deployment checks as requested"
fi
# Generate deployment report
if [ "$SKIP_REPORT" = false ]; then
generate_deployment_report
else
print_warning "Skipping deployment report generation as requested"
fi
print_success "Deployment completed successfully!"
echo ""
print_status "Environment: $DEPLOY_ENV"
print_status "Method: $DEPLOY_METHOD"
print_status "Target: $DEPLOY_TARGET"
echo ""
print_status "Deployment report: $PROJECT_ROOT/deployment-report-$DEPLOY_ENV-$TIMESTAMP.txt"

368
shared/scripts/dev/setup-dev.sh Executable file
View File

@@ -0,0 +1,368 @@
#!/bin/bash
# ThrillWiki Development Environment Setup
# Sets up the complete development environment for both backend and frontend
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../" && pwd)"
# Configuration
BACKEND_DIR="$PROJECT_ROOT/backend"
FRONTEND_DIR="$PROJECT_ROOT/frontend"
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check system requirements
check_requirements() {
print_status "Checking system requirements..."
local missing_deps=()
# Check Python
if ! command_exists python3; then
missing_deps+=("python3")
else
local python_version=$(python3 --version | cut -d' ' -f2 | cut -d'.' -f1,2)
if (( $(echo "$python_version < 3.11" | bc -l) )); then
print_warning "Python version $python_version detected. Python 3.11+ recommended."
fi
fi
# Check uv
if ! command_exists uv; then
missing_deps+=("uv")
fi
# Check Node.js
if ! command_exists node; then
missing_deps+=("node")
else
local node_version=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
if (( node_version < 18 )); then
print_warning "Node.js version $node_version detected. Node.js 18+ recommended."
fi
fi
# Check pnpm
if ! command_exists pnpm; then
missing_deps+=("pnpm")
fi
# Check PostgreSQL (optional)
if ! command_exists psql; then
print_warning "PostgreSQL not found. SQLite will be used for development."
fi
# Check Redis (optional)
if ! command_exists redis-server; then
print_warning "Redis not found. Some features may not work."
fi
if [ ${#missing_deps[@]} -ne 0 ]; then
print_error "Missing required dependencies: ${missing_deps[*]}"
print_status "Please install the missing dependencies and run this script again."
print_status "Installation instructions:"
print_status " - Python 3.11+: https://www.python.org/downloads/"
print_status " - uv: pip install uv"
print_status " - Node.js 18+: https://nodejs.org/"
print_status " - pnpm: npm install -g pnpm"
exit 1
fi
print_success "All system requirements met!"
}
# Function to setup backend
setup_backend() {
print_status "Setting up Django backend..."
cd "$BACKEND_DIR"
# Install Python dependencies with uv
print_status "Installing Python dependencies..."
if [ ! -d ".venv" ]; then
uv sync
else
print_warning "Virtual environment already exists. Updating dependencies..."
uv sync
fi
# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
print_status "Creating backend .env file..."
cp .env.example .env
print_warning "Please edit backend/.env with your settings"
else
print_warning "Backend .env file already exists"
fi
# Run database migrations
print_status "Running database migrations..."
uv run manage.py migrate
# Create superuser (optional)
print_status "Creating Django superuser..."
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.filter(username='admin').exists() or User.objects.create_superuser('admin', 'admin@example.com', 'admin')" | uv run manage.py shell
print_success "Backend setup completed!"
cd "$PROJECT_ROOT"
}
# Function to setup frontend
setup_frontend() {
print_status "Setting up Vue.js frontend..."
cd "$FRONTEND_DIR"
# Install Node.js dependencies
print_status "Installing Node.js dependencies..."
if [ ! -d "node_modules" ]; then
pnpm install
else
print_warning "node_modules already exists. Updating dependencies..."
pnpm install
fi
# Create environment files if they don't exist
if [ ! -f ".env.local" ]; then
print_status "Creating frontend .env.local file..."
cp .env.development .env.local
print_warning "Please edit frontend/.env.local with your settings"
else
print_warning "Frontend .env.local file already exists"
fi
print_success "Frontend setup completed!"
cd "$PROJECT_ROOT"
}
# Function to setup root environment
setup_root_env() {
print_status "Setting up root environment..."
cd "$PROJECT_ROOT"
# Create root .env file if it doesn't exist
if [ ! -f ".env" ]; then
print_status "Creating root .env file..."
cp .env.example .env
print_warning "Please edit .env with your settings"
else
print_warning "Root .env file already exists"
fi
print_success "Root environment setup completed!"
}
# Function to verify setup
verify_setup() {
print_status "Verifying setup..."
local issues=()
# Check backend
cd "$BACKEND_DIR"
if [ ! -d ".venv" ]; then
issues+=("Backend virtual environment not found")
fi
if [ ! -f ".env" ]; then
issues+=("Backend .env file not found")
fi
# Check if Django can start
if ! uv run manage.py check --settings=config.django.local >/dev/null 2>&1; then
issues+=("Django configuration check failed")
fi
cd "$FRONTEND_DIR"
# Check frontend
if [ ! -d "node_modules" ]; then
issues+=("Frontend node_modules not found")
fi
if [ ! -f ".env.local" ]; then
issues+=("Frontend .env.local file not found")
fi
# Check if Vue can build
if ! pnpm run type-check >/dev/null 2>&1; then
issues+=("Vue.js type check failed")
fi
cd "$PROJECT_ROOT"
if [ ${#issues[@]} -ne 0 ]; then
print_warning "Setup verification found issues:"
for issue in "${issues[@]}"; do
echo -e " - ${YELLOW}$issue${NC}"
done
return 1
else
print_success "Setup verification passed!"
return 0
fi
}
# Function to show usage
show_usage() {
cat << EOF
Usage: $0 [OPTIONS]
Set up the complete ThrillWiki development environment.
Options:
-h, --help Show this help message
-b, --backend-only Setup only the backend
-f, --frontend-only Setup only the frontend
-y, --yes Skip confirmation prompts
--no-verify Skip setup verification
Examples:
$0 # Setup both backend and frontend
$0 --backend-only # Setup only backend
$0 --frontend-only # Setup only frontend
Environment Variables:
SKIP_CONFIRMATION Set to 'true' to skip confirmation prompts
SKIP_VERIFICATION Set to 'true' to skip verification
EOF
}
# Parse command line arguments
BACKEND_ONLY=false
FRONTEND_ONLY=false
SKIP_CONFIRMATION=false
SKIP_VERIFICATION=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-b|--backend-only)
BACKEND_ONLY=true
shift
;;
-f|--frontend-only)
FRONTEND_ONLY=true
shift
;;
-y|--yes)
SKIP_CONFIRMATION=true
shift
;;
--no-verify)
SKIP_VERIFICATION=true
shift
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Override from environment variables
if [ "$SKIP_CONFIRMATION" = "true" ] || [ "$SKIP_CONFIRMATION_ENV" = "true" ]; then
SKIP_CONFIRMATION=true
fi
if [ "$SKIP_VERIFICATION" = "true" ] || [ "$SKIP_VERIFICATION_ENV" = "true" ]; then
SKIP_VERIFICATION=true
fi
# Print banner
echo -e "${GREEN}"
echo "=========================================="
echo " ThrillWiki Development Setup"
echo "=========================================="
echo -e "${NC}"
print_status "Project root: $PROJECT_ROOT"
# Confirmation prompt
if [ "$SKIP_CONFIRMATION" = false ]; then
echo ""
read -p "This will set up the development environment. Continue? (y/N): " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_status "Setup cancelled."
exit 0
fi
fi
# Check requirements
check_requirements
# Setup components based on options
if [ "$BACKEND_ONLY" = true ]; then
print_status "Setting up backend only..."
setup_backend
setup_root_env
elif [ "$FRONTEND_ONLY" = true ]; then
print_status "Setting up frontend only..."
setup_frontend
setup_root_env
else
print_status "Setting up both backend and frontend..."
setup_backend
setup_frontend
setup_root_env
fi
# Verify setup
if [ "$SKIP_VERIFICATION" = false ]; then
echo ""
if verify_setup; then
print_success "Development environment setup completed successfully!"
echo ""
print_status "Next steps:"
echo " 1. Edit .env files with your configuration"
echo " 2. Start development servers: ./shared/scripts/dev/start-all.sh"
echo " 3. Visit http://localhost:5174 for the frontend"
echo " 4. Visit http://localhost:8000 for the backend API"
echo ""
print_status "Happy coding! 🚀"
else
print_warning "Setup completed with issues. Please review the warnings above."
exit 1
fi
else
print_success "Development environment setup completed!"
print_status "Skipped verification as requested."
fi

279
shared/scripts/dev/start-all.sh Executable file
View File

@@ -0,0 +1,279 @@
#!/bin/bash
# ThrillWiki Development Server Starter
# Starts both Django backend and Vue.js frontend servers concurrently
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory and project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../../" && pwd)"
# Configuration
BACKEND_PORT=8000
FRONTEND_PORT=5174
BACKEND_DIR="$PROJECT_ROOT/backend"
FRONTEND_DIR="$PROJECT_ROOT/frontend"
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if a port is available
check_port() {
local port=$1
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
return 1
else
return 0
fi
}
# Function to kill process on port
kill_port() {
local port=$1
local pid=$(lsof -ti:$port)
if [ ! -z "$pid" ]; then
print_warning "Killing process $pid on port $port"
kill -9 $pid
fi
}
# Function to wait for service to be ready
wait_for_service() {
local url=$1
local service_name=$2
local max_attempts=30
local attempt=1
print_status "Waiting for $service_name to be ready at $url"
while [ $attempt -le $max_attempts ]; do
if curl -s -f "$url" > /dev/null 2>&1; then
print_success "$service_name is ready!"
return 0
fi
echo -n "."
sleep 2
((attempt++))
done
print_error "$service_name failed to start after $max_attempts attempts"
return 1
}
# Function to start backend server
start_backend() {
print_status "Starting Django backend server..."
# Kill any existing process on backend port
kill_port $BACKEND_PORT
# Clean up Python cache files
find "$BACKEND_DIR" -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
cd "$BACKEND_DIR"
# Check if virtual environment exists
if [ ! -d ".venv" ]; then
print_error "Backend virtual environment not found. Please run setup-dev.sh first."
exit 1
fi
# Start Django server in background
print_status "Starting Django development server on port $BACKEND_PORT"
uv run manage.py runserver 0.0.0.0:$BACKEND_PORT &
BACKEND_PID=$!
# Wait for backend to be ready
wait_for_service "http://localhost:$BACKEND_PORT/api/" "Django backend"
cd "$PROJECT_ROOT"
}
# Function to start frontend server
start_frontend() {
print_status "Starting Vue.js frontend server..."
cd "$FRONTEND_DIR"
# Check if node_modules exists
if [ ! -d "node_modules" ]; then
print_error "Frontend dependencies not installed. Please run setup-dev.sh first."
exit 1
fi
# Start Vue.js dev server in background
print_status "Starting Vue.js development server on port $FRONTEND_PORT"
pnpm run dev &
FRONTEND_PID=$!
# Wait for frontend to be ready
wait_for_service "http://localhost:$FRONTEND_PORT" "Vue.js frontend"
cd "$PROJECT_ROOT"
}
# Function to cleanup on script exit
cleanup() {
print_warning "Shutting down development servers..."
if [ ! -z "$BACKEND_PID" ]; then
kill $BACKEND_PID 2>/dev/null || true
fi
if [ ! -z "$FRONTEND_PID" ]; then
kill $FRONTEND_PID 2>/dev/null || true
fi
# Kill any remaining processes on our ports
kill_port $BACKEND_PORT
kill_port $FRONTEND_PORT
print_success "Development servers stopped."
exit 0
}
# Function to show usage
show_usage() {
cat << EOF
Usage: $0 [OPTIONS]
Start both Django backend and Vue.js frontend development servers.
Options:
-h, --help Show this help message
-b, --backend-only Start only the backend server
-f, --frontend-only Start only the frontend server
-p, --production Start in production mode (if applicable)
--no-wait Don't wait for services to be ready
Examples:
$0 # Start both servers
$0 --backend-only # Start only backend
$0 --frontend-only # Start only frontend
Environment Variables:
BACKEND_PORT Backend server port (default: 8000)
FRONTEND_PORT Frontend server port (default: 5174)
EOF
}
# Parse command line arguments
BACKEND_ONLY=false
FRONTEND_ONLY=false
PRODUCTION=false
WAIT_FOR_SERVICES=true
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-b|--backend-only)
BACKEND_ONLY=true
shift
;;
-f|--frontend-only)
FRONTEND_ONLY=true
shift
;;
-p|--production)
PRODUCTION=true
shift
;;
--no-wait)
WAIT_FOR_SERVICES=false
shift
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Override ports from environment if set
if [ ! -z "$BACKEND_PORT_ENV" ]; then
BACKEND_PORT=$BACKEND_PORT_ENV
fi
if [ ! -z "$FRONTEND_PORT_ENV" ]; then
FRONTEND_PORT=$FRONTEND_PORT_ENV
fi
# Set up signal handlers for graceful shutdown
trap cleanup SIGINT SIGTERM
# Print banner
echo -e "${GREEN}"
echo "=========================================="
echo " ThrillWiki Development Environment"
echo "=========================================="
echo -e "${NC}"
print_status "Project root: $PROJECT_ROOT"
print_status "Backend port: $BACKEND_PORT"
print_status "Frontend port: $FRONTEND_PORT"
# Check if required tools are available
command -v uv >/dev/null 2>&1 || { print_error "uv is required but not installed. Please install uv first."; exit 1; }
command -v pnpm >/dev/null 2>&1 || { print_error "pnpm is required but not installed. Please install pnpm first."; exit 1; }
command -v curl >/dev/null 2>&1 || { print_error "curl is required but not installed."; exit 1; }
# Start services based on options
if [ "$BACKEND_ONLY" = true ]; then
print_status "Starting backend only..."
start_backend
print_success "Backend server started successfully!"
print_status "Backend URL: http://localhost:$BACKEND_PORT"
print_status "API URL: http://localhost:$BACKEND_PORT/api/"
wait
elif [ "$FRONTEND_ONLY" = true ]; then
print_status "Starting frontend only..."
start_frontend
print_success "Frontend server started successfully!"
print_status "Frontend URL: http://localhost:$FRONTEND_PORT"
wait
else
print_status "Starting both backend and frontend servers..."
start_backend &
BACKEND_PID=$!
start_frontend &
FRONTEND_PID=$!
print_success "Development servers started successfully!"
echo ""
print_status "Backend URL: http://localhost:$BACKEND_PORT"
print_status "API URL: http://localhost:$BACKEND_PORT/api/"
print_status "Frontend URL: http://localhost:$FRONTEND_PORT"
echo ""
print_status "Press Ctrl+C to stop all servers"
# Wait for both processes
wait
fi