Allow to start wallet API by cashu --daemon (#243)

* Allow to start wallet API by cashu --daemon

* Provide access to wallet name via settings

* Make format

* Use flag is_eager for daemon option

* add setting api_host

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
sihamon
2023-06-08 14:42:37 +02:00
committed by GitHub
parent 5c820f9469
commit 786fbf2856
6 changed files with 37 additions and 13 deletions

View File

@@ -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(

View File

@@ -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()

View File

@@ -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()

View File

@@ -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)

View File

@@ -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):

View File

@@ -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"