File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed
Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ # Environment variables for devil-api
2+ # API Key for authentication
3+ DEVIL_API_KEY = your-api-key-here
4+
5+ # Logging level (DEBUG, INFO, WARNING, ERROR)
6+ LOG_LEVEL = INFO
7+
8+ # Authentication settings
9+ AUTH_FAIL_THRESHOLD = 5
10+ AUTH_BLOCK_SECONDS = 300
11+
12+ # CORS configuration - comma-separated list of allowed origins
13+ # Examples:
14+ # CORS_ORIGINS=http://localhost:3000,http://localhost:8080
15+ # CORS_ORIGINS=https://yourdomain.com,https://app.yourdomain.com
16+ # Leave empty to disable CORS
17+ CORS_ORIGINS =
Original file line number Diff line number Diff line change @@ -80,6 +80,7 @@ export DEVIL_API_KEY="your-secret"
8080export LOG_LEVEL="INFO"
8181export AUTH_FAIL_THRESHOLD="5"
8282export AUTH_BLOCK_SECONDS="300"
83+ export CORS_ORIGINS=http://localhost:3000,https://yourapp.com
8384```
8485
8586## Usage
@@ -126,13 +127,15 @@ Notes:
126127- LOG_LEVEL (optional): e.g., INFO, DEBUG
127128- AUTH_FAIL_THRESHOLD (optional, default 5): number of failed attempts before blocking
128129- AUTH_BLOCK_SECONDS (optional, default 300): block duration in seconds
130+ - CORS_ORIGINS (optional): comma-separated list of allowed origins for CORS. Leave empty to disable CORS
129131
130132Create a local .env file to load automatically:
131133```
132134DEVIL_API_KEY=your-secret
133135LOG_LEVEL=INFO
134136AUTH_FAIL_THRESHOLD=5
135137AUTH_BLOCK_SECONDS=300
138+ CORS_ORIGINS=http://localhost:3000,https://yourapp.com
136139```
137140
138141## Contributing
Original file line number Diff line number Diff line change 55from importlib .metadata import PackageNotFoundError
66from importlib .metadata import version
77
8+ from dotenv import load_dotenv
89from fastapi import Depends
910from fastapi import FastAPI
1011from fastapi import Request
12+ from fastapi .middleware .cors import CORSMiddleware
1113from fastapi .responses import JSONResponse
1214
1315from app .api .endpoints import dns
2729from app .services .socket_client import DevilSocketError
2830from app .services .socket_client import DevilSocketProtocolError
2931
32+ # Load environment variables from .env file
33+ load_dotenv ()
34+
3035log_level = os .getenv ("LOG_LEVEL" , "INFO" ).upper ()
3136logging .basicConfig (level = getattr (logging , log_level , logging .INFO ))
3237
3843
3944app = FastAPI (title = "devil API" , version = __version__ )
4045
46+ # CORS configuration
47+ cors_origins_str = os .getenv ("CORS_ORIGINS" , "" )
48+ cors_origins = [
49+ origin .strip () for origin in cors_origins_str .split ("," ) if origin .strip ()
50+ ]
51+
52+ if cors_origins :
53+ app .add_middleware (
54+ CORSMiddleware ,
55+ allow_origins = cors_origins ,
56+ allow_credentials = True ,
57+ allow_methods = ["*" ],
58+ allow_headers = ["*" ],
59+ )
60+
4161
4262# Include routers
4363protected_dependency = [Depends (verify_api_key )]
You can’t perform that action at this time.
0 commit comments