Skip to content

Commit 0969820

Browse files
committed
Add core function structure based on fastapi
1 parent 071df83 commit 0969820

File tree

14 files changed

+143
-28
lines changed

14 files changed

+143
-28
lines changed
File renamed without changes.

code/function/api/v1/__init__.py

Whitespace-only changes.

code/function/api/v1/api_v1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import APIRouter
2+
from function.api.v1.endpoints import heartbeat, sample
3+
4+
api_v1_router = APIRouter()
5+
api_v1_router.include_router(
6+
sample.router, prefix="/landingZone", tags=["sample"]
7+
)
8+
api_v1_router.include_router(heartbeat.router, prefix="/health", tags=["health"])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import Any
2+
3+
from fastapi import APIRouter
4+
from function.models.heartbeat import HearbeatResult
5+
from function.utils import setup_logging
6+
7+
logger = setup_logging(__name__)
8+
9+
router = APIRouter()
10+
11+
12+
@router.get("/heartbeat", response_model=HearbeatResult, name="heartbeat")
13+
async def get_hearbeat() -> Any:
14+
logger.info("Received Heartbeat Request")
15+
return HearbeatResult(isAlive=True)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any
2+
3+
from fastapi import APIRouter
4+
from function.models.sample import (
5+
SampleRequest,
6+
SampleResponse,
7+
)
8+
from function.utils import setup_logging
9+
10+
logger = setup_logging(__name__)
11+
12+
router = APIRouter()
13+
14+
15+
@router.post("/create", response_model=SampleResponse, name="create")
16+
async def post_predict(
17+
data: SampleRequest,
18+
) -> SampleResponse:
19+
logger.info(f"Received request: {data}")
20+
return SampleResponse(output=f"Hello ${data.input}")

code/function/core/__init__.py

Whitespace-only changes.

code/function/core/config.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
from pydantic import BaseSettings
4+
5+
6+
class Settings(BaseSettings):
7+
PROJECT_NAME: str = "FunctionSample"
8+
SERVER_NAME: str = "FunctionSample"
9+
APP_VERSION: str = "v0.0.1"
10+
API_V1_STR: str = "/v1"
11+
LOGGING_LEVEL: int = logging.INFO
12+
DEBUG: bool = False
13+
14+
15+
settings = Settings()

code/function/core/messages.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pydantic import BaseSettings
2+
3+
4+
class Messages(BaseSettings):
5+
# Base messages
6+
NO_API_KEY = "No API key provided."
7+
AUTH_REQ = "Authentication required."
8+
HTTP_500_DETAIL = "Internal server error."
9+
10+
# Templates
11+
NO_VALID_PAYLOAD = "{} is not a valid payload."
12+
13+
14+
messages = Messages()

code/function/function_app.py

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import logging
2-
31
import azure.functions as func
2+
from fastapi import FastAPI
3+
from function.api.v1.api_v1 import api_v1_router
4+
from function.core.config import settings
5+
6+
7+
def get_app() -> FastAPI:
8+
app = FastAPI(
9+
title=settings.PROJECT_NAME,
10+
version=settings.APP_VERSION,
11+
openapi_url="/openapi.json",
12+
debug=settings.DEBUG,
13+
)
14+
app.include_router(api_v1_router, prefix=settings.API_V1_STR)
15+
return app
16+
17+
18+
fastapi_app = get_app()
19+
20+
21+
@fastapi_app.on_event("startup")
22+
async def startup_event():
23+
pass
24+
25+
26+
@fastapi_app.on_event("shutdown")
27+
async def shutdown_event():
28+
pass
29+
430

5-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
6-
7-
8-
@app.route(route="HttpTrigger")
9-
def HttpTrigger(req: func.HttpRequest) -> func.HttpResponse:
10-
logging.info("Python HTTP trigger function processed a request.")
11-
12-
name = req.params.get("name")
13-
if not name:
14-
try:
15-
req_body = req.get_json()
16-
except ValueError:
17-
pass
18-
else:
19-
name = req_body.get("name")
20-
21-
if name:
22-
return func.HttpResponse(
23-
f"Hello, {name}. This HTTP triggered function executed successfully."
24-
)
25-
else:
26-
return func.HttpResponse(
27-
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
28-
status_code=200,
29-
)
31+
app = func.AsgiFunctionApp(
32+
app=fastapi_app,
33+
http_auth_level=func.AuthLevel.FUNCTION,
34+
)

code/function/models/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)