|
| 1 | +# Container Engine - Development Setup |
| 2 | + |
| 3 | +This document provides detailed instructions for setting up and running the Container Engine project locally. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you begin, ensure you have the following installed: |
| 8 | + |
| 9 | +- **Rust** (1.70 or later) - Install from [rustup.rs](https://rustup.rs/) |
| 10 | +- **Docker** and **Docker Compose** - For running PostgreSQL and Redis |
| 11 | +- **Git** - For version control |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +### 1. Clone the Repository |
| 16 | + |
| 17 | +```bash |
| 18 | +git clone https://github.com/ngocbd/Open-Container-Engine.git |
| 19 | +cd Open-Container-Engine |
| 20 | +``` |
| 21 | + |
| 22 | +### 2. Set Up Environment Variables |
| 23 | + |
| 24 | +Copy the example environment file and update it: |
| 25 | + |
| 26 | +```bash |
| 27 | +cp .env.example .env |
| 28 | +``` |
| 29 | + |
| 30 | +Edit `.env` to customize settings if needed. The defaults should work for local development. |
| 31 | + |
| 32 | +### 3. Start Database Services |
| 33 | + |
| 34 | +Using Docker Compose, start PostgreSQL and Redis: |
| 35 | + |
| 36 | +```bash |
| 37 | +docker-compose up postgres redis -d |
| 38 | +``` |
| 39 | + |
| 40 | +This will start: |
| 41 | +- PostgreSQL on port 5432 |
| 42 | +- Redis on port 6379 |
| 43 | + |
| 44 | +### 4. Install Dependencies and Run Migrations |
| 45 | + |
| 46 | +```bash |
| 47 | +# Install dependencies |
| 48 | +cargo build |
| 49 | + |
| 50 | +# Set up the database (requires DATABASE_URL) |
| 51 | +export DATABASE_URL="postgresql://postgres:password@localhost:5432/container_engine" |
| 52 | + |
| 53 | +# Run database migrations |
| 54 | +cargo install sqlx-cli |
| 55 | +sqlx migrate run |
| 56 | +``` |
| 57 | + |
| 58 | +### 5. Generate SQLx Query Cache (Optional) |
| 59 | + |
| 60 | +To enable offline compilation without a database connection: |
| 61 | + |
| 62 | +```bash |
| 63 | +cargo sqlx prepare |
| 64 | +``` |
| 65 | + |
| 66 | +### 6. Run the Application |
| 67 | + |
| 68 | +```bash |
| 69 | +cargo run |
| 70 | +``` |
| 71 | + |
| 72 | +The API server will start on `http://localhost:3000`. |
| 73 | + |
| 74 | +### 7. Test the API |
| 75 | + |
| 76 | +Check if the server is running: |
| 77 | + |
| 78 | +```bash |
| 79 | +curl http://localhost:3000/health |
| 80 | +``` |
| 81 | + |
| 82 | +Expected response: |
| 83 | +```json |
| 84 | +{ |
| 85 | + "status": "healthy", |
| 86 | + "service": "container-engine", |
| 87 | + "version": "0.1.0" |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Development Workflow |
| 92 | + |
| 93 | +### Running with Docker Compose |
| 94 | + |
| 95 | +For a complete development environment: |
| 96 | + |
| 97 | +```bash |
| 98 | +# Start all services (PostgreSQL, Redis, and the API) |
| 99 | +docker-compose up --build |
| 100 | + |
| 101 | +# Or run in the background |
| 102 | +docker-compose up -d --build |
| 103 | +``` |
| 104 | + |
| 105 | +### Database Management |
| 106 | + |
| 107 | +#### Running Migrations |
| 108 | + |
| 109 | +```bash |
| 110 | +# Apply all pending migrations |
| 111 | +sqlx migrate run |
| 112 | + |
| 113 | +# Create a new migration |
| 114 | +sqlx migrate add <migration_name> |
| 115 | + |
| 116 | +# Revert the last migration |
| 117 | +sqlx migrate revert |
| 118 | +``` |
| 119 | + |
| 120 | +#### Resetting the Database |
| 121 | + |
| 122 | +```bash |
| 123 | +# Stop the database |
| 124 | +docker-compose stop postgres |
| 125 | + |
| 126 | +# Remove the database volume |
| 127 | +docker-compose rm -f postgres |
| 128 | +docker volume rm open-container-engine_postgres_data |
| 129 | + |
| 130 | +# Restart and run migrations |
| 131 | +docker-compose up postgres -d |
| 132 | +sqlx migrate run |
| 133 | +``` |
| 134 | + |
| 135 | +### Code Development |
| 136 | + |
| 137 | +#### Project Structure |
| 138 | + |
| 139 | +``` |
| 140 | +src/ |
| 141 | +├── main.rs # Application entry point |
| 142 | +├── config.rs # Configuration management |
| 143 | +├── database.rs # Database connection and setup |
| 144 | +├── error.rs # Error handling and types |
| 145 | +├── auth/ # Authentication and authorization |
| 146 | +│ ├── mod.rs |
| 147 | +│ ├── models.rs # User and API key models |
| 148 | +│ ├── jwt.rs # JWT token management |
| 149 | +│ └── middleware.rs # Authentication middleware |
| 150 | +├── user/ # User profile management |
| 151 | +│ ├── mod.rs |
| 152 | +│ └── models.rs |
| 153 | +├── deployment/ # Container deployment management |
| 154 | +│ ├── mod.rs |
| 155 | +│ └── models.rs |
| 156 | +└── handlers/ # HTTP request handlers |
| 157 | + ├── mod.rs |
| 158 | + ├── auth.rs # Authentication endpoints |
| 159 | + ├── user.rs # User management endpoints |
| 160 | + └── deployment.rs # Deployment endpoints |
| 161 | +``` |
| 162 | + |
| 163 | +#### Running Tests |
| 164 | + |
| 165 | +```bash |
| 166 | +# Run all tests |
| 167 | +cargo test |
| 168 | + |
| 169 | +# Run tests with output |
| 170 | +cargo test -- --nocapture |
| 171 | + |
| 172 | +# Run specific test module |
| 173 | +cargo test auth |
| 174 | +``` |
| 175 | + |
| 176 | +#### Code Formatting and Linting |
| 177 | + |
| 178 | +```bash |
| 179 | +# Format code |
| 180 | +cargo fmt |
| 181 | + |
| 182 | +# Check for linting issues |
| 183 | +cargo clippy |
| 184 | + |
| 185 | +# Check compilation without building |
| 186 | +cargo check |
| 187 | +``` |
| 188 | + |
| 189 | +## API Documentation |
| 190 | + |
| 191 | +The API provides the following main endpoints: |
| 192 | + |
| 193 | +### Authentication |
| 194 | +- `POST /v1/auth/register` - Register a new user |
| 195 | +- `POST /v1/auth/login` - Login and get tokens |
| 196 | +- `POST /v1/auth/refresh` - Refresh access token |
| 197 | +- `POST /v1/auth/logout` - Logout |
| 198 | + |
| 199 | +### API Key Management |
| 200 | +- `GET /v1/api-keys` - List API keys |
| 201 | +- `POST /v1/api-keys` - Create API key |
| 202 | +- `DELETE /v1/api-keys/{id}` - Revoke API key |
| 203 | + |
| 204 | +### User Profile |
| 205 | +- `GET /v1/user/profile` - Get user profile |
| 206 | +- `PUT /v1/user/profile` - Update profile |
| 207 | +- `PUT /v1/user/password` - Change password |
| 208 | + |
| 209 | +### Deployment Management |
| 210 | +- `GET /v1/deployments` - List deployments |
| 211 | +- `POST /v1/deployments` - Create deployment |
| 212 | +- `GET /v1/deployments/{id}` - Get deployment details |
| 213 | +- `PUT /v1/deployments/{id}` - Update deployment |
| 214 | +- `DELETE /v1/deployments/{id}` - Delete deployment |
| 215 | +- `PATCH /v1/deployments/{id}/scale` - Scale deployment |
| 216 | +- `POST /v1/deployments/{id}/start` - Start deployment |
| 217 | +- `POST /v1/deployments/{id}/stop` - Stop deployment |
| 218 | + |
| 219 | +For detailed API documentation, see [APIs.md](./APIs.md). |
| 220 | + |
| 221 | +## Environment Variables |
| 222 | + |
| 223 | +| Variable | Description | Default | |
| 224 | +|----------|-------------|---------| |
| 225 | +| `DATABASE_URL` | PostgreSQL connection string | `postgresql://postgres:password@localhost:5432/container_engine` | |
| 226 | +| `REDIS_URL` | Redis connection string | `redis://localhost:6379` | |
| 227 | +| `PORT` | Server port | `3000` | |
| 228 | +| `JWT_SECRET` | JWT signing secret | `your-super-secret-jwt-key-change-this-in-production` | |
| 229 | +| `JWT_EXPIRES_IN` | JWT expiration time (seconds) | `3600` | |
| 230 | +| `API_KEY_PREFIX` | API key prefix | `ce_api_` | |
| 231 | +| `KUBERNETES_NAMESPACE` | Kubernetes namespace | `container-engine` | |
| 232 | +| `DOMAIN_SUFFIX` | Domain suffix for deployments | `container-engine.app` | |
| 233 | +| `RUST_LOG` | Logging level | `container_engine=debug,tower_http=debug` | |
| 234 | + |
| 235 | +## Troubleshooting |
| 236 | + |
| 237 | +### Database Connection Issues |
| 238 | + |
| 239 | +1. Ensure PostgreSQL is running: |
| 240 | + ```bash |
| 241 | + docker-compose ps postgres |
| 242 | + ``` |
| 243 | + |
| 244 | +2. Check database logs: |
| 245 | + ```bash |
| 246 | + docker-compose logs postgres |
| 247 | + ``` |
| 248 | + |
| 249 | +3. Test connection manually: |
| 250 | + ```bash |
| 251 | + psql postgresql://postgres:password@localhost:5432/container_engine |
| 252 | + ``` |
| 253 | + |
| 254 | +### Redis Connection Issues |
| 255 | + |
| 256 | +1. Ensure Redis is running: |
| 257 | + ```bash |
| 258 | + docker-compose ps redis |
| 259 | + ``` |
| 260 | + |
| 261 | +2. Test Redis connection: |
| 262 | + ```bash |
| 263 | + redis-cli ping |
| 264 | + ``` |
| 265 | + |
| 266 | +### SQLx Compilation Issues |
| 267 | + |
| 268 | +If you encounter SQLx compilation errors: |
| 269 | + |
| 270 | +1. Ensure the database is running and accessible |
| 271 | +2. Set the `DATABASE_URL` environment variable |
| 272 | +3. Run `cargo sqlx prepare` to generate the query cache |
| 273 | +4. Use `SQLX_OFFLINE=true` for builds without database access |
| 274 | + |
| 275 | +### Port Conflicts |
| 276 | + |
| 277 | +If ports 3000, 5432, or 6379 are already in use: |
| 278 | + |
| 279 | +1. Update the ports in `docker-compose.yml` |
| 280 | +2. Update corresponding environment variables |
| 281 | +3. Restart the services |
| 282 | + |
| 283 | +## Production Deployment |
| 284 | + |
| 285 | +For production deployment: |
| 286 | + |
| 287 | +1. Use secure values for `JWT_SECRET` |
| 288 | +2. Configure proper database credentials |
| 289 | +3. Set up SSL/TLS certificates |
| 290 | +4. Configure proper logging levels |
| 291 | +5. Set up monitoring and alerting |
| 292 | +6. Use a reverse proxy (nginx, traefik) |
| 293 | +7. Configure Kubernetes cluster access |
| 294 | + |
| 295 | +## Contributing |
| 296 | + |
| 297 | +1. Fork the repository |
| 298 | +2. Create a feature branch |
| 299 | +3. Make your changes |
| 300 | +4. Run tests and ensure they pass |
| 301 | +5. Submit a pull request |
| 302 | + |
| 303 | +For more details, see the main [README.md](./README.md). |
0 commit comments