mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 23:54:22 +01:00
plugin/clnrest: Improved readme and comments
- Adding runes info in readme - Added extra comments in the code - System exit on shutdown - Replaced single quotes with double quotes Changelog-None
This commit is contained in:
committed by
Rusty Russell
parent
3fab4cc4f3
commit
637f314bc6
@@ -21,6 +21,9 @@ If `rest-port` is not specified, the plugin will disable itself.
|
|||||||
|
|
||||||
With the default configurations, the Swagger user interface will be available at https://127.0.0.1:3010/. The POST method requires `rune` and `nodeid` headers for authorization.
|
With the default configurations, the Swagger user interface will be available at https://127.0.0.1:3010/. The POST method requires `rune` and `nodeid` headers for authorization.
|
||||||
|
|
||||||
|
- `nodeid` is the same as `id (pubkey)` received from [getinfo](https://docs.corelightning.org/reference/lightning-getinfo).
|
||||||
|
- A new `rune` can be created via [createrune](https://docs.corelightning.org/reference/lightning-createrune) or the list of existing runes can be retrieved with [listrunes](https://docs.corelightning.org/reference/lightning-listrunes) command.
|
||||||
|
|
||||||
### cURL
|
### cURL
|
||||||
Example curl command for POST will also require `rune` and `nodeid` headers like below:
|
Example curl command for POST will also require `rune` and `nodeid` headers like below:
|
||||||
`curl -k -X POST 'https://127.0.0.1:3010/v1/getinfo' -H 'Rune: <node-rune>' -H 'Nodeid: <node-id>'`
|
`curl -k -X POST 'https://127.0.0.1:3010/v1/getinfo' -H 'Rune: <node-rune>' -H 'Nodeid: <node-id>'`
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# For --hidden-import gunicorn.glogging gunicorn.workers.sync
|
# For --hidden-import gunicorn.glogging gunicorn.workers.sync
|
||||||
try:
|
try:
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
from gunicorn import glogging # noqa: F401
|
from gunicorn import glogging # noqa: F401
|
||||||
from gunicorn.workers import sync # noqa: F401
|
from gunicorn.workers import sync # noqa: F401
|
||||||
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_restx import Api
|
from flask_restx import Api
|
||||||
@@ -19,7 +20,6 @@ try:
|
|||||||
except ModuleNotFoundError as err:
|
except ModuleNotFoundError as err:
|
||||||
# OK, something is not installed?
|
# OK, something is not installed?
|
||||||
import json
|
import json
|
||||||
import sys
|
|
||||||
getmanifest = json.loads(sys.stdin.readline())
|
getmanifest = json.loads(sys.stdin.readline())
|
||||||
print(json.dumps({'jsonrpc': "2.0",
|
print(json.dumps({'jsonrpc': "2.0",
|
||||||
'id': getmanifest['id'],
|
'id': getmanifest['id'],
|
||||||
@@ -29,7 +29,7 @@ except ModuleNotFoundError as err:
|
|||||||
|
|
||||||
jobs = {}
|
jobs = {}
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
socketio = SocketIO(app, async_mode='gevent', cors_allowed_origins='*')
|
socketio = SocketIO(app, async_mode="gevent", cors_allowed_origins="*")
|
||||||
msgq = Queue()
|
msgq = Queue()
|
||||||
|
|
||||||
|
|
||||||
@@ -41,9 +41,12 @@ def broadcast_from_message_queue():
|
|||||||
return
|
return
|
||||||
plugin.log(f"Emitting message: {msg}", "debug")
|
plugin.log(f"Emitting message: {msg}", "debug")
|
||||||
socketio.emit("message", msg)
|
socketio.emit("message", msg)
|
||||||
time.sleep(1) # Wait for a second after processing all items in the queue
|
# Wait for a second after processing all items in the queue
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
# Starts a background task which pulls notifications from the message queue
|
||||||
|
# and broadcasts them to all connected ws clients at one-second intervals.
|
||||||
socketio.start_background_task(broadcast_from_message_queue)
|
socketio.start_background_task(broadcast_from_message_queue)
|
||||||
|
|
||||||
|
|
||||||
@@ -74,6 +77,8 @@ def set_application_options(plugin):
|
|||||||
from utilities.shared import CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT
|
from utilities.shared import CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT
|
||||||
plugin.log(f"REST Server is starting at {REST_PROTOCOL}://{REST_HOST}:{REST_PORT}", "debug")
|
plugin.log(f"REST Server is starting at {REST_PROTOCOL}://{REST_HOST}:{REST_PORT}", "debug")
|
||||||
if REST_PROTOCOL == "http":
|
if REST_PROTOCOL == "http":
|
||||||
|
# Assigning only one worker due to added complexity between gunicorn's multiple worker process forks
|
||||||
|
# and websocket connection's persistance with a single worker.
|
||||||
options = {
|
options = {
|
||||||
"bind": f"{REST_HOST}:{REST_PORT}",
|
"bind": f"{REST_HOST}:{REST_PORT}",
|
||||||
"workers": 1,
|
"workers": 1,
|
||||||
@@ -91,6 +96,8 @@ def set_application_options(plugin):
|
|||||||
plugin.log(f"Certs Path: {CERTS_PATH}", "debug")
|
plugin.log(f"Certs Path: {CERTS_PATH}", "debug")
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
raise Exception(f"{err}: Certificates do not exist at {CERTS_PATH}")
|
raise Exception(f"{err}: Certificates do not exist at {CERTS_PATH}")
|
||||||
|
# Assigning only one worker due to added complexity between gunicorn's multiple worker process forks
|
||||||
|
# and websocket connection's persistance with a single worker.
|
||||||
options = {
|
options = {
|
||||||
"bind": f"{REST_HOST}:{REST_PORT}",
|
"bind": f"{REST_HOST}:{REST_PORT}",
|
||||||
"workers": 1,
|
"workers": 1,
|
||||||
@@ -156,10 +163,16 @@ def init(options, configuration, plugin):
|
|||||||
@plugin.subscribe("*")
|
@plugin.subscribe("*")
|
||||||
def on_any_notification(request, **kwargs):
|
def on_any_notification(request, **kwargs):
|
||||||
plugin.log("Notification: {}".format(kwargs), "debug")
|
plugin.log("Notification: {}".format(kwargs), "debug")
|
||||||
msgq.put(str(kwargs))
|
if request.method == 'shutdown':
|
||||||
|
# A plugin which subscribes to shutdown is expected to exit itself.
|
||||||
|
sys.exit(0)
|
||||||
|
else:
|
||||||
|
msgq.put(str(kwargs))
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
plugin.run()
|
plugin.run()
|
||||||
|
except ValueError as err:
|
||||||
|
plugin.log("Unable to subscribe to all events. Feature available with CLN v23.08 and above: {}".format(err), "warn")
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user