-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
66 lines (50 loc) · 1.77 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Module to serve a FastAPI application with static files Uvicorn.
The module sets up a FastAPI application and mounts a directory
containing static files using the Starlette `StaticFiles` class.
The root URL ("/") is handled to redirect to the static files.
The application can be started using the Uvicorn server by running
the command: uvicorn main:app --reload
Usage:
python main.py
Dependencies:
- uvicorn
- fastapi
- starlette
"""
import uvicorn
from fastapi import HTTPException
from fastapi.staticfiles import StaticFiles
from starlette.responses import RedirectResponse
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
from backend import app
# http://192.168.239.143:8000/static/
# http://127.0.0.1:8000/static/
# To start the server run the following command: uvicorn main:app --reload
# (main is the name of the file and app is the name of the FastAPI object)
app.mount(
"/static/", StaticFiles(directory="./frontend/dist", html=True), name="static"
)
@app.get("/")
async def read_index():
"""
Handles the root URL ("/") to redirect to the static files.
This function redirects the root URL to the static files served
from the "/static/" URL.
Returns:
RedirectResponse: Redirects to the static files.
Raises:
HTTPException:
The status code is set to HTTP 500 Internal Server Error,
and the detail contains the error message.
"""
try:
return RedirectResponse(url="static")
except Exception as error:
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error"
) from error
if __name__ == "__main__":
HOST = "127.0.0.1"
PORT = 8000
uvicorn.run("main:app", port=PORT, host=HOST, reload=True)