-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.py
More file actions
133 lines (124 loc) · 4.62 KB
/
Copy pathmodule.py
File metadata and controls
133 lines (124 loc) · 4.62 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
import typing as t
from ellar.auth import AppIdentitySchemes, IdentityAuthenticationService
from ellar.auth.session import SessionServiceNullStrategy, SessionStrategy
from ellar.common import (
IApplicationShutdown,
IApplicationStartup,
IExceptionMiddlewareService,
IExecutionContext,
IExecutionContextFactory,
IGuardsConsumer,
IHostContext,
IHostContextFactory,
IHTTPConnectionContextFactory,
IIdentitySchemes,
IInterceptorsConsumer,
IWebSocketContextFactory,
Module,
)
from ellar.common.interfaces import ITemplateRenderingService
from ellar.common.logging import logger
from ellar.core import ModuleSetup, TemplateRenderingService
from ellar.core.conf import Config
from ellar.core.exceptions.service import ExceptionMiddlewareService
from ellar.core.execution_context.factory import (
ExecutionContextFactory,
HostContextFactory,
HTTPConnectionContextFactory,
WebSocketContextFactory,
)
from ellar.core.guards import GuardConsumer
from ellar.core.interceptors import EllarInterceptorConsumer
from ellar.core.services import Reflector, reflector
from ellar.di import EllarInjector, ProviderConfig, request_scope
from ellar.di.injector.tree_manager import ModuleTreeManager
from ellar.events import app_context_started, app_context_teardown
if t.TYPE_CHECKING: # pragma: no cover
from ellar.app import App
def _raise_unavailable_exception() -> None:
raise Exception("Service is only available during request")
def get_core_module(app_module: t.Union[t.Type, t.Any], config: Config) -> t.Type:
@Module(
modules=[app_module],
providers=[
ProviderConfig(
Config,
use_value=config,
export=True,
),
ProviderConfig(
ITemplateRenderingService,
use_class=TemplateRenderingService,
export=True,
),
ProviderConfig(
ModuleTreeManager,
use_value=lambda: config.MODULE_TREE_MANAGER_CLASS(
ModuleSetup(EllarCoreModule)
),
export=True,
),
ProviderConfig(
IExceptionMiddlewareService,
use_class=ExceptionMiddlewareService,
export=True,
),
ProviderConfig(
IExecutionContextFactory, use_class=ExecutionContextFactory, export=True
),
ProviderConfig(
IHostContextFactory, use_class=HostContextFactory, export=True
),
ProviderConfig(
IHostContext,
use_value=_raise_unavailable_exception,
scope=request_scope,
export=True,
),
ProviderConfig(
IExecutionContext,
use_value=_raise_unavailable_exception,
scope=request_scope,
export=True,
),
ProviderConfig(
IHTTPConnectionContextFactory,
use_class=HTTPConnectionContextFactory,
export=True,
),
ProviderConfig(
IWebSocketContextFactory, use_class=WebSocketContextFactory, export=True
),
ProviderConfig(Reflector, use_value=reflector, export=True),
ProviderConfig(
IInterceptorsConsumer, use_value=EllarInterceptorConsumer, export=True
),
ProviderConfig(IGuardsConsumer, use_class=GuardConsumer, export=True),
ProviderConfig(IIdentitySchemes, use_class=AppIdentitySchemes, export=True),
ProviderConfig(
IdentityAuthenticationService,
use_class=IdentityAuthenticationService,
export=True,
),
ProviderConfig(
SessionStrategy, use_class=SessionServiceNullStrategy, export=True
),
],
)
class EllarCoreModule(IApplicationStartup, IApplicationShutdown):
def __init__(self, _config: Config, injector: EllarInjector) -> None:
self.config = _config
self.injector = injector
async def on_startup(self, app: "App") -> None:
try:
await app_context_started.run(app=app)
except Exception as ex: # pragma: no cover
logger.exception(ex)
raise ex
async def on_shutdown(self) -> None:
try:
await app_context_teardown.run()
except Exception as ex: # pragma: no cover
logger.exception(ex)
raise ex
return EllarCoreModule