forked from hashplex/Lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightningserver.py
71 lines (59 loc) · 1.88 KB
/
lightningserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Run a lightning node.
Set up the flask development server, and install channel,
lightning, and local APIs.
"""
import os
import os.path
import json
import hashlib
from flask import Flask, request, current_app, g
import bitcoin.rpc
from bitcoin.wallet import CBitcoinSecret
import config
from serverutil import requires_auth
import channel
import lightning
import local
app = Flask(__name__) # pylint: disable=invalid-name
@app.before_request
def before_request():
"""Setup g context"""
g.config = current_app.config
g.bit = g.config['bitcoind']
secret = hashlib.sha256(g.config['secret']).digest()
g.seckey = CBitcoinSecret.from_secret_bytes(secret)
g.addr = 'http://localhost:%d/' % int(g.config['port'])
g.logger = current_app.logger
app.register_blueprint(channel.API)
app.register_blueprint(lightning.API)
app.register_blueprint(local.API)
@app.route('/error')
@requires_auth
def error():
"""Raise an error."""
raise Exception("Hello")
@app.route("/get-ip")
def get_my_ip():
"""Return remote_addr."""
return json.dumps({'ip': request.remote_addr}), 200
@app.route('/info')
@requires_auth
def infoweb():
"""Get bitcoind info."""
return str(app.config['bitcoind'].getinfo())
def run(conf):
"""Start the server."""
with open(os.path.join(conf['datadir'], conf['pidfile']), 'w') as pid_file:
pid_file.write(str(os.getpid()))
if conf.getboolean('regtest'):
bitcoin.SelectParams('regtest')
else:
raise Exception("Non-regnet use not supported")
port = conf.getint('port')
app.config['secret'] = b'correct horse battery staple' + bytes(str(port), 'utf8')
app.config.update(conf)
app.config['bitcoind'] = config.bitcoin_proxy(datadir=conf['datadir'])
channel.init(app.config)
lightning.init(app.config)
app.run(port=port, debug=conf.getboolean('debug'), use_reloader=False,
processes=3)