-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_ojp2.py
More file actions
62 lines (52 loc) · 1.6 KB
/
server_ojp2.py
File metadata and controls
62 lines (52 loc) · 1.6 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
#!/usr/bin/env python3
import logging
from fastapi import FastAPI, Request, Response
import app_logging
from api.ojp2 import FareServiceOjp2
from configuration import (
HTTP_HOST,
HTTP_PORT,
HTTPS,
SSL_CERTFILE,
SSL_KEYFILE,
HTTP_SLUG,
)
app_logging.setup_logging()
logger = logging.getLogger(__name__)
app = FastAPI(title="ojp-fare API for OJP2")
fare_service = FareServiceOjp2()
@app.get("/health/liveness", tags=["Health"])
async def liveness(fastapi_req: Request):
"""
Implements liveness probe.
"""
logger.debug("Received liveness probe request: " + str(fastapi_req))
return Response("Liveness: OK", media_type="text/plain; charset=utf-8")
# implements basic readiness probe
@app.get("/health/readiness", tags=["Health"])
async def readiness(fastapi_req: Request):
"""
Implements readiness probe.
"""
logger.debug("Received readiness probe request: " + str(fastapi_req))
return Response("Readiness: OK", media_type="text/plain; charset=utf-8")
@app.post("/" + HTTP_SLUG, tags=["Open Journey Planner"])
async def post_request(fastapi_req: Request) ->Response:
"""
Handles OjpFare requests: for ojp2 only.
"""
body = await fastapi_req.body()
logger.debug("Received request: " + str(body))
return fare_service.handle_request(body)
if __name__ == "__main__":
import uvicorn
if HTTPS:
uvicorn.run(
app,
host=HTTP_HOST,
port=HTTP_PORT,
ssl_keyfile=SSL_KEYFILE,
ssl_certfile=SSL_CERTFILE,
)
else:
uvicorn.run(app, host=HTTP_HOST, port=HTTP_PORT)