Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
127 lines (104 loc) · 3.77 KB
/
Copy pathserver.py
File metadata and controls
127 lines (104 loc) · 3.77 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import json
import webbrowser
from typing import Optional
from fastapi import FastAPI, Body
from fastapi.responses import HTMLResponse, StreamingResponse
import uvicorn
import config
import index_engine
app = FastAPI(title="DEADLINE Search Platform")
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
INDEX_HTML_PATH = os.path.join(BASE_DIR, "index.html")
def _resolve_dir(folder: str) -> str:
scan_dir = config.get_root_dir()
if folder and folder.strip():
f_clean = folder.strip()
if os.path.isabs(f_clean) and os.path.exists(f_clean):
scan_dir = f_clean
else:
candidate = os.path.normpath(os.path.join(scan_dir, f_clean))
if os.path.exists(candidate):
scan_dir = candidate
return scan_dir
@app.get("/", response_class=HTMLResponse)
async def serve_ui():
with open(INDEX_HTML_PATH, "r", encoding="utf-8") as f:
return f.read()
@app.get("/api/info")
async def get_info():
return {
"root_dir": config.get_root_dir(),
"index_dir": config.get_index_dir(),
"engine": "ripgrep",
}
@app.get("/api/settings")
async def get_settings():
cfg = config.get_config()
eff_root = config.get_root_dir()
eff_index = config.get_index_dir()
return {
"root_dir": cfg.get("root_dir", ""),
"index_dir": cfg.get("index_dir", ""),
"workers": cfg.get("workers", 0),
"max_matches": cfg.get("max_matches", 100000),
"effective_root_dir": eff_root,
"effective_index_dir": eff_index,
"root_exists": os.path.exists(eff_root),
"index_exists": os.path.exists(eff_index),
}
@app.post("/api/settings")
async def update_settings(payload: dict = Body(...)):
cfg = config.save_config(payload)
eff_root = config.get_root_dir()
eff_index = config.get_index_dir()
return {
"ok": True,
"root_dir": cfg.get("root_dir", ""),
"index_dir": cfg.get("index_dir", ""),
"workers": cfg.get("workers", 0),
"max_matches": cfg.get("max_matches", 100000),
"effective_root_dir": eff_root,
"effective_index_dir": eff_index,
"root_exists": os.path.exists(eff_root),
"index_exists": os.path.exists(eff_index),
}
@app.get("/api/index/status")
async def index_status_endpoint(folder: str = ""):
return index_engine.index_status(_resolve_dir(folder))
@app.get("/api/index/build")
async def index_build_endpoint(folder: str = "", force: bool = False):
scan_dir = _resolve_dir(folder)
def event_stream():
for event in index_engine.build_index_generator(scan_dir, force=force):
yield json.dumps(event, ensure_ascii=False) + "\n"
return StreamingResponse(event_stream(), media_type="text/plain")
@app.get("/api/search")
async def search_endpoint(
q: str,
mode: str = "OR",
is_regex: bool = False,
folder: str = "",
deep_scan: bool = False,
):
scan_dir = _resolve_dir(folder)
max_matches = config.get_max_matches()
def event_stream():
for event in index_engine.search_index_generator(
scan_dir,
query=q,
mode=mode,
is_regex=is_regex,
max_matches=max_matches,
include_large=deep_scan,
):
yield json.dumps(event, ensure_ascii=False) + "\n"
return StreamingResponse(event_stream(), media_type="text/plain")
if __name__ == "__main__":
print("[*] DEADLINE Search Platform")
print("[*] Engine : ripgrep + Sharded Columnar Bloom index")
print(f"[*] Root dir : {config.get_root_dir()}")
print(f"[*] Index dir: {config.get_index_dir()}")
print("[*] URL : http://127.0.0.1:8000")
webbrowser.open("http://127.0.0.1:8000")
uvicorn.run(app, host="127.0.0.1", port=8000, log_level="warning")