diff --git a/cashu/core/settings.py b/cashu/core/settings.py index 6a66723..6ead713 100644 --- a/cashu/core/settings.py +++ b/cashu/core/settings.py @@ -80,8 +80,10 @@ class WalletSettings(CashuSettings): mint_url: str = Field(default=None) mint_host: str = Field(default="8333.space") mint_port: int = Field(default=3338) + wallet_name: str = Field(default="wallet") api_port: int = Field(default=4448) + api_host: str = Field(default="127.0.0.1") nostr_private_key: str = Field(default=None) nostr_relays: List[str] = Field( diff --git a/cashu/wallet/api/api_server.py b/cashu/wallet/api/api_server.py new file mode 100644 index 0000000..2d09be0 --- /dev/null +++ b/cashu/wallet/api/api_server.py @@ -0,0 +1,13 @@ +import uvicorn + +from ...core.settings import settings + + +def start_api_server(port=settings.api_port, host=settings.api_host): + config = uvicorn.Config( + "cashu.wallet.api.app:app", + port=port, + host=host, + ) + server = uvicorn.Server(config) + server.run() diff --git a/cashu/wallet/api/main.py b/cashu/wallet/api/main.py deleted file mode 100644 index 4486661..0000000 --- a/cashu/wallet/api/main.py +++ /dev/null @@ -1,9 +0,0 @@ -import uvicorn - -from ...core.settings import settings - - -def main(port=settings.api_port): - config = uvicorn.Config("cashu.wallet.api.app:app", port=port, host="127.0.0.1") - server = uvicorn.Server(config) - server.run() diff --git a/cashu/wallet/api/router.py b/cashu/wallet/api/router.py index 113ed9e..a36b8cd 100644 --- a/cashu/wallet/api/router.py +++ b/cashu/wallet/api/router.py @@ -36,7 +36,9 @@ from .responses import ( router: APIRouter = APIRouter() -def create_wallet(url=settings.mint_url, dir=settings.cashu_dir, name="wallet"): +def create_wallet( + url=settings.mint_url, dir=settings.cashu_dir, name=settings.wallet_name +): return Wallet(url, os.path.join(dir, name), name=name) diff --git a/cashu/wallet/cli/cli.py b/cashu/wallet/cli/cli.py index d3801a5..126002b 100644 --- a/cashu/wallet/cli/cli.py +++ b/cashu/wallet/cli/cli.py @@ -20,6 +20,7 @@ from ...nostr.nostr.client.client import NostrClient from ...tor.tor import TorProxy from ...wallet.crud import get_lightning_invoices, get_reserved_proofs, get_unused_locks from ...wallet.wallet import Wallet as Wallet +from ..api.api_server import start_api_server from ..cli.cli_helpers import get_mint_wallet, print_mint_balances, verify_mint from ..helpers import deserialize_token_from_string, init_wallet, receive, send from ..nostr import receive_nostr, send_nostr @@ -32,6 +33,13 @@ class NaturalOrderGroup(click.Group): return self.commands.keys() +def run_api_server(ctx, param, daemon): + if not daemon: + return + start_api_server() + ctx.exit() + + @click.group(cls=NaturalOrderGroup) @click.option( "--host", @@ -43,8 +51,17 @@ class NaturalOrderGroup(click.Group): "--wallet", "-w", "walletname", - default="wallet", - help="Wallet name (default: wallet).", + default=settings.wallet_name, + help=f"Wallet name (default: {settings.wallet_name}).", +) +@click.option( + "--daemon", + "-d", + is_flag=True, + is_eager=True, + expose_value=False, + callback=run_api_server, + help="Start server for wallet REST API", ) @click.pass_context def cli(ctx: Context, host: str, walletname: str): diff --git a/pyproject.toml b/pyproject.toml index f6c5e7c..e489a7e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,5 +53,4 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] mint = "cashu.mint.main:main" cashu = "cashu.wallet.cli.cli:cli" -api = "cashu.wallet.api.main:main" wallet-test = "tests.test_wallet:test" \ No newline at end of file