-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgraph_server.py
More file actions
50 lines (46 loc) · 1.68 KB
/
Copy pathgraph_server.py
File metadata and controls
50 lines (46 loc) · 1.68 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
"""Launch the optional read-only recall/repo-graph HTTP server."""
from __future__ import annotations
import argparse
import ipaddress
import os
def _loopback(host: str) -> bool:
# An empty host string makes the socket layer bind ALL interfaces, so it is
# emphatically not loopback; any unparseable hostname is treated as
# non-loopback too (fail closed: a token is then required).
if not host:
return False
if host == "localhost":
return True
try:
return ipaddress.ip_address(host).is_loopback
except ValueError:
return False
def main(argv=None) -> int:
parser = argparse.ArgumentParser(prog="engraphis-graph-server")
parser.add_argument("--host", default=os.environ.get("ENGRAPHIS_GRAPH_HOST", "127.0.0.1"))
parser.add_argument(
"--port", type=int, default=int(os.environ.get("ENGRAPHIS_GRAPH_PORT", "8720"))
)
args = parser.parse_args(argv)
token = (
os.environ.get("ENGRAPHIS_GRAPH_TOKEN")
or os.environ.get("ENGRAPHIS_API_TOKEN", "")
)
if not _loopback(args.host) and not token:
parser.error(
"non-loopback graph serving requires ENGRAPHIS_GRAPH_TOKEN "
"or ENGRAPHIS_API_TOKEN"
)
try:
import uvicorn
from engraphis.read_only_api import create_read_only_app
except ImportError as exc:
raise SystemExit(
"The server extra is required: pip install \"engraphis[server]\""
" (needs Python 3.10+)"
) from exc
uvicorn.run(create_read_only_app(token=token), host=args.host, port=args.port,
proxy_headers=False)
return 0
if __name__ == "__main__":
raise SystemExit(main())