-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
61 lines (46 loc) · 1.67 KB
/
server.py
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
"""
Runs a server that can be used when running in a Kubernetes or Docker Compose environment.
Not needed outside of this.
"""
import logging
import socket
import sys
import traceback
import uvicorn
from fastapi import BackgroundTasks, FastAPI, HTTPException
from fastapi.responses import JSONResponse
from oedisi.types.common import BrokerConfig, HeathCheck, ServerReply
from test_federate import run_simulator
app = FastAPI()
@app.get("/")
async def read_root():
"""
Root endpoint that returns the health check information of the server.
Returns:
JSONResponse: The health check information in JSON format.
"""
hostname = socket.gethostname()
host_ip = socket.gethostbyname(hostname)
response = HeathCheck(hostname=hostname, host_ip=host_ip).dict()
return JSONResponse(response, 200)
@app.post("/run/")
async def run_model(broker_config: BrokerConfig, background_tasks: BackgroundTasks):
"""
Endpoint to run the simulator with the provided broker configuration.
Args:
broker_config (BrokerConfig): The broker configuration.
background_tasks (BackgroundTasks): Background tasks to be executed.
Returns:
JSONResponse: The response indicating the task has been successfully added.
"""
try:
logging.info(broker_config)
background_tasks.add_task(run_simulator, broker_config)
response = ServerReply(detail="Task successfully added.").dict()
return JSONResponse(response, 200)
except Exception as _:
err = traceback.format_exc()
HTTPException(500, str(err))
if __name__ == "__main__":
port = int(sys.argv[2])
uvicorn.run(app, host="0.0.0.0", port=port)