fix: properly start/stop tasks (#28)
* fix: properly start/stop tasks https://github.com/lnbits/lnbits/issues/2411 * types * nicer
This commit is contained in:
parent
942d997c70
commit
d656d41b90
2 changed files with 33 additions and 39 deletions
34
views_api.py
34
views_api.py
|
|
@ -9,16 +9,13 @@ from starlette.exceptions import HTTPException
|
|||
from lnbits.decorators import check_admin
|
||||
from lnbits.helpers import decrypt_internal_message, urlsafe_short_hash
|
||||
|
||||
from . import nostr_client, nostrclient_ext, scheduled_tasks
|
||||
from . import all_routers, nostr_client, nostrclient_ext
|
||||
from .crud import add_relay, create_config, delete_relay, get_config, get_relays, update_config
|
||||
from .helpers import normalize_public_key
|
||||
from .models import Config, Relay, TestMessage, TestMessageResponse
|
||||
from .nostr.key import EncryptedDirectMessage, PrivateKey
|
||||
from .router import NostrRouter
|
||||
|
||||
# we keep this in
|
||||
all_routers: list[NostrRouter] = []
|
||||
|
||||
|
||||
@nostrclient_ext.get("/api/v1/relays", dependencies=[Depends(check_admin)])
|
||||
async def api_get_relays() -> List[Relay]:
|
||||
|
|
@ -111,28 +108,6 @@ async def api_test_endpoint(data: TestMessage) -> TestMessageResponse:
|
|||
)
|
||||
|
||||
|
||||
@nostrclient_ext.delete(
|
||||
"/api/v1", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
|
||||
)
|
||||
async def api_stop():
|
||||
for router in all_routers:
|
||||
try:
|
||||
await router.stop()
|
||||
all_routers.remove(router)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
nostr_client.close()
|
||||
|
||||
for scheduled_task in scheduled_tasks:
|
||||
try:
|
||||
scheduled_task.cancel()
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
|
||||
return {"success": True}
|
||||
|
||||
|
||||
@nostrclient_ext.websocket("/api/v1/{id}")
|
||||
async def ws_relay(id: str, websocket: WebSocket) -> None:
|
||||
"""Relay multiplexer: one client (per endpoint) <-> multiple relays"""
|
||||
|
|
@ -184,13 +159,14 @@ async def ws_relay(id: str, websocket: WebSocket) -> None:
|
|||
|
||||
|
||||
@nostrclient_ext.get("/api/v1/config", dependencies=[Depends(check_admin)])
|
||||
async def api_get_relays() -> Config:
|
||||
async def api_get_config() -> Config:
|
||||
config = await get_config()
|
||||
if not config:
|
||||
await create_config()
|
||||
|
||||
config = await create_config()
|
||||
assert config, "Failed to create config"
|
||||
return config
|
||||
|
||||
|
||||
@nostrclient_ext.put("/api/v1/config", dependencies=[Depends(check_admin)])
|
||||
async def api_update_config(
|
||||
data: Config
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue