Skip to content

Commit 8552b92

Browse files
Add the option to the API to configure CORS. (#267)
* Add the option to the API to configure CORS. * Update api/README.md Co-authored-by: Lukas Phaf <lukas.phaf@knmi.nl> * Change middleware order and remove arguments with defaults. * Update readme with headers environment variable. * Ensure empty list. * Reorder according to https://fastapi.tiangolo.com/tutorial/middleware/#multiple-middleware-execution-order. --------- Co-authored-by: Lukas Phaf <lukas.phaf@knmi.nl>
1 parent 1270a32 commit 8552b92

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

api/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# E-SOH API
22

3-
## Enviorment variable
4-
These enviorment variables should be set when you are starting the container or be set in the enviorment where you are running the application.
5-
### DSHOST
3+
## Environment variables
64

7-
IP address to datastore
5+
Environment variables that can be used to configure the container or the environment where you are running the application.
86

9-
### DSPORT
10-
11-
Port the datastore is available on.
12-
13-
### FORWARDED_ALLOW_IPS
14-
15-
Environment variable used to set the `forwarded-allow-ips` in gunicorn. If this API is set behind a proxy, `FORWARDED_ALLOW_IPS` should be set to the proxy IP. Setting this to `*` is possible, but should only be set if you have ensured the API is only reachable from the proxy, and not directly from the internet. If not using docker compose this have to be passed to docker using the `-e` argument.
7+
| Name | Description | Mandatory |
8+
|---------------------|-------------------------------------------------------------------------------------------------------------------------------|-----------|
9+
| DSHOST | Address to the datastore ||
10+
| DSPORT | Port where the datastore is available. ||
11+
| FORWARDED_ALLOW_IPS | Environment variable used to set the `forwarded-allow-ips` in gunicorn. If this API is set behind a proxy, `FORWARDED_ALLOW_IPS` should be set to the proxy IP. Setting this to `*` is possible, but should only be set if you have ensured the API is only reachable from the proxy, and not directly from the internet. If not using docker compose this have to be passed to docker using the `-e` argument. ||
12+
| GUNICORN_CMD_ARGS | Command-line arguments for configuring Gunicorn, a Python WSGI HTTP Server. ||
13+
| CORS_ORIGINS | Indicates whether the response can be shared with requesting code from the given origins (passed as a comma separated string) ||
14+
| CORS_HEADERS | Indicates what headers should be supported with cross-origin requests (passed as a comma separated string) ||
1615

1716
## Prerequisites of running locally
1817

1918
### QUDT
19+
2020
Move the `std_unit_names.json`to the api folder with
21+
2122
```bash
2223
just copy-units
2324
```
2425

2526
Generate the file needed for QUDT dictionary by running
27+
2628
```bash
2729
python generate_qudt_units.py
2830
```

api/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from routers import edr
1818
from routers import feature
1919
from utilities import create_url_from_request
20+
from fastapi.middleware.cors import CORSMiddleware
2021

2122

2223
all_collections = collections_metadata.keys()
@@ -40,7 +41,15 @@ def setup_logging():
4041
root_path=os.getenv("FASTAPI_ROOT_PATH", ""),
4142
**openapi_metadata,
4243
)
44+
4345
app.add_middleware(BrotliMiddleware)
46+
if (cors_origins := os.getenv("CORS_ORIGINS", None)) is not None:
47+
cors_headers = os.getenv("CORS_HEADERS", None)
48+
app.add_middleware(
49+
CORSMiddleware,
50+
allow_origins=cors_origins.split(","),
51+
allow_headers=[] if cors_headers is None else cors_headers.split(","),
52+
)
4453
add_metrics(app)
4554

4655

0 commit comments

Comments
 (0)