chatsbt/app.py

47 lines
1.7 KiB
Python
Raw Permalink Normal View History

from starlette.applications import Starlette
2025-07-31 18:30:25 -04:00
from starlette.routing import Route, Mount
from starlette.staticfiles import StaticFiles
from starlette.responses import FileResponse
2025-07-31 15:59:19 -04:00
from controllers import create_chat, post_message, chat_stream, history, get_models
from starlette.middleware import Middleware
from starlette.middleware.cors import CORSMiddleware
2025-07-31 18:30:25 -04:00
import os
middleware = [
Middleware(
CORSMiddleware,
2025-07-29 23:42:15 -04:00
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
]
2025-07-31 18:30:25 -04:00
async def serve_frontend(request):
"""Serve the frontend index.html file"""
return FileResponse(os.path.join("frontend", "dist", "index.html"))
2025-08-06 00:40:42 -04:00
async def serve_chat(request):
"""Serve the chat.html file for specific chat routes"""
return FileResponse(os.path.join("frontend", "dist", "chat.html"))
routes = [
2025-07-31 18:30:25 -04:00
Route("/", serve_frontend, methods=["GET"]),
2025-08-06 00:40:42 -04:00
Route("/chats/{chat_id:str}", serve_chat, methods=["GET"]),
2025-08-02 00:57:39 -04:00
Route("/api/models", get_models, methods=["GET"]),
Route("/api/chats", create_chat, methods=["POST"]),
Route("/api/chats/{chat_id:str}", history, methods=["GET"]),
Route("/api/chats/{chat_id:str}/messages", post_message, methods=["POST"]),
Route("/api/chats/{chat_id:str}/stream", chat_stream, methods=["GET"]),
2025-07-31 18:30:25 -04:00
Mount("/assets", StaticFiles(directory=os.path.join("frontend", "dist", "assets")), name="assets"),
Mount("/icon", StaticFiles(directory=os.path.join("frontend", "dist", "icon")), name="icon"),
]
application = Starlette(debug=True, routes=routes, middleware=middleware)
# ----------------- Run -----------------
if __name__ == "__main__":
import uvicorn
2025-07-29 23:42:15 -04:00
uvicorn.run("app:application", host="0.0.0.0", port=8000, reload=True)