-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from ScilifelabDataCentre/fastapi
Fastapi
- Loading branch information
Showing
16 changed files
with
1,071 additions
and
1,084 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
FROM python:3.10-alpine AS base | ||
FROM python:3.11-alpine AS base | ||
|
||
RUN apk update && apk upgrade | ||
RUN apk update && apk upgrade && apk add build-base | ||
|
||
LABEL org.opencontainers.image.description "Menu aggregator - backend (Fastapi instead of Flask)" | ||
|
||
RUN mkdir /code | ||
COPY ./backend/requirements.txt /code/ | ||
COPY ./backend/restaurants.json /code/ | ||
WORKDIR /code | ||
|
||
RUN pip install -r requirements.txt | ||
RUN pip install gunicorn | ||
|
||
COPY ./backend/*.py /code/ | ||
|
||
ENV UVICORN_HOST=0.0.0.0 | ||
ENV UVICORN_PORT=8000 | ||
|
||
CMD ["uvicorn", "app:app"] | ||
|
||
|
||
FROM base as dev | ||
|
||
CMD ["flask", "run", "-h", "0.0.0.0", "--port", "5000"] | ||
ENV VERSION=dev | ||
ENV UVICORN_RELOAD=1 | ||
|
||
|
||
FROM base as production | ||
|
||
ARG version | ||
ENV VERSION=$version | ||
|
||
ENV GUNICORN_CMD_ARGS "--bind=0.0.0.0:8000 --workers=2 --thread=4 --worker-class=gthread --forwarded-allow-ips='*' --access-logfile -" | ||
|
||
CMD ["gunicorn","flask_app:app"] | ||
|
||
EXPOSE 8000 |
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 |
---|---|---|
@@ -1,19 +1,9 @@ | ||
worker_processes 1; ## Default: 1 | ||
|
||
events { | ||
worker_connections 4096; | ||
} | ||
|
||
http { | ||
include mime.types; | ||
default_type application/octet-stream; | ||
server { | ||
listen 80; | ||
server_name _; | ||
index index.html; | ||
root /usr/share/nginx/html; | ||
location / { | ||
try_files $uri $uri/ /index.html; | ||
} | ||
server { | ||
listen 8080; | ||
server_name _; | ||
index index.html; | ||
root /usr/share/nginx/html; | ||
location / { | ||
try_files $uri $uri/ /index.html; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,53 +1,35 @@ | ||
worker_processes 1; ## Default: 1 | ||
|
||
events { | ||
worker_connections 4096; | ||
} | ||
|
||
http { | ||
include mime.types; | ||
default_type application/octet-stream; | ||
|
||
sendfile on; | ||
#tcp_nopush on; | ||
|
||
keepalive_timeout 65; | ||
|
||
#gzip on; | ||
|
||
server { | ||
listen 80; | ||
server_name localhost; | ||
location ~ ^/(api) { | ||
proxy_pass http://backend:5000; | ||
|
||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "upgrade"; | ||
proxy_set_header Host $http_host; | ||
|
||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto http; | ||
proxy_set_header X-Nginx-Proxy true; | ||
|
||
proxy_redirect off; | ||
} | ||
server { | ||
listen 8080; | ||
server_name localhost; | ||
location ~ ^/(api) { | ||
proxy_pass http://backend:8000; | ||
|
||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "upgrade"; | ||
proxy_set_header Host $http_host; | ||
|
||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto http; | ||
proxy_set_header X-Nginx-Proxy true; | ||
|
||
proxy_redirect off; | ||
} | ||
|
||
location / { | ||
proxy_pass http://frontend:8080; | ||
location / { | ||
proxy_pass http://frontend:8080; | ||
|
||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "upgrade"; | ||
proxy_set_header Host $http_host; | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection "upgrade"; | ||
proxy_set_header Host $http_host; | ||
|
||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto http; | ||
proxy_set_header X-Nginx-Proxy true; | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto http; | ||
proxy_set_header X-Nginx-Proxy true; | ||
|
||
proxy_redirect off; | ||
} | ||
proxy_redirect off; | ||
} | ||
} |
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,70 @@ | ||
import os | ||
|
||
from fastapi import FastAPI, Request | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from fastapi_cache import FastAPICache | ||
from fastapi_cache.backends.inmemory import InMemoryBackend | ||
from fastapi_cache.decorator import cache | ||
from starlette_context import middleware, plugins | ||
|
||
import utils | ||
|
||
|
||
app = FastAPI(openapi_url="/api/openapi.json", | ||
docs_url="/api/docs", | ||
redoc_url="/api/redoc") | ||
|
||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["GET"], | ||
allow_headers=["*"], | ||
) | ||
|
||
if os.environ.get("REVERSE_PROXY", False): | ||
app.add_middleware( | ||
middleware.ContextMiddleware, | ||
plugins=( | ||
plugins.ForwardedForPlugin(), | ||
), | ||
) | ||
|
||
|
||
@app.get("/api") | ||
@cache(expire=360000) | ||
async def list_entities(): | ||
return {"documentation_swagger": app.url_path_for("swagger_ui_html"), | ||
"documentation_redoc": app.url_path_for("redoc_html"), | ||
"openapi": app.url_path_for("openapi"),} | ||
|
||
|
||
@app.get("/api/restaurant") | ||
@cache(expire=360000) | ||
async def list_restaurants(): | ||
return { | ||
"restaurants": utils.list_restaurants(), | ||
} | ||
|
||
|
||
@app.get("/api/restaurant/{name}") | ||
@cache(expire=10800) | ||
async def get_restaurant(name): | ||
print("not cached") | ||
data = dict(utils.get_restaurant(name)) | ||
if not data: | ||
flask.abort(status=404) | ||
data["menu"] = [{"dish": entry} for entry in data["menu"]] | ||
return {"restaurant": data,} | ||
|
||
|
||
@app.get("/api/version") | ||
@cache(expire=360000) | ||
async def get_backend_version(): | ||
ver = os.environ.get("VERSION", "") | ||
return {"version": ver,} | ||
|
||
|
||
@app.on_event("startup") | ||
async def startup(): | ||
FastAPICache.init(InMemoryBackend()) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.