Initial iol

This commit is contained in:
2023-07-01 19:59:14 -03:00
parent f067d97095
commit f0ab7c90dd
12 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,49 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.9.10 as watchdog
FROM --platform=${TARGETPLATFORM:-linux/amd64} python:3.11-alpine
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
ARG ADDITIONAL_PACKAGE
RUN apk --no-cache add musl-dev gcc make ${ADDITIONAL_PACKAGE}
# Add non root user
RUN addgroup -S app && adduser app -S -G app
RUN chown app /home/app
USER app
ENV PATH=$PATH:/home/app/.local/bin
WORKDIR /home/app/
COPY requirements.txt .
USER root
RUN pip install -r requirements.txt
USER app
COPY index.py .
RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt .
RUN pip install --user -r requirements.txt
WORKDIR /home/app/
USER root
# remove build dependencies
RUN apk del musl-dev gcc make
COPY function function
RUN chown -R app:app ./
USER app
ENV fprocess="uvicorn index:app --workers 1 --host 0.0.0.0 --port 8000"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:8000"
HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]

View File

@ -0,0 +1,51 @@
# author: Justin Guese, 11.3.22, justin@datafortress.cloud
from fastapi import HTTPException
from fastapi import APIRouter
from pydantic import BaseModel
from typing import Dict, List
from os import environ
import glob
# reads in secrets to environment variables, such that they can be
# easily used with environ["SECRET_NAME"]
def readSecretToEnv(secretpath):
secretname = secretpath.split('/')[-1]
with open(secretpath, "r") as f:
environ[secretname] = f.read()
for secret in glob.glob("/var/openfaas/secrets/*"):
readSecretToEnv(secret)
router = APIRouter()
# just as an example
class User(BaseModel):
id: int
name: str
age: int
colleagues: List[str]
class ResponseModel(BaseModel):
data: Dict
# user: User
# otherStuff: str
@router.post("/", response_model = ResponseModel, tags=["Main Routes"])
def handle(request: Dict):
"""handle a request to the function
Args:
req (dict): request body
"""
try:
res = ResponseModel(data=request)
except Exception as e:
raise HTTPException(status_code=500, detail=str(repr(e)))
return res
@router.get("/", response_model = ResponseModel, tags=["Main Routes"])
def get():
return ResponseModel(data={"message": "Hello from OpenFAAS!"})
# again just as an example, delete this if not required
@router.get("/users/{user_id}", response_model = User, tags=["Main Routes"])
def getUser(user_id: int):
return User(id = user_id, name="Exampleuser", age=20, colleagues=["Colleague 1", "Colleague 2"])

View File

@ -0,0 +1,19 @@
# author: Justin Guese, 11.3.22, justin@datafortress.cloud
from os import environ
import glob
from fastapi import FastAPI, Request
from fastapi.openapi.docs import get_swagger_ui_html
from function.handler import router
app = FastAPI()
app.include_router(router)
# required to render /docs path
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html(req: Request):
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + app.openapi_url
return get_swagger_ui_html(
openapi_url=openapi_url,
title="API",
)

View File

@ -0,0 +1,2 @@
fastapi
uvicorn

View File

@ -0,0 +1,2 @@
language: python3-fastapi
fprocess: uvicorn index:app --workers 1 --host 0.0.0.0 --port 8000