Skip to content

feat: Make TrustedHostMiddleware configurable via ALLOWED_HOSTS env var #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ docker run -d --name gitingest -p 8000:8000 gitingest
```
The application will be available at `http://localhost:8000`

### 🌐 Environment Configuration

You can configure the application using the following environment variables:

- **`ALLOWED_HOSTS`**: Specify allowed hostnames for the application. Default: `"gitingest.com,*.gitingest.com,gitdigest.dev,localhost"`.

Example:

```bash
ALLOWED_HOSTS="gitingest.local,localhost"
```

Ensure these variables are set before running the application or deploying it via Docker.

## ✔️ Contributions are welcome!
Create a pull request or open an Issue about anything you'd like to see in gitingest

Expand Down
13 changes: 12 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@

app.mount("/static", StaticFiles(directory="static"), name="static")
app.add_middleware(Analytics, api_key=os.getenv('API_ANALYTICS_KEY'))
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["gitingest.com", "*.gitingest.com", "gitdigest.dev", "localhost"])

# Define the default allowed hosts
default_allowed_hosts = ["gitingest.com", "*.gitingest.com", "gitdigest.dev", "localhost"]

# Fetch allowed hosts from the environment variable or use the default
allowed_hosts = os.getenv("ALLOWED_HOSTS")
if allowed_hosts:
allowed_hosts = allowed_hosts.split(",")
else:
allowed_hosts = default_allowed_hosts

app.add_middleware(TrustedHostMiddleware, allowed_hosts=allowed_hosts)
templates = Jinja2Templates(directory="templates")

@app.get("/health")
Expand Down