13
13
from app .middleware import GzipRequestMiddleware
14
14
15
15
from app .websockets import register_ws_routes , redis_listener
16
- from app .config import get_config
16
+ from app .config import config
17
17
from app .utils .sentry import initialize_sentry
18
18
19
19
@contextlib .asynccontextmanager
@@ -24,34 +24,32 @@ async def lifespan(app: FastAPI):
24
24
# optionally do cleanup here
25
25
task .cancel ()
26
26
27
- config = get_config ()
28
-
29
27
app = FastAPI (lifespan = lifespan )
30
28
app .add_middleware (GZipMiddleware )
31
29
app .add_middleware (GzipRequestMiddleware )
32
30
33
31
register_ws_routes (app )
34
32
35
- if config .HASSIO_MODE :
36
- if config .HASSIO_MODE == "public" :
33
+ if config .HASSIO_RUN_MODE :
34
+ if config .HASSIO_RUN_MODE == "public" :
35
+ app .include_router (api_public , prefix = "/api" )
36
+ elif config .HASSIO_RUN_MODE == "ingress" :
37
37
app .include_router (api_public , prefix = "/api" )
38
- elif config .HASSIO_MODE == "ingress" :
39
- app .include_router (api_public , prefix = config .base_path + "/api" )
40
- app .include_router (api_no_auth , prefix = config .base_path + "/api" )
41
- app .include_router (api_with_auth , prefix = config .base_path + "/api" )
38
+ app .include_router (api_no_auth , prefix = "/api" )
39
+ app .include_router (api_with_auth , prefix = "/api" )
42
40
else :
43
- raise ValueError ("Invalid HASSIO_MODE " )
41
+ raise ValueError ("Invalid HASSIO_RUN_MODE " )
44
42
else :
45
43
app .include_router (api_public , prefix = "/api" )
46
44
app .include_router (api_no_auth , prefix = "/api" )
47
45
app .include_router (api_with_auth , prefix = "/api" , dependencies = [Depends (get_current_user )])
48
46
49
- # Serve HTML and static files in all cases except for public HASSIO_MODE
50
- serve_html = config .HASSIO_MODE != "public"
47
+ # Serve HTML and static files in all cases except for public HASSIO_RUN_MODE
48
+ serve_html = config .HASSIO_RUN_MODE != "public"
51
49
if serve_html :
52
- app .mount (config . base_path + "/assets" , StaticFiles (directory = "../frontend/dist/assets" ), name = "assets" )
53
- app .mount (config . base_path + "/img" , StaticFiles (directory = "../frontend/dist/img" ), name = "img" )
54
- app .mount (config . base_path + "/static" , StaticFiles (directory = "../frontend/dist/static" ), name = "static" )
50
+ app .mount ("/assets" , StaticFiles (directory = "../frontend/dist/assets" ), name = "assets" )
51
+ app .mount ("/img" , StaticFiles (directory = "../frontend/dist/img" ), name = "img" )
52
+ app .mount ("/static" , StaticFiles (directory = "../frontend/dist/static" ), name = "static" )
55
53
56
54
# Public config for the frontend
57
55
frameos_app_config = {}
@@ -64,25 +62,20 @@ async def lifespan(app: FastAPI):
64
62
else :
65
63
raise
66
64
67
- if config .HASSIO_MODE :
68
- frameos_app_config ["HASSIO_MODE " ] = config .HASSIO_MODE
65
+ if config .HASSIO_RUN_MODE :
66
+ frameos_app_config ["HASSIO_RUN_MODE " ] = config .HASSIO_RUN_MODE
69
67
if config .base_path :
70
68
frameos_app_config ["base_path" ] = config .base_path
71
- index_html = index_html . replace ( '<base href="/">' , f'<base href=" { config . base_path } /">' )
69
+
72
70
index_html = index_html .replace ('<head>' , f'<head><script>window.FRAMEOS_APP_CONFIG={ json .dumps (frameos_app_config )} </script>' )
73
71
74
- @app .get (config . base_path + "/" )
72
+ @app .get ("/" )
75
73
async def read_index ():
76
74
return HTMLResponse (index_html )
77
75
78
- if config .base_path :
79
- @app .get (config .base_path )
80
- async def read_index ():
81
- return HTMLResponse (index_html )
82
-
83
76
@app .exception_handler (StarletteHTTPException )
84
77
async def custom_404_handler (request : Request , exc : StarletteHTTPException ):
85
- if os .environ .get ("TEST" ) == "1" or exc .status_code != 404 or ( config . base_path and not request . url . path . startswith ( config . base_path )) :
78
+ if os .environ .get ("TEST" ) == "1" or exc .status_code != 404 :
86
79
return JSONResponse (
87
80
status_code = exc .status_code ,
88
81
content = {"detail" : exc .detail or f"Error { exc .status_code } " }
@@ -100,8 +93,8 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
100
93
101
94
if __name__ == '__main__' :
102
95
# run migrations
103
- if get_config () .DEBUG :
104
- database_url = get_config () .DATABASE_URL
96
+ if config .DEBUG :
97
+ database_url = config .DATABASE_URL
105
98
if database_url .startswith ("sqlite:///../db/" ):
106
99
os .makedirs ('../db' , exist_ok = True )
107
100
# start server
0 commit comments