-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapplication.py
More file actions
140 lines (125 loc) · 5.37 KB
/
application.py
File metadata and controls
140 lines (125 loc) · 5.37 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
from datetime import datetime
from fastapi.middleware.cors import CORSMiddleware
from libs.base.application_base import Application_Base
from libs.base.typed_fastapi import TypedFastAPI
from libs.repositories.file_repository import FileRepository
from libs.repositories.process_repository import ProcessRepository
from libs.repositories.process_status_repository import ProcessStatusRepository
from libs.sas.storage import AsyncStorageBlobHelper, AsyncStorageQueueHelper
from libs.services.implementations import (
ConsoleLoggerService,
HttpClientService,
InMemoryDataService,
)
from libs.services.interfaces import IDataService, IHttpService, ILoggerService
from libs.services.process_services import ProcessService
# Import from the new locations (main branch)
from routers import router_debug, router_files, router_process
from routers.http_probes import router as http_probes
class Application(Application_Base):
"""
Application class that extends the base application class.
This class can be used to implement specific application logic.
"""
app: TypedFastAPI
start_time = datetime.now()
def __init__(self):
super().__init__(env_file_path=os.path.join(os.path.dirname(__file__), ".env"))
def initialize(self):
"""
Initialize the application.
This method can be overridden by subclasses to perform any necessary setup.
"""
############################################################
# Initialize the FastAPI application with typed version
############################################################
self.app = TypedFastAPI(
redirect_slashes=False, title="FastAPI Application", version="1.0.0"
)
######################################################################
# Set the application context to the FastAPI app with proper typing
######################################################################
self.app.set_app_context(self.application_context)
self.app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
self.app.include_router(http_probes)
self._register_dependencies()
self._config_routers()
# self._initialize_database()
def _config_routers(self):
"""
Configure routers for the FastAPI application.
This method can be overridden by subclasses to add custom routers.
"""
############################################################
# Add your routers here
############################################################
routers = [
http_probes,
router_process.router,
router_files.router,
router_debug.router,
]
for router in routers:
self.app.include_router(router)
def _register_dependencies(self):
"""
Add dependencies to the FastAPI application.
This method can be overridden by subclasses to add custom dependencies.
"""
# Register router business logics
(
# router_process_business logic
self.application_context.add_transient(
ProcessService, lambda: ProcessService(self.app)
)
.add_transient(
AsyncStorageBlobHelper,
lambda: AsyncStorageBlobHelper(
account_name=self.application_context.configuration.storage_account_name,
),
)
# Repository is thread safe.
.add_scoped(
ProcessStatusRepository,
lambda: ProcessStatusRepository(
account_url=self.application_context.configuration.cosmos_db_account_url,
database_name=self.application_context.configuration.cosmos_db_database_name,
container_name=self.application_context.configuration.cosmos_db_process_log_container,
),
)
# Repository is thread safe.
.add_async_singleton(
ProcessRepository,
lambda: ProcessRepository(
account_url=self.application_context.configuration.cosmos_db_account_url,
database_name=self.application_context.configuration.cosmos_db_database_name,
container_name="processes",
),
)
.add_singleton(
FileRepository,
lambda: FileRepository(
account_url=self.application_context.configuration.cosmos_db_account_url,
database_name=self.application_context.configuration.cosmos_db_database_name,
container_name="files",
),
)
.add_transient(
AsyncStorageQueueHelper,
lambda: AsyncStorageQueueHelper(
account_name=self.application_context.configuration.storage_account_name,
),
)
.add_singleton(ILoggerService, ConsoleLoggerService)
.add_transient(IHttpService, HttpClientService)
.add_singleton(IDataService, lambda: InMemoryDataService())
)
def run(self, host: str = "0.0.0.0", port: int = 8000, reload: bool = True):
pass