-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
46 lines (38 loc) · 1.18 KB
/
Copy pathserver.py
File metadata and controls
46 lines (38 loc) · 1.18 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
from fastapi import FastAPI, HTTPException, Request
from fastapi.templating import Jinja2Templates
from typing import Optional
import uvicorn
from lp import Storage, Search
app = FastAPI()
storage = Storage("maas")
searcher = Search(storage)
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def get_search_page(request: Request):
"""
Serve the search page.
"""
return templates.TemplateResponse(
"search.html", {"request": request, "limit": 5, "query": ""}
)
@app.get("/search/")
async def search(request: Request, query: str, limit: Optional[int] = 10):
"""
Handle the search query and return results.
"""
try:
results = searcher.find_similar_issues(query, limit)
return templates.TemplateResponse(
"search.html",
{
"request": request,
"results": results,
"query": query,
"limit": limit,
},
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Run with `uvicorn server:app --reload` for live updates
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)