git-asgi 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
git_asgi/__init__.py ADDED
File without changes
git_asgi/app.py ADDED
@@ -0,0 +1,191 @@
1
+ from http import HTTPStatus
2
+ from os import environ
3
+ from sys import exit, stderr
4
+
5
+ from asgiref.typing import (
6
+ ASGIReceiveCallable,
7
+ ASGISendCallable,
8
+ HTTPScope,
9
+ LifespanScope,
10
+ Scope,
11
+ )
12
+
13
+ from git_asgi.middleware import GitMiddleware
14
+
15
+
16
+ class Application:
17
+ """
18
+ ASGI 3 application.
19
+
20
+ Supports the `lifespan` scope by answering the `lifespan.startup` and
21
+ `lifespan.shutdown` events directly.
22
+
23
+ Supports the `http` scope by responding with an HTTP status `403 FORBIDDEN`.
24
+
25
+ Other scope types are unsupported and raise an exception, as specified.
26
+
27
+ - https://asgi.readthedocs.io/en/latest/specs/main.html#protocol-specifications
28
+ """
29
+
30
+ async def __call__(
31
+ self,
32
+ scope: Scope,
33
+ receive: ASGIReceiveCallable,
34
+ send: ASGISendCallable,
35
+ ) -> None:
36
+ """
37
+ Dispatches incoming ASGI `lifespan` and `http` scopes.
38
+
39
+ Arguments:
40
+ scope: the ASGI scope.
41
+ receive: the ASGI receive callable.
42
+ send: the ASGI send callable.
43
+
44
+ Raises:
45
+ NotImplementedError: when receiving an unsupported scope.
46
+ """
47
+ match scope["type"]:
48
+ case "lifespan":
49
+ await self.lifespan(scope, receive, send)
50
+ case "http":
51
+ await self.http(scope, receive, send)
52
+ case unsupported:
53
+ raise NotImplementedError(
54
+ f"unsupported message type '{unsupported}'"
55
+ )
56
+
57
+ async def lifespan(
58
+ self,
59
+ scope: LifespanScope,
60
+ receive: ASGIReceiveCallable,
61
+ send: ASGISendCallable,
62
+ ) -> None:
63
+ """
64
+ Handles the ASGI `lifespan` scope.
65
+
66
+ Immediately answers a `lifespan.startup` event with a
67
+ `lifespan.startup.complete` event.
68
+
69
+ Immediately answers a `lifespan.shutdown` event with a
70
+ `lifespan.shutdown.complete` event.
71
+
72
+ Arguments:
73
+ scope: the ASGI scope.
74
+ receive: the ASGI receive callable.
75
+ send: the ASGI send callable.
76
+ """
77
+ event = await receive()
78
+
79
+ match event["type"]:
80
+ case "lifespan.startup":
81
+ await send(
82
+ {
83
+ "type": "lifespan.startup.complete",
84
+ }
85
+ )
86
+ case "lifespan.shutdown":
87
+ await send(
88
+ {
89
+ "type": "lifespan.shutdown.complete",
90
+ }
91
+ )
92
+
93
+ async def http(
94
+ self,
95
+ scope: HTTPScope,
96
+ receive: ASGIReceiveCallable,
97
+ send: ASGISendCallable,
98
+ ) -> None:
99
+ """
100
+ Handles the ASGI `http` scope.
101
+
102
+ Immediately answers HTTP requests with an HTTP `403 FORBIDDEN` response.
103
+
104
+ Arguments:
105
+ scope: the ASGI scope.
106
+ receive: the ASGI receive callable.
107
+ send: the ASGI send callable.
108
+ """
109
+ await send(
110
+ {
111
+ "type": "http.response.start",
112
+ "status": HTTPStatus.FORBIDDEN,
113
+ "headers": (),
114
+ "trailers": False,
115
+ }
116
+ )
117
+ await send(
118
+ {
119
+ "type": "http.response.body",
120
+ "body": b"",
121
+ "more_body": False,
122
+ }
123
+ )
124
+
125
+
126
+ def _bool(value: str | bool) -> bool:
127
+ """
128
+ Handle a boolean environment value or its default.
129
+
130
+ Arguments:
131
+ value: the environment value.
132
+
133
+ Returns:
134
+ the resulting boolean.
135
+ """
136
+ return bool(int(value))
137
+
138
+
139
+ def _int(value: str | None) -> int | None:
140
+ """
141
+ Handle an integer environment value or its default.
142
+
143
+ Arguments:
144
+ value: the environment value.
145
+
146
+ Returns:
147
+ the resulting integer or `None`.
148
+ """
149
+ return int(value) if value is not None else value
150
+
151
+
152
+ # instantiate the terminal ASGI app
153
+ end = Application()
154
+
155
+ try:
156
+ # read environment variables
157
+ base = environ.get("GIT_ASGI_BASE", ".")
158
+ logger = environ.get("GIT_ASGI_LOGGER")
159
+ level = environ.get("GIT_ASGI_LEVEL")
160
+ fmt = environ.get("GIT_ASGI_FORMAT")
161
+ smart = _bool(environ.get("GIT_ASGI_SMART", True))
162
+ dumb = _bool(environ.get("GIT_ASGI_DUMB", False))
163
+ upload = _bool(environ.get("GIT_ASGI_UPLOAD", True))
164
+ receive = _bool(environ.get("GIT_ASGI_RECEIVE", True))
165
+ archive = _bool(environ.get("GIT_ASGI_ARCHIVE", False))
166
+ request = _int(environ.get("GIT_ASGI_REQUEST"))
167
+ overload = _int(environ.get("GIT_ASGI_OVERLOAD"))
168
+ chunk = _int(environ.get("GIT_ASGI_CHUNK"))
169
+
170
+ # wrap the terminal ASGI app in the Git middleware
171
+ app = GitMiddleware(
172
+ end,
173
+ base=base,
174
+ logger=logger,
175
+ level=level,
176
+ fmt=fmt,
177
+ smart=smart,
178
+ dumb=dumb,
179
+ upload=upload,
180
+ receive=receive,
181
+ archive=archive,
182
+ request=request,
183
+ overload=overload,
184
+ chunk=chunk,
185
+ )
186
+ except Exception as exc:
187
+ # print to STDERR
188
+ print(f"Error: {str(exc)}", file=stderr)
189
+
190
+ # exit with non-zero return code
191
+ exit(1)