Skip to content

Commit 23bfe8e

Browse files
committed
Add .env.example and implement CORS configuration
1 parent 8b7de2d commit 23bfe8e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

.env.example

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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=

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export DEVIL_API_KEY="your-secret"
8080
export LOG_LEVEL="INFO"
8181
export AUTH_FAIL_THRESHOLD="5"
8282
export 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
130132
Create a local .env file to load automatically:
131133
```
132134
DEVIL_API_KEY=your-secret
133135
LOG_LEVEL=INFO
134136
AUTH_FAIL_THRESHOLD=5
135137
AUTH_BLOCK_SECONDS=300
138+
CORS_ORIGINS=http://localhost:3000,https://yourapp.com
136139
```
137140
138141
## Contributing

app/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
from importlib.metadata import PackageNotFoundError
66
from importlib.metadata import version
77

8+
from dotenv import load_dotenv
89
from fastapi import Depends
910
from fastapi import FastAPI
1011
from fastapi import Request
12+
from fastapi.middleware.cors import CORSMiddleware
1113
from fastapi.responses import JSONResponse
1214

1315
from app.api.endpoints import dns
@@ -27,6 +29,9 @@
2729
from app.services.socket_client import DevilSocketError
2830
from app.services.socket_client import DevilSocketProtocolError
2931

32+
# Load environment variables from .env file
33+
load_dotenv()
34+
3035
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
3136
logging.basicConfig(level=getattr(logging, log_level, logging.INFO))
3237

@@ -38,6 +43,21 @@
3843

3944
app = 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
4363
protected_dependency = [Depends(verify_api_key)]

0 commit comments

Comments
 (0)