Merge pull request #418 from arcbtc/FastAPI

Posgres support
This commit is contained in:
Arc
2021-11-15 20:46:49 -06:00
committed by GitHub
5 changed files with 46 additions and 15 deletions

View File

@ -5,6 +5,21 @@ nav_order: 2
---
# Basic installation
Install Postgres and setup a database for LNbits:
```sh
# on debian/ubuntu 'sudo apt-get -y install postgresql'
# or follow instructions at https://www.postgresql.org/download/linux/
# Postgres doesn't have a default password, so we'll create one.
sudo -i -u postgres
psql
# on psql
ALTER USER postgres PASSWORD 'myPassword'; # choose whatever password you want
\q
# on postgres user
createdb lnbits
exit
```
Download this repo and install the dependencies:
@ -16,7 +31,10 @@ cd lnbits/
python3 -m venv venv
./venv/bin/pip install -r requirements.txt
cp .env.example .env
mkdir data
# add the database connection string to .env 'nano .env' LNBITS_DATABASE_URL=
# postgres://<user>:<password>@<host>/<database> - alter line bellow with your user, password and db name
LNBITS_DATABASE_URL="postgres://postgres:postgres@localhost/lnbits"
# save and exit
./venv/bin/uvicorn lnbits.__main__:app --port 5000
```
@ -28,6 +46,10 @@ Then you can restart it and it will be using the new settings.
You might also need to install additional packages or perform additional setup steps, depending on the chosen backend. See [the short guide](./wallets.md) on each different funding source.
## Important note
If you already have LNbits installed and running, we **HIGHLY** recommend you migrate to postgres!
You can use the script and instructions on [this guide](https://github.com/talvasconcelos/lnbits-sqlite-to-postgres) to migrate your SQLite database to Postgres.
# Additional guides
### LNbits running on Umbrel behind Tor

View File

@ -64,7 +64,7 @@ async def m001_initial(db):
await db.execute(
"""
CREATE VIEW IF NOT EXISTS balances AS
CREATE VIEW balances AS
SELECT wallet, COALESCE(SUM(s), 0) AS balance FROM (
SELECT wallet, SUM(amount) AS s -- incoming
FROM apipayments
@ -144,7 +144,7 @@ async def m004_ensure_fees_are_always_negative(db):
await db.execute("DROP VIEW balances")
await db.execute(
"""
CREATE VIEW IF NOT EXISTS balances AS
CREATE VIEW balances AS
SELECT wallet, COALESCE(SUM(s), 0) AS balance FROM (
SELECT wallet, SUM(amount) AS s -- incoming
FROM apipayments

View File

@ -1,9 +1,10 @@
import os
import asyncio
import time
import datetime
from typing import Optional
import os
import time
from contextlib import asynccontextmanager
from typing import Optional
from sqlalchemy import create_engine
from sqlalchemy_aio.base import AsyncConnection
from sqlalchemy_aio.strategy import ASYNCIO_STRATEGY # type: ignore
@ -93,6 +94,12 @@ class Database(Compat):
import psycopg2 # type: ignore
def _parse_timestamp(value, _):
f = "%Y-%m-%d %H:%M:%S.%f"
if not "." in value:
f = "%Y-%m-%d %H:%M:%S"
return time.mktime(datetime.datetime.strptime(value, f).timetuple())
psycopg2.extensions.register_type(
psycopg2.extensions.new_type(
psycopg2.extensions.DECIMAL.values,
@ -114,11 +121,12 @@ class Database(Compat):
psycopg2.extensions.new_type(
(1184, 1114),
"TIMESTAMP2INT",
lambda value, curs: time.mktime(
datetime.datetime.strptime(
value, "%Y-%m-%d %H:%M:%S.%f"
).timetuple()
),
_parse_timestamp
# lambda value, curs: time.mktime(
# datetime.datetime.strptime(
# value, "%Y-%m-%d %H:%M:%S.%f"
# ).timetuple()
# ),
)
)
else:

View File

@ -63,7 +63,7 @@ class LNbitsWallet(Wallet):
async with httpx.AsyncClient() as client:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments", headers=self.key, json=data
url=f"{self.endpoint}/api/v1/payments", headers=self.key, json=data,
)
ok, checking_id, payment_request, error_message = (
not r.is_error,
@ -85,7 +85,8 @@ class LNbitsWallet(Wallet):
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json={"out": True, "bolt11": bolt11},
json={"out": True, "bolt11": bolt11},
timeout=100,
)
ok, checking_id, fee_msat, error_message = not r.is_error, None, 0, None

View File

@ -80,10 +80,10 @@ class LntxbotWallet(Wallet):
f"{self.endpoint}/payinvoice",
headers=self.auth,
json={"invoice": bolt11},
timeout=40,
timeout=100,
)
if r.is_error:
if "error" in r.json():
try:
data = r.json()
error_message = data["message"]