Skip to content

Commit

Permalink
feat: code quality (#6)
Browse files Browse the repository at this point in the history
* feat: code quality

* fixes
  • Loading branch information
dni authored Aug 2, 2024
1 parent 93b6a09 commit 55cf5e7
Show file tree
Hide file tree
Showing 23 changed files with 2,857 additions and 111 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: lint
on:
push:
branches:
- main
pull_request:

jobs:
lint:
uses: lnbits/lnbits/.github/workflows/lint.yml@dev
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

release:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -34,12 +33,12 @@ jobs:
- name: Create pull request in extensions repo
env:
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
repo_name: "${{ github.event.repository.name }}"
tag: "${{ github.ref_name }}"
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
repo_name: '${{ github.event.repository.name }}'
tag: '${{ github.ref_name }}'
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
run: |
cd lnbits-extensions
git checkout -b $branch
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
__pycache__
node_modules
.mypy_cache
.venv
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"semi": false,
"arrowParens": "avoid",
"insertPragma": false,
"printWidth": 80,
"proseWrap": "preserve",
"singleQuote": true,
"trailingComma": "none",
"useTabs": false,
"bracketSameLine": false,
"bracketSpacing": false
}
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
all: format check

format: prettier black ruff

check: mypy pyright checkblack checkruff checkprettier

prettier:
poetry run ./node_modules/.bin/prettier --write .
pyright:
poetry run ./node_modules/.bin/pyright

mypy:
poetry run mypy .

black:
poetry run black .

ruff:
poetry run ruff check . --fix

checkruff:
poetry run ruff check .

checkprettier:
poetry run ./node_modules/.bin/prettier --check .

checkblack:
poetry run black --check .

checkeditorconfig:
editorconfig-checker

test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest
install-pre-commit-hook:
@echo "Installing pre-commit hook to git"
@echo "Uninstall the hook with poetry run pre-commit uninstall"
poetry run pre-commit install

pre-commit:
poetry run pre-commit run --all-files


checkbundle:
@echo "skipping checkbundle"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Subdomains Extension - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>

<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>

So the goal of the extension is to allow the owner of a domain to sell subdomains to anyone who is willing to pay some money for it.
Expand Down
34 changes: 19 additions & 15 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from fastapi import APIRouter
from loguru import logger

from lnbits.db import Database
from lnbits.helpers import template_renderer
from lnbits.tasks import create_permanent_unique_task

db = Database("ext_subdomains")
from .crud import db
from .tasks import wait_for_paid_invoices
from .views import subdomains_generic_router
from .views_api import subdomains_api_router

subdomains_ext: APIRouter = APIRouter(prefix="/subdomains", tags=["subdomains"])
subdomains_ext.include_router(subdomains_generic_router)
subdomains_ext.include_router(subdomains_api_router)

subdomains_static_files = [
{
Expand All @@ -18,25 +19,28 @@
}
]


def subdomains_renderer():
return template_renderer(["subdomains/templates"])


from .tasks import wait_for_paid_invoices
from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403


scheduled_tasks: list[asyncio.Task] = []


def subdomains_stop():
for task in scheduled_tasks:
try:
task.cancel()
except Exception as ex:
logger.warning(ex)


def subdomains_start():
from lnbits.tasks import create_permanent_unique_task

task = create_permanent_unique_task("ext_subdomains", wait_for_paid_invoices)
scheduled_tasks.append(task)


__all__ = [
"db",
"subdomains_ext",
"subdomains_static_files",
"subdomains_start",
"subdomains_stop",
]
7 changes: 4 additions & 3 deletions cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
async def cloudflare_create_subdomain(
domain: Domains, subdomain: str, record_type: str, ip: str
):
# Call to cloudflare sort of a dry-run - if success delete the domain and wait for payment
# Call to cloudflare sort of a dry-run
# if success delete the domain and wait for payment
### SEND REQUEST TO CLOUDFLARE
url = (
"https://api.cloudflare.com/client/v4/zones/"
Expand All @@ -17,14 +18,14 @@ async def cloudflare_create_subdomain(
"Authorization": "Bearer " + domain.cf_token,
"Content-Type": "application/json",
}
aRecord = subdomain + "." + domain.domain
a_record = subdomain + "." + domain.domain
async with httpx.AsyncClient() as client:
r = await client.post(
url,
headers=header,
json={
"type": record_type,
"name": aRecord,
"name": a_record,
"content": ip,
"ttl": 0,
"proxied": False,
Expand Down
41 changes: 30 additions & 11 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from typing import List, Optional, Union
from typing import Optional, Union

from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash

from . import db
from .models import CreateDomain, CreateSubdomain, Domains, Subdomains

db = Database("ext_subdomains")


async def create_subdomain(payment_hash, wallet, data: CreateSubdomain) -> Subdomains:
await db.execute(
"""
INSERT INTO subdomains.subdomain (id, domain, email, subdomain, ip, wallet, sats, duration, paid, record_type)
INSERT INTO subdomains.subdomain
(id, domain, email, subdomain, ip, wallet, sats, duration, paid, record_type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
Expand All @@ -33,7 +36,10 @@ async def create_subdomain(payment_hash, wallet, data: CreateSubdomain) -> Subdo

async def set_subdomain_paid(payment_hash: str) -> Subdomains:
row = await db.fetchone(
"SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?",
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?
""",
(payment_hash,),
)
if row[8] is False:
Expand Down Expand Up @@ -66,27 +72,36 @@ async def set_subdomain_paid(payment_hash: str) -> Subdomains:

async def get_subdomain(subdomain_id: str) -> Optional[Subdomains]:
row = await db.fetchone(
"SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?",
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?
""",
(subdomain_id,),
)
return Subdomains(**row) if row else None


async def get_subdomainBySubdomain(subdomain: str) -> Optional[Subdomains]:
async def get_subdomain_by_subdomain(subdomain: str) -> Optional[Subdomains]:
row = await db.fetchone(
"SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.subdomain = ?",
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.subdomain = ?
""",
(subdomain,),
)
return Subdomains(**row) if row else None


async def get_subdomains(wallet_ids: Union[str, List[str]]) -> List[Subdomains]:
async def get_subdomains(wallet_ids: Union[str, list[str]]) -> list[Subdomains]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.wallet IN ({q})",
f"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.wallet IN ({q})
""",
(*wallet_ids,),
)

Expand All @@ -104,7 +119,11 @@ async def create_domain(data: CreateDomain) -> Domains:
domain_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO subdomains.domain (id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, amountmade, allowed_record_types)
INSERT INTO subdomains.domain
(
id, wallet, domain, webhook, cf_token, cf_zone_id,
description, cost, amountmade, allowed_record_types
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
Expand Down Expand Up @@ -145,7 +164,7 @@ async def get_domain(domain_id: str) -> Optional[Domains]:
return Domains(**row) if row else None


async def get_domains(wallet_ids: Union[str, List[str]]) -> List[Domains]:
async def get_domains(wallet_ids: Union[str, list[str]]) -> list[Domains]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

Expand Down
1 change: 0 additions & 1 deletion description.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Use the cloudflare API to rent subdomains for the domains your own.

Specify exactly what sort of record you want the api to access.

14 changes: 7 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"repos": [
{
"id": "subdomains",
"organisation": "lnbits",
"repository": "subdomains"
}
]
"repos": [
{
"id": "subdomains",
"organisation": "lnbits",
"repository": "subdomains"
}
]
}
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "subdomains",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"prettier": "^3.2.5",
"pyright": "^1.1.358"
}
}
Loading

0 comments on commit 55cf5e7

Please sign in to comment.