|
166 | 166 | api_limiter = None |
167 | 167 | RateLimitExceededError = Exception |
168 | 168 |
|
| 169 | +# ============================================================================= |
| 170 | +# ENVIRONMENT VALIDATION |
| 171 | +# ============================================================================= |
| 172 | +def validate_environment(): |
| 173 | + """Validate required environment variables on startup.""" |
| 174 | + required_vars = { |
| 175 | + "GROQ_API_KEY": "AI content generation", |
| 176 | + "LINKEDIN_CLIENT_ID": "LinkedIn OAuth", |
| 177 | + "LINKEDIN_CLIENT_SECRET": "LinkedIn OAuth", |
| 178 | + } |
| 179 | + |
| 180 | + optional_but_recommended = { |
| 181 | + "GITHUB_CLIENT_ID": "GitHub OAuth (private repos)", |
| 182 | + "GITHUB_CLIENT_SECRET": "GitHub OAuth (private repos)", |
| 183 | + "UNSPLASH_ACCESS_KEY": "Image generation", |
| 184 | + } |
| 185 | + |
| 186 | + missing_required = [] |
| 187 | + missing_optional = [] |
| 188 | + |
| 189 | + for var, purpose in required_vars.items(): |
| 190 | + if not os.getenv(var): |
| 191 | + missing_required.append(f" - {var}: {purpose}") |
| 192 | + |
| 193 | + for var, purpose in optional_but_recommended.items(): |
| 194 | + if not os.getenv(var): |
| 195 | + missing_optional.append(f" - {var}: {purpose}") |
| 196 | + |
| 197 | + if missing_required: |
| 198 | + print("\n⚠️ WARNING: Missing REQUIRED environment variables:") |
| 199 | + for msg in missing_required: |
| 200 | + print(msg) |
| 201 | + print(" Some features will not work until these are set.\n") |
| 202 | + |
| 203 | + if missing_optional: |
| 204 | + print("\n💡 TIP: Missing OPTIONAL environment variables:") |
| 205 | + for msg in missing_optional: |
| 206 | + print(msg) |
| 207 | + print(" These are recommended for full functionality.\n") |
| 208 | + |
| 209 | +# Run validation on import |
| 210 | +validate_environment() |
| 211 | + |
169 | 212 | app = FastAPI( |
170 | 213 | title="LinkedIn Post Bot API", |
171 | 214 | description=""" |
|
207 | 250 | init_settings_db() |
208 | 251 |
|
209 | 252 | # Add CORS middleware |
| 253 | +# CORS_ORIGINS env var should be comma-separated list of allowed origins |
| 254 | +# Example: CORS_ORIGINS=http://localhost:3000,https://your-app.vercel.app |
| 255 | +CORS_ORIGINS = os.getenv("CORS_ORIGINS", "http://localhost:3000").split(",") |
| 256 | +CORS_ORIGINS = [origin.strip() for origin in CORS_ORIGINS if origin.strip()] |
| 257 | + |
210 | 258 | app.add_middleware( |
211 | 259 | CORSMiddleware, |
212 | | - allow_origins=["http://localhost:3000"], |
| 260 | + allow_origins=CORS_ORIGINS, |
213 | 261 | allow_credentials=True, |
214 | 262 | allow_methods=["*"], |
215 | 263 | allow_headers=["*"], |
|
0 commit comments