refactor: extract relay_info_response
This commit is contained in:
parent
2d4e836676
commit
729f36e993
2 changed files with 15 additions and 9 deletions
12
helpers.py
12
helpers.py
|
|
@ -1,6 +1,7 @@
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from bech32 import bech32_decode, convertbits
|
from bech32 import bech32_decode, convertbits
|
||||||
|
from starlette.responses import JSONResponse
|
||||||
|
|
||||||
|
|
||||||
def normalize_public_key(pubkey: str) -> str:
|
def normalize_public_key(pubkey: str) -> str:
|
||||||
|
|
@ -23,3 +24,14 @@ def normalize_public_key(pubkey: str) -> str:
|
||||||
|
|
||||||
def extract_domain(url: str) -> str:
|
def extract_domain(url: str) -> str:
|
||||||
return urlparse(url).netloc
|
return urlparse(url).netloc
|
||||||
|
|
||||||
|
|
||||||
|
def relay_info_response(relay_public_data: dict) -> JSONResponse:
|
||||||
|
return JSONResponse(
|
||||||
|
content=relay_public_data,
|
||||||
|
headers={
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
"Access-Control-Allow-Headers": "*",
|
||||||
|
"Access-Control-Allow-Methods": "GET",
|
||||||
|
},
|
||||||
|
)
|
||||||
12
views.py
12
views.py
|
|
@ -3,13 +3,14 @@ from http import HTTPStatus
|
||||||
from fastapi import Depends, Request
|
from fastapi import Depends, Request
|
||||||
from fastapi.exceptions import HTTPException
|
from fastapi.exceptions import HTTPException
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from starlette.responses import HTMLResponse, JSONResponse
|
from starlette.responses import HTMLResponse
|
||||||
|
|
||||||
from lnbits.core.models import User
|
from lnbits.core.models import User
|
||||||
from lnbits.decorators import check_user_exists
|
from lnbits.decorators import check_user_exists
|
||||||
|
|
||||||
from . import nostrrelay_ext, nostrrelay_renderer
|
from . import nostrrelay_ext, nostrrelay_renderer
|
||||||
from .crud import get_public_relay
|
from .crud import get_public_relay
|
||||||
|
from .helpers import relay_info_response
|
||||||
|
|
||||||
templates = Jinja2Templates(directory="templates")
|
templates = Jinja2Templates(directory="templates")
|
||||||
|
|
||||||
|
|
@ -32,14 +33,7 @@ async def nostrrelay(request: Request, relay_id: str):
|
||||||
)
|
)
|
||||||
|
|
||||||
if request.headers.get("accept") == "application/nostr+json":
|
if request.headers.get("accept") == "application/nostr+json":
|
||||||
return JSONResponse(
|
return relay_info_response(relay_public_data)
|
||||||
content=relay_public_data,
|
|
||||||
headers={
|
|
||||||
"Access-Control-Allow-Origin": "*",
|
|
||||||
"Access-Control-Allow-Headers": "*",
|
|
||||||
"Access-Control-Allow-Methods": "GET",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
return nostrrelay_renderer().TemplateResponse(
|
return nostrrelay_renderer().TemplateResponse(
|
||||||
"nostrrelay/public.html", {"request": request, "relay": relay_public_data}
|
"nostrrelay/public.html", {"request": request, "relay": relay_public_data}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue