34 lines
848 B
Python
34 lines
848 B
Python
|
|
from fastapi import FastAPI
|
||
|
|
|
||
|
|
from models import MsgPayload
|
||
|
|
|
||
|
|
app = FastAPI()
|
||
|
|
messages_list: dict[int, MsgPayload] = {}
|
||
|
|
|
||
|
|
|
||
|
|
@app.get("/")
|
||
|
|
def root() -> dict[str, str]:
|
||
|
|
return {"message": "Hello"}
|
||
|
|
|
||
|
|
|
||
|
|
# About page route
|
||
|
|
@app.get("/about")
|
||
|
|
def about() -> dict[str, str]:
|
||
|
|
return {"message": "This is the about page."}
|
||
|
|
|
||
|
|
|
||
|
|
# Route to add a message
|
||
|
|
@app.post("/messages/{msg_name}/")
|
||
|
|
def add_msg(msg_name: str) -> dict[str, MsgPayload]:
|
||
|
|
# Generate an ID for the item based on the highest ID in the messages_list
|
||
|
|
msg_id = max(messages_list.keys()) + 1 if messages_list else 0
|
||
|
|
messages_list[msg_id] = MsgPayload(msg_id=msg_id, msg_name=msg_name)
|
||
|
|
|
||
|
|
return {"message": messages_list[msg_id]}
|
||
|
|
|
||
|
|
|
||
|
|
# Route to list all messages
|
||
|
|
@app.get("/messages")
|
||
|
|
def message_items() -> dict[str, dict[int, MsgPayload]]:
|
||
|
|
return {"messages:": messages_list}
|