xref: /unit/test/python/lifespan/empty/asgi.py (revision 1873)
11628Szelenkov@nginx.comimport os
21628Szelenkov@nginx.com
31628Szelenkov@nginx.com
4*1873So.canty@f5.comasync def handler(prefix, scope, receive, send):
51628Szelenkov@nginx.com    if scope['type'] == 'lifespan':
6*1873So.canty@f5.com        with open(prefix + 'version', 'w+') as f:
71628Szelenkov@nginx.com            f.write(
81628Szelenkov@nginx.com                scope['asgi']['version'] + ' ' + scope['asgi']['spec_version']
91628Szelenkov@nginx.com            )
101628Szelenkov@nginx.com        while True:
111628Szelenkov@nginx.com            message = await receive()
121628Szelenkov@nginx.com            if message['type'] == 'lifespan.startup':
13*1873So.canty@f5.com                os.remove(prefix + 'startup')
141628Szelenkov@nginx.com                await send({'type': 'lifespan.startup.complete'})
151628Szelenkov@nginx.com            elif message['type'] == 'lifespan.shutdown':
16*1873So.canty@f5.com                os.remove(prefix + 'shutdown')
171628Szelenkov@nginx.com                await send({'type': 'lifespan.shutdown.complete'})
181628Szelenkov@nginx.com                return
191628Szelenkov@nginx.com
201628Szelenkov@nginx.com    if scope['type'] == 'http':
211628Szelenkov@nginx.com        await send(
221628Szelenkov@nginx.com            {
231628Szelenkov@nginx.com                'type': 'http.response.start',
241628Szelenkov@nginx.com                'status': 204,
251628Szelenkov@nginx.com                'headers': [(b'content-length', b'0'),],
261628Szelenkov@nginx.com            }
271628Szelenkov@nginx.com        )
28*1873So.canty@f5.com
29*1873So.canty@f5.com
30*1873So.canty@f5.comasync def application(scope, receive, send):
31*1873So.canty@f5.com    return await handler('', scope, receive, send)
32*1873So.canty@f5.com
33*1873So.canty@f5.com
34*1873So.canty@f5.comasync def application2(scope, receive, send):
35*1873So.canty@f5.com    return await handler('app2_', scope, receive, send)
36