|
| 1 | +# Nginx Configuration API |
| 2 | + |
| 3 | +A Orcinus API for generating Nginx configuration files with support for templates, validation, and advanced features. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🔧 Pre-defined templates for common setups |
| 8 | +- ✅ Configuration validation |
| 9 | +- 🔒 Advanced SSL/Security configurations |
| 10 | +- 🚀 Microservices support |
| 11 | +- 📝 Detailed documentation |
| 12 | +- 🔄 WebSocket support |
| 13 | +- 💾 Caching strategies |
| 14 | +- ⚡ Rate limiting |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +1. Install Deno: |
| 19 | +```bash |
| 20 | + |
| 21 | +``` |
| 22 | + |
| 23 | +2. Run the server: |
| 24 | +```bash |
| 25 | + |
| 26 | +``` |
| 27 | + |
| 28 | +The server will start on http://localhost:3000 |
| 29 | + |
| 30 | +## API Endpoints |
| 31 | + |
| 32 | +### GET / |
| 33 | +Returns available templates and documentation. |
| 34 | + |
| 35 | +### POST / |
| 36 | +Generates Nginx configuration based on template or custom configuration. |
| 37 | + |
| 38 | +## Using Templates |
| 39 | + |
| 40 | +### Available Templates |
| 41 | + |
| 42 | +1. **Static Website** |
| 43 | +```json |
| 44 | +{ |
| 45 | + "template": "static", |
| 46 | + "templateParams": { |
| 47 | + "domain": "example.com", |
| 48 | + "rootPath": "/var/www/example", |
| 49 | + "sslEnabled": true |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +2. **Single Page Application (SPA)** |
| 55 | +```json |
| 56 | +{ |
| 57 | + "template": "spa", |
| 58 | + "templateParams": { |
| 59 | + "domain": "myapp.com", |
| 60 | + "rootPath": "/var/www/myapp", |
| 61 | + "apiUrl": "http://api.myapp.com", |
| 62 | + "sslEnabled": true |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +3. **WordPress** |
| 68 | +```json |
| 69 | +{ |
| 70 | + "template": "wordpress", |
| 71 | + "templateParams": { |
| 72 | + "domain": "blog.com", |
| 73 | + "rootPath": "/var/www/wordpress", |
| 74 | + "phpVersion": "8.2", |
| 75 | + "sslEnabled": true |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +4. **Microservices** |
| 81 | +```json |
| 82 | +{ |
| 83 | + "template": "microservices", |
| 84 | + "templateParams": { |
| 85 | + "domain": "api.myapp.com", |
| 86 | + "services": [ |
| 87 | + { |
| 88 | + "name": "auth", |
| 89 | + "port": 3001, |
| 90 | + "path": "/auth", |
| 91 | + "methods": ["POST", "GET"], |
| 92 | + "corsEnabled": true |
| 93 | + }, |
| 94 | + { |
| 95 | + "name": "users", |
| 96 | + "port": 3002, |
| 97 | + "path": "/users", |
| 98 | + "methods": ["GET", "POST", "PUT", "DELETE"], |
| 99 | + "corsEnabled": true |
| 100 | + } |
| 101 | + ], |
| 102 | + "sslEnabled": true |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Custom Configuration |
| 108 | + |
| 109 | +You can also provide a custom configuration: |
| 110 | + |
| 111 | +```json |
| 112 | +{ |
| 113 | + "config": { |
| 114 | + "domain": "custom.com", |
| 115 | + "serverName": "custom.com www.custom.com", |
| 116 | + "port": 443, |
| 117 | + "ssl": { |
| 118 | + "certificate": "/etc/ssl/custom.com.crt", |
| 119 | + "certificateKey": "/etc/ssl/custom.com.key", |
| 120 | + "protocols": ["TLSv1.2", "TLSv1.3"], |
| 121 | + "forceRedirect": true |
| 122 | + }, |
| 123 | + "security": { |
| 124 | + "xFrameOptions": "DENY", |
| 125 | + "xContentTypeOptions": true, |
| 126 | + "xXSSProtection": true |
| 127 | + }, |
| 128 | + "locations": [ |
| 129 | + { |
| 130 | + "path": "/", |
| 131 | + "root": "/var/www/custom", |
| 132 | + "index": "index.html" |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Advanced Features |
| 140 | + |
| 141 | +### SSL Configuration |
| 142 | +```json |
| 143 | +{ |
| 144 | + "ssl": { |
| 145 | + "certificate": "/path/to/cert.pem", |
| 146 | + "certificateKey": "/path/to/key.pem", |
| 147 | + "protocols": ["TLSv1.2", "TLSv1.3"], |
| 148 | + "ciphers": ["ECDHE-ECDSA-AES128-GCM-SHA256"], |
| 149 | + "preferServerCiphers": true, |
| 150 | + "dhParam": "/path/to/dhparam.pem", |
| 151 | + "ocspStapling": true, |
| 152 | + "sessionTimeout": "1d", |
| 153 | + "hsts": { |
| 154 | + "enabled": true, |
| 155 | + "maxAge": 31536000, |
| 156 | + "includeSubdomains": true, |
| 157 | + "preload": true |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### Security Headers |
| 164 | +```json |
| 165 | +{ |
| 166 | + "security": { |
| 167 | + "xFrameOptions": "DENY", |
| 168 | + "xContentTypeOptions": true, |
| 169 | + "xXSSProtection": true, |
| 170 | + "referrerPolicy": "strict-origin-when-cross-origin", |
| 171 | + "contentSecurityPolicy": [ |
| 172 | + "default-src 'self'", |
| 173 | + "img-src 'self' data: https:", |
| 174 | + "script-src 'self' 'unsafe-inline'" |
| 175 | + ] |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +### WebSocket Support |
| 181 | +```json |
| 182 | +{ |
| 183 | + "websocket": { |
| 184 | + "enabled": true, |
| 185 | + "timeout": 3600, |
| 186 | + "keepaliveTimeout": 60 |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +### Caching Configuration |
| 192 | +```json |
| 193 | +{ |
| 194 | + "cache": { |
| 195 | + "enabled": true, |
| 196 | + "validTime": "30m", |
| 197 | + "keys": ["$host", "$request_uri"], |
| 198 | + "useStale": ["error", "timeout", "updating"], |
| 199 | + "minUses": 2, |
| 200 | + "bypass": ["$http_cache_control"], |
| 201 | + "methods": ["GET", "HEAD"] |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +### Rate Limiting |
| 207 | +```json |
| 208 | +{ |
| 209 | + "rateLimit": { |
| 210 | + "rate": "10r/s", |
| 211 | + "burstSize": 20, |
| 212 | + "nodelay": true |
| 213 | + } |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +### CORS Configuration |
| 218 | +```json |
| 219 | +{ |
| 220 | + "cors": { |
| 221 | + "enabled": true, |
| 222 | + "origins": ["https://example.com"], |
| 223 | + "methods": ["GET", "POST", "OPTIONS"], |
| 224 | + "headers": ["Authorization", "Content-Type"], |
| 225 | + "credentials": true |
| 226 | + } |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +## Project Structure |
| 231 | + |
| 232 | +- `main.ts` - Main server file |
| 233 | +- `types.ts` - TypeScript interfaces |
| 234 | +- `validator.ts` - Configuration validation |
| 235 | +- `generator.ts` - Nginx config generation |
| 236 | +- `templates.ts` - Pre-defined templates |
| 237 | + |
| 238 | +## Validation |
| 239 | + |
| 240 | +The API validates: |
| 241 | +- Domain format |
| 242 | +- Port ranges |
| 243 | +- SSL configuration |
| 244 | +- Security headers |
| 245 | +- Rate limit format |
| 246 | +- URL formats |
| 247 | +- Required fields |
| 248 | + |
| 249 | +## Response Format |
| 250 | + |
| 251 | +Success Response: |
| 252 | +```json |
| 253 | +{ |
| 254 | + "success": true, |
| 255 | + "config": "... nginx configuration ...", |
| 256 | + "fileName": "example.com.conf", |
| 257 | + "template": "static" |
| 258 | +} |
| 259 | +``` |
| 260 | + |
| 261 | +Error Response: |
| 262 | +```json |
| 263 | +{ |
| 264 | + "error": "Configuration validation failed", |
| 265 | + "validationErrors": [ |
| 266 | + { |
| 267 | + "field": "domain", |
| 268 | + "message": "Invalid domain format" |
| 269 | + } |
| 270 | + ] |
| 271 | +} |
| 272 | +``` |
| 273 | + |
| 274 | +## Examples |
| 275 | + |
| 276 | +### 1. Basic Static Website |
| 277 | +```bash |
| 278 | +curl -X POST -H "Content-Type: application/json" -d '{ |
| 279 | + "template": "static", |
| 280 | + "templateParams": { |
| 281 | + "domain": "example.com", |
| 282 | + "rootPath": "/var/www/example", |
| 283 | + "sslEnabled": true |
| 284 | + } |
| 285 | +}' http://localhost:3000 |
| 286 | +``` |
| 287 | + |
| 288 | +### 2. WordPress Site |
| 289 | +```bash |
| 290 | +curl -X POST -H "Content-Type: application/json" -d '{ |
| 291 | + "template": "wordpress", |
| 292 | + "templateParams": { |
| 293 | + "domain": "blog.com", |
| 294 | + "rootPath": "/var/www/wordpress", |
| 295 | + "phpVersion": "8.2", |
| 296 | + "sslEnabled": true |
| 297 | + } |
| 298 | +}' http://localhost:3000 |
| 299 | +``` |
| 300 | + |
| 301 | +### 3. Microservices API |
| 302 | +```bash |
| 303 | +curl -X POST -H "Content-Type: application/json" -d '{ |
| 304 | + "template": "microservices", |
| 305 | + "templateParams": { |
| 306 | + "domain": "api.myapp.com", |
| 307 | + "services": [ |
| 308 | + { |
| 309 | + "name": "auth", |
| 310 | + "port": 3001, |
| 311 | + "path": "/auth", |
| 312 | + "methods": ["POST", "GET"], |
| 313 | + "corsEnabled": true |
| 314 | + } |
| 315 | + ], |
| 316 | + "sslEnabled": true |
| 317 | + } |
| 318 | +}' http://localhost:3000 |
| 319 | +``` |
| 320 | + |
| 321 | +## Error Handling |
| 322 | + |
| 323 | +The API provides detailed error messages for: |
| 324 | +- Invalid configuration |
| 325 | +- Missing required fields |
| 326 | +- Invalid template names |
| 327 | +- Invalid parameter values |
| 328 | +- Server errors |
| 329 | + |
| 330 | +## Contributing |
| 331 | + |
| 332 | +1. Fork the repository |
| 333 | +2. Create a feature branch |
| 334 | +3. Commit your changes |
| 335 | +4. Push to the branch |
| 336 | +5. Create a Pull Request |
| 337 | + |
| 338 | +## License |
| 339 | + |
| 340 | +MIT License |
0 commit comments