1313import asyncio
1414import logging
1515from collections .abc import Callable
16- from dataclasses import dataclass
16+ from dataclasses import dataclass , field
1717from typing import TYPE_CHECKING
1818
1919from aiohttp import web
2424logger = logging .getLogger (__name__ )
2525
2626
27+ def _no_store () -> Store | None :
28+ """Default store getter that returns None."""
29+ return None
30+
31+
32+ async def _handle_health (_request : web .Request ) -> web .Response :
33+ """Handle health check endpoint."""
34+ return web .json_response ({"status" : "healthy" , "service" : "lean-spec-api" })
35+
36+
2737@dataclass (frozen = True , slots = True )
2838class ApiServerConfig :
2939 """Configuration for the API server."""
@@ -38,6 +48,7 @@ class ApiServerConfig:
3848 """Whether the API server is enabled."""
3949
4050
51+ @dataclass (slots = True )
4152class ApiServer :
4253 """
4354 HTTP API server for checkpoint sync and node status.
@@ -49,36 +60,22 @@ class ApiServer:
4960 Uses aiohttp to handle HTTP protocol details efficiently.
5061 """
5162
52- def __init__ (
53- self ,
54- config : ApiServerConfig ,
55- store_getter : Callable [[], Store | None ] = lambda : None ,
56- ):
57- """
58- Initialize the API server.
63+ config : ApiServerConfig
64+ """Server configuration."""
5965
60- Args:
61- config: Server configuration.
62- store_getter: Callable that returns the current Store instance.
63- """
64- self .config = config
65- self ._store_getter = store_getter
66- self ._runner : web .AppRunner | None = None
67- self ._site : web .TCPSite | None = None
66+ store_getter : Callable [[], Store | None ] = _no_store
67+ """Callable that returns the current Store instance."""
6868
69- def set_store_getter (self , getter : Callable [[], Store | None ]) -> None :
70- """
71- Set the store getter function.
69+ _runner : web .AppRunner | None = field (default = None , init = False )
70+ """The aiohttp application runner."""
7271
73- Args:
74- getter: Callable that returns the current Store instance.
75- """
76- self ._store_getter = getter
72+ _site : web .TCPSite | None = field (default = None , init = False )
73+ """The TCP site for the server."""
7774
7875 @property
7976 def store (self ) -> Store | None :
8077 """Get the current Store instance."""
81- return self ._store_getter ()
78+ return self .store_getter ()
8279
8380 async def start (self ) -> None :
8481 """Start the API server in the background."""
@@ -89,7 +86,7 @@ async def start(self) -> None:
8986 app = web .Application ()
9087 app .add_routes (
9188 [
92- web .get ("/health" , self . _handle_health ),
89+ web .get ("/health" , _handle_health ),
9390 web .get ("/lean/states/finalized" , self ._handle_finalized_state ),
9491 ]
9592 )
@@ -127,11 +124,7 @@ async def _async_stop(self) -> None:
127124 self ._site = None
128125 logger .info ("API server stopped" )
129126
130- async def _handle_health (self , request : web .Request ) -> web .Response :
131- """Handle health check endpoint."""
132- return web .json_response ({"status" : "healthy" , "service" : "lean-spec-api" })
133-
134- async def _handle_finalized_state (self , request : web .Request ) -> web .Response :
127+ async def _handle_finalized_state (self , _request : web .Request ) -> web .Response :
135128 """
136129 Handle finalized checkpoint state endpoint.
137130
0 commit comments