forked from lnbits/tpos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
69 lines (50 loc) · 1.73 KB
/
__init__.py
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
import asyncio
from fastapi import APIRouter, Request, Response
from fastapi.routing import APIRoute
from lnbits.db import Database
from lnbits.helpers import template_renderer
from lnbits.tasks import create_permanent_task
from typing import Callable
from fastapi.responses import JSONResponse
db = Database("ext_tpos")
class LNURLErrorResponseHandler(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
try:
response = await original_route_handler(request)
except HTTPException as exc:
logger.debug(f"HTTPException: {exc}")
response = JSONResponse(
status_code=exc.status_code,
content={"status": "ERROR", "reason": f"{exc.detail}"},
)
except Exception as exc:
raise exc
return response
return custom_route_handler
tpos_ext: APIRouter = APIRouter(
prefix="/tpos", tags=["TPoS"], route_class=LNURLErrorResponseHandler
)
tpos_static_files = [
{
"path": "/tpos/static",
"name": "tpos_static",
}
]
def tpos_renderer():
return template_renderer(["tpos/templates"])
from .lnurl import * # noqa: F401,F403
from .tasks import wait_for_paid_invoices
from .views import * # noqa
from .views_api import * # noqa
scheduled_tasks: list[asyncio.Task] = []
def tpos_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)
def tpos_start():
task = create_permanent_task(wait_for_paid_invoices)
scheduled_tasks.append(task)