Skip to content

Commit

Permalink
use cbor for backend communication
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Dec 24, 2024
1 parent 86cb853 commit 6c86565
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
60 changes: 59 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ include = "syzygy_tables_info"
python = "^3.9.2"
aiohttp = "^3.11.11"
Brotli = "^1.1.0"
cbor2 = "^5.6.5"
chess = "^1.11.1"
tinyhtml = "^1.2.0"

Expand Down
15 changes: 11 additions & 4 deletions syzygy_tables_info/server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import aiohttp.web

import cbor2
import chess
import chess.pgn
import chess.syzygy
Expand Down Expand Up @@ -185,15 +186,18 @@ async def syzygy_vs_syzygy_pgn(request: aiohttp.web.Request) -> aiohttp.web.Stre

# Query backend.
async with request.app["session"].get(request.app["config"].get("server", "backend") + "/mainline",
headers={"X-Forwarded-For": request.remote} if request.remote else {},
headers={
"Accept": "application/cbor",
"X-Forwarded-For": request.remote,
},
params={"fen": board.fen()}) as res:
if res.status == 404:
result: Dict[str, Any] = {
"dtz": None,
"mainline": [],
}
else:
result = await res.json()
result = cbor2.loads(await res.read())

# Starting comment.
if result["dtz"] == 0:
Expand Down Expand Up @@ -306,7 +310,10 @@ async def index(request: aiohttp.web.Request) -> aiohttp.web.Response:
else:
# Query backend.
async with request.app["session"].get(request.app["config"].get("server", "backend"),
headers={"X-Forwarded-For": request.remote} if request.remote else {},
headers={
"Accept": "application/cbor",
"X-Forwarded-For": request.remote
},
params={"fen": board.fen()}) as res:
if res.status != 200:
return aiohttp.web.Response(
Expand All @@ -315,7 +322,7 @@ async def index(request: aiohttp.web.Request) -> aiohttp.web.Response:
body=await res.read(),
charset=res.charset)

probe = await res.json()
probe = cbor2.loads(await res.read())

dtz = probe["dtz"]
active_dtz = dtz if dtz else None
Expand Down

0 comments on commit 6c86565

Please sign in to comment.