-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.6.5: fix: handle multiple middlewares and document it, add example
- Loading branch information
1 parent
86466cd
commit 4d8daf9
Showing
22 changed files
with
29,232 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM ghcr.io/another-ai/embedbase:latest | ||
COPY ./middlewares/processing_time/processing_time.py /app/middlewares/processing_time.py | ||
COPY ./middlewares/enrich/enrich.py /app/middlewares/enrich.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## simple-react | ||
|
||
This is a simple React app that is using **Embedbase**. | ||
|
||
Add this to your `config.yaml`: | ||
```yaml | ||
# ... | ||
middlewares: | ||
- middlewares.processing_time | ||
# ... | ||
``` | ||
|
||
```bash | ||
docker-compose up | ||
``` | ||
|
||
Start the front-end from another terminal: | ||
|
||
```bash | ||
npm i | ||
npm start | ||
``` | ||
|
||
|
||
https://user-images.githubusercontent.com/25003283/216080514-9d40f912-7201-419e-80e3-11ad4fd52ac6.mov | ||
|
15 changes: 15 additions & 0 deletions
15
examples/simple-react-custom-middleware/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: '3.7' | ||
|
||
services: | ||
embedbase: | ||
build: . | ||
ports: | ||
- "8000:8000" | ||
environment: | ||
- PYTHONUNBUFFERED=1 | ||
- PORT=8000 | ||
volumes: | ||
- ./config.yaml:/app/config.yaml | ||
- ./middlewares/processing_time/processing_time.py:/app/middlewares/processing_time.py | ||
- ./middlewares/enrich/enrich.py:/app/middlewares/enrich.py | ||
|
14 changes: 14 additions & 0 deletions
14
examples/simple-react-custom-middleware/middlewares/enrich/enrich.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from typing import Tuple | ||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
import requests | ||
class CustomHeaderMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next) -> Tuple[str, str]: | ||
response = await call_next(request) | ||
blood_types = requests.get("https://random-data-api.com/api/v2/blood_types").json() | ||
response.headers["X-Enrich"] = str(blood_types) | ||
print("my middleware is running") | ||
print("enrich middleware did this:", blood_types) | ||
print("enrich middleware ran after processing_time middleware, look what it did") | ||
print("response.headers['X-Process-Time']", response.headers["X-Process-Time"]) | ||
return response |
13 changes: 13 additions & 0 deletions
13
examples/simple-react-custom-middleware/middlewares/processing_time/processing_time.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import time | ||
from typing import Tuple | ||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
class CustomHeaderMiddleware(BaseHTTPMiddleware): | ||
async def processing_time(request: Request, call_next) -> Tuple[str, str]: | ||
start_time = time.time() | ||
response = await call_next(request) | ||
process_time = time.time() - start_time | ||
response.headers["X-Process-Time"] = str(process_time) | ||
print("my middleware is running") | ||
print("processing_time middleware did this:", str(process_time)) | ||
return response |
Oops, something went wrong.