Skip to content

Commit

Permalink
enable azure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Apr 25, 2024
1 parent ce5cdac commit d1d168c
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ EXPOSE 8000
# up the FastAPI workers.
# NOTE: If you run this Dockerfile via "docker compose up", CMD will be overridden
# by docker-compose.yaml
CMD ["gunicorn", "app.main:sio_app", \
CMD ["gunicorn", "app.main:main_app", \
"--bind", "0.0.0.0:8000", \
"--access-logfile", "-", \
"--workers", "1", \
Expand Down
1 change: 1 addition & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Settings(BaseSettings):
enable_examples: bool = True
enable_excel_online: bool = True
enable_htmx: bool = True
enable_socketio: bool = True
entraid_client_id: Optional[str] = None
entraid_tenant_id: Optional[str] = None
# Set to False if you have users from external organizations
Expand Down
17 changes: 11 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
# App
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)

# Routers
app.include_router(xlwings_router)
app.include_router(macros_router)
app.include_router(taskpane_router)

# CORS: Office Scripts and custom functions in Excel on the web require CORS
# Using app.add_middleware won't add the CORS headers if you handle the root "Exception"
# in an exception handler (it would require a specific exception type).
Expand All @@ -36,7 +31,17 @@
)

# Socket.io
sio_app = socketio.ASGIApp(socketio_router.sio, cors_app)
if settings.enable_socketio:
sio_app = socketio.ASGIApp(socketio_router.sio, cors_app)
main_app = sio_app
else:
main_app = cors_app


# Routers
app.include_router(xlwings_router)
app.include_router(macros_router)
app.include_router(taskpane_router)


# Security headers
Expand Down
2 changes: 1 addition & 1 deletion app/static/js/socketio-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ try {
},
});
} catch (error) {
console.error(error);
console.log("Didn't load socket.io: ", error);
globalThis.socket = null;
}
4 changes: 3 additions & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
{% endif %}
<script src="/static/js/utils/officejs-history-fix-part2.js"></script>
{# Socket.io (must come before xlwings.js) #}
<script src="/static/vendor/socket.io/client-dist/socket.io.min.js"></script>
{% if settings.enable_socketio %}
<script src="/static/vendor/socket.io/client-dist/socket.io.min.js"></script>
{% endif %}
{# xlwings.js (must come before custom-function-code) #}
<script src="/static/vendor/xlwings/xlwings.min.js"></script>
{# Examples #}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
app:
build: .
command: >
gunicorn app.main:sio_app
gunicorn app.main:main_app
--bind 0.0.0.0:8000
--access-logfile -
--workers 1
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
app:
build: .
command: >
uvicorn app.main:sio_app
uvicorn app.main:main_app
--host 0.0.0.0 --port 8000
--ssl-keyfile /project/certs/localhost+2-key.pem --ssl-certfile /project/certs/localhost+2.pem
--reload
Expand Down
21 changes: 21 additions & 0 deletions function_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Azure Functions
Note: Azure Functions don't support streaming functions/socket.io.
The other files required for Azure Functions are:
- host.json
- local.settings.json
The function is always called http_app_func, see:
https://github.com/Azure-Samples/fastapi-on-azure-functions/issues/31
For app logs, in Azure portal go to:
Function App > My Function App. Than, under `http_app_func`, click on `Invocations and more`.
"""

import azure.functions as func

from app.main import main_app

app = func.AsgiFunctionApp(app=main_app, http_auth_level=func.AuthLevel.ANONYMOUS)
22 changes: 22 additions & 0 deletions host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[2.*, 3.0.0)"
},
"extensions":
{
"http":
{
"routePrefix": ""
}
}
}
8 changes: 8 additions & 0 deletions local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
}
}
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
azure-functions; sys_platform != 'win32'
cachetools
fastapi
gunicorn; sys_platform != 'win32'
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ anyio==4.3.0
# httpx
# starlette
# watchfiles
azure-functions==1.19.0
bidict==0.23.1
# via python-socketio
cachetools==5.3.3
Expand Down
2 changes: 1 addition & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

if __name__ == "__main__":
uvicorn.run(
"app.main:sio_app",
"app.main:main_app",
host="127.0.0.1",
port=8000,
reload=True,
Expand Down

0 comments on commit d1d168c

Please sign in to comment.