Merge branch 'FastAPI' of https://github.com/arcbtc/lnbits into FastAPI
This commit is contained in:
commit
3df8e22ba2
5 changed files with 23 additions and 24 deletions
|
|
@ -96,6 +96,10 @@ def register_routes(app: FastAPI) -> None:
|
||||||
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
||||||
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
ext_route = getattr(ext_module, f"{ext.code}_ext")
|
||||||
|
|
||||||
|
if hasattr(ext_module, f"{ext.code}_start"):
|
||||||
|
ext_start_func = getattr(ext_module, f"{ext.code}_start")
|
||||||
|
ext_start_func()
|
||||||
|
|
||||||
if hasattr(ext_module, f"{ext.code}_static_files"):
|
if hasattr(ext_module, f"{ext.code}_static_files"):
|
||||||
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
|
ext_statics = getattr(ext_module, f"{ext.code}_static_files")
|
||||||
for s in ext_statics:
|
for s in ext_statics:
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
from lnbits.helpers import template_renderer
|
from lnbits.helpers import template_renderer
|
||||||
|
from lnbits.tasks import catch_everything_and_restart
|
||||||
|
|
||||||
db = Database("ext_lnticket")
|
db = Database("ext_lnticket")
|
||||||
|
|
||||||
|
|
@ -21,9 +24,10 @@ def lnticket_renderer():
|
||||||
|
|
||||||
from .views_api import * # noqa
|
from .views_api import * # noqa
|
||||||
from .views import * # noqa
|
from .views import * # noqa
|
||||||
from .tasks import register_listeners
|
from .tasks import wait_for_paid_invoices
|
||||||
|
|
||||||
|
|
||||||
|
def lnticket_start():
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
|
||||||
|
|
||||||
@lnticket_ext.on_event("startup")
|
|
||||||
def _do_it():
|
|
||||||
# FIXME: isn't called yet
|
|
||||||
register_listeners()
|
|
||||||
|
|
|
||||||
|
|
@ -4,25 +4,21 @@ from typing import List, Optional, Union
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import CreateFormData, Tickets, Forms
|
from .models import CreateFormData, CreateTicketData, Tickets, Forms
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
|
||||||
async def create_ticket(
|
async def create_ticket(
|
||||||
payment_hash: str,
|
payment_hash: str,
|
||||||
wallet: str,
|
wallet: str,
|
||||||
form: str,
|
data: CreateTicketData
|
||||||
name: str,
|
|
||||||
email: str,
|
|
||||||
ltext: str,
|
|
||||||
sats: int,
|
|
||||||
) -> Tickets:
|
) -> Tickets:
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
INSERT INTO lnticket.ticket (id, form, email, ltext, name, wallet, sats, paid)
|
INSERT INTO lnticket.ticket (id, form, email, ltext, name, wallet, sats, paid)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(payment_hash, form, email, ltext, name, wallet, sats, False),
|
(payment_hash, data.form, data.email, data.ltext, data.name, wallet, data.sats, False),
|
||||||
)
|
)
|
||||||
|
|
||||||
ticket = await get_ticket(payment_hash)
|
ticket = await get_ticket(payment_hash)
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,12 @@ from lnbits.tasks import register_invoice_listener
|
||||||
from .crud import get_ticket, set_ticket_paid
|
from .crud import get_ticket, set_ticket_paid
|
||||||
|
|
||||||
|
|
||||||
async def register_listeners():
|
async def wait_for_paid_invoices():
|
||||||
send_queue = asyncio.Queue()
|
invoice_queue = asyncio.Queue()
|
||||||
recv_queue = asyncio.Queue()
|
register_invoice_listener(invoice_queue)
|
||||||
register_invoice_listener(send_queue)
|
|
||||||
await wait_for_paid_invoices(recv_queue)
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue):
|
|
||||||
while True:
|
while True:
|
||||||
payment = await invoice_paid_queue.get()
|
payment = await invoice_queue.get()
|
||||||
await on_invoice_paid(payment)
|
await on_invoice_paid(payment)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -136,13 +136,12 @@ async def api_ticket_make_ticket(data: CreateTicketData, form_id):
|
||||||
)
|
)
|
||||||
# return {"message": "LNTicket does not exist."}, HTTPStatus.NOT_FOUND
|
# return {"message": "LNTicket does not exist."}, HTTPStatus.NOT_FOUND
|
||||||
|
|
||||||
nwords = len(re.split(r"\s+", data["ltext"]))
|
nwords = len(re.split(r"\s+", data.ltext))
|
||||||
sats = data["sats"]
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payment_hash, payment_request = await create_invoice(
|
payment_hash, payment_request = await create_invoice(
|
||||||
wallet_id=form.wallet,
|
wallet_id=form.wallet,
|
||||||
amount=sats,
|
amount=data.sats,
|
||||||
memo=f"ticket with {nwords} words on {form_id}",
|
memo=f"ticket with {nwords} words on {form_id}",
|
||||||
extra={"tag": "lnticket"},
|
extra={"tag": "lnticket"},
|
||||||
)
|
)
|
||||||
|
|
@ -154,7 +153,7 @@ async def api_ticket_make_ticket(data: CreateTicketData, form_id):
|
||||||
# return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
|
# return {"message": str(e)}, HTTPStatus.INTERNAL_SERVER_ERROR
|
||||||
|
|
||||||
ticket = await create_ticket(
|
ticket = await create_ticket(
|
||||||
payment_hash=payment_hash, wallet=form.wallet, **data
|
payment_hash=payment_hash, wallet=form.wallet, data=data
|
||||||
)
|
)
|
||||||
|
|
||||||
if not ticket:
|
if not ticket:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue