41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from starlette.applications import Starlette
|
|
from starlette.routing import Route, Mount
|
|
from starlette.staticfiles import StaticFiles
|
|
from starlette.responses import FileResponse
|
|
from controllers import create_chat, post_message, chat_stream, history, get_models
|
|
from starlette.middleware import Middleware
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
import os
|
|
|
|
middleware = [
|
|
Middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
]
|
|
|
|
async def serve_frontend(request):
|
|
"""Serve the frontend index.html file"""
|
|
return FileResponse(os.path.join("frontend", "dist", "index.html"))
|
|
|
|
routes = [
|
|
Route("/", serve_frontend, methods=["GET"]),
|
|
Route("/models", get_models, methods=["GET"]),
|
|
Route("/chats", create_chat, methods=["POST"]),
|
|
Route("/chats/{chat_id:str}", history, methods=["GET"]),
|
|
Route("/chats/{chat_id:str}/messages", post_message, methods=["POST"]),
|
|
Route("/chats/{chat_id:str}/stream", chat_stream, methods=["GET"]),
|
|
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
|
|
uvicorn.run("app:application", host="0.0.0.0", port=8000, reload=True)
|