Skip to content

Commit

Permalink
Release v0.5.0 (#73)
Browse files Browse the repository at this point in the history
* merging release v0.5.0
  • Loading branch information
saubyk authored Aug 29, 2021
1 parent 93e7d19 commit 243494f
Show file tree
Hide file tree
Showing 17 changed files with 829 additions and 109 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ The below run time configuration are available, which can be configured either v
- Port - Port for serving the APIs
- Documentation port - Port for serving the swagger documentation
- Protocol - Http protocol for communication with the REST server. Two options are supported `https` and `http`(unencrypted and insecure communication between API server and the app)
- Execution mode - Control for more detailed log info
- Execution mode - Control for more detailed log info. The options supported are `test` and `production`
- Lightning-RPC Path - Configure the path where `lightning-rpc` file is located. It will default to standard lightning path if not configured
- RPC Command - - Enable additional RPC commands for `/rpc` endpoint

#### Option 1: Via Config file `cl-rest-config.json`
Expand All @@ -41,6 +42,7 @@ For running the server, rename the file `sample-cl-rest-config.json` to `cl-rest
- DOCPORT (Default: `4001`)
- PROTOCOL (Default: `https`)
- EXECMODE (Default: `production`)
- LNRPCPATH (Default: ` `)
- RPCCOMMANDS (Default: `["*"]`)

#### Option 2: With the plugin configuration, if used as a plugin
Expand All @@ -49,6 +51,7 @@ If running as a plugin, configure the below options in your c-lightning `config`
- `rest-docport`
- `rest-protocol`
- `rest-execmode`
- `rest-lnrpcpath`
- `rest-rpc`

Defaults are the same as in option # 1 with the exception that `rest-rpc` is a comma separated string.
Expand Down Expand Up @@ -153,6 +156,7 @@ C-Lightning commands covered by this API suite is [here](docs/CLTCommandCoverage
- getfees (/v1/getFees) - `GET`: Returns the routing fee collected by the node
- signmessage (/v1/utility/signMessage) - `POST`: Creates a digital signature of the message using node's secret key
- verifymessage(/v1/utility/verifyMessage) - `GET`: Verifies a signature and the pubkey of the node which signed the message
- decode (/v1/utility/decode) - `GET`: Decodes various invoice strings including BOLT12
### On-Chain fund management
- newaddr (/v1/newaddr) - `GET`: Generate address for recieving on-chain funds
- withdraw (/v1/withdraw) - `POST`: Withdraw on-chain funds to an address
Expand Down Expand Up @@ -185,6 +189,12 @@ C-Lightning commands covered by this API suite is [here](docs/CLTCommandCoverage
- feerates (/v1/network/feeRates) - `GET`: Lookup fee rates on the network, for a given rate style (`perkb` or `perkw`)
- estimatefees (/v1/network/estimateFees) - `GET`: Get the urgent, normal and slow Bitcoin feerates as sat/kVB.

### Offers
- offer (/v1/offers/offer) - `POST`: Create an offer
- listoffers (/v1/offers/listOffers) - `GET`: Returns a list of all the offers on the node
- fetchinvoice (/v1/offers/fetchInvoice) - `POST`: Fetch an invoice for an offer
- disableoffer (v1/offers/disableOffer) - `DEL`: Disables an existing offer, so that it cannot be used for future payments

### RPC
- rpc (/v1/rpc) - `POST`: additional access to RPC comands. Examples of body param for rpc:
#### No param
Expand Down
62 changes: 41 additions & 21 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
const app = require('express')();
const bodyparser = require('body-parser');
let configFile = './cl-rest-config.json';
fs = require('fs');

function prepDataForLogging(msg) {
return typeof msg === 'string' ? msg : JSON.stringify(msg)
}

function configLogger(config) {
return {
log(msg) { if (config.EXECMODE === 'test') config.PLUGIN.log(prepDataForLogging(msg), "info") },
warn(msg) { config.PLUGIN.log(prepDataForLogging(msg), "warn") },
error(msg) { config.PLUGIN.log(prepDataForLogging(msg), "error") }
}
}

if (typeof global.REST_PLUGIN_CONFIG === 'undefined') {
global.logger = console
//Read config file when not running as a plugin
console.log("Reading config file");
let rawconfig = fs.readFileSync(configFile, function (err) {
if (err) {
console.warn("Failed to read config key");
console.error(error);
process.exit(1);
}
});
global.config = JSON.parse(rawconfig);
global.config.PLUGIN = console;
} else {
function pluginMsg(msg) {
return typeof msg === 'string' ? msg : JSON.stringify(msg)
}
global.logger = {
log(msg) {global.REST_PLUGIN_CONFIG.PLUGIN.log(pluginMsg(msg), "info")},
warn(msg) {global.REST_PLUGIN_CONFIG.PLUGIN.log(pluginMsg(msg), "warn")},
error(msg) {global.REST_PLUGIN_CONFIG.PLUGIN.log(pluginMsg(msg), "error")}
}
global.config = global.REST_PLUGIN_CONFIG;
}

global.logger = configLogger(global.config);

//LN_PATH is the path containing lightning-rpc file
global.ln = require('./lightning-client-js')(process.env.LN_PATH);
global.ln = require('./lightning-client-js')(global.config.LNRPCPATH.trim() || process.env.LN_PATH);
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, filePath, macaroon, encodingtype"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, filePath, macaroon, encodingtype"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS"
);
next();
});

//Use declared routes here
Expand All @@ -45,5 +64,6 @@ app.use('/v1/pay', require('./routes/payments'));
app.use('/v1/invoice', require('./routes/invoice'));
app.use('/v1/network', require('./routes/network'));
app.use('/v1/rpc', require('./routes/rpc'));
app.use('/v1/offers', require('./routes/offers'));

module.exports = app;
31 changes: 6 additions & 25 deletions cl-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const app = require('./app');
const docapp = require('./docapp');
const mcrn = require('./utils/bakeMacaroons');
fs = require( 'fs' );
var PORT = 3001;
var DOCPORT = 4001;
var EXECMODE = "production";

const { execSync } = require( 'child_process' );
const execOptions = { encoding: 'utf-8', windowsHide: true };
Expand All @@ -17,24 +14,8 @@ let key = './certs/key.pem';
let certificate = './certs/certificate.pem';
let macaroonFile = './certs/access.macaroon';
let rootKey = './certs/rootKey.key';
let configFile = './cl-rest-config.json';

if (typeof global.REST_PLUGIN_CONFIG === 'undefined') {
//Read config file when not running as a plugin
global.logger.log("Reading config file");
let rawconfig = fs.readFileSync (configFile, function (err){
if (err)
{
global.logger.warn("Failed to read config key");
global.logger.error( error );
process.exit(1);
}
});
global.config = JSON.parse(rawconfig);
} else {
global.config = global.REST_PLUGIN_CONFIG
}
global.logger.log('--- Starting the cl-rest server ---');
global.logger.warn('--- Starting the cl-rest server ---');

if (!config.PORT || !config.DOCPORT || !config.PROTOCOL || !config.EXECMODE)
{
Expand All @@ -43,9 +24,9 @@ if (!config.PORT || !config.DOCPORT || !config.PROTOCOL || !config.EXECMODE)
}

//Set config params
PORT = config.PORT;
EXECMODE = config.EXECMODE;
DOCPORT = config.DOCPORT;
const PORT = config.PORT;
const EXECMODE = config.EXECMODE;
const DOCPORT = config.DOCPORT;

//Create certs folder
try {
Expand Down Expand Up @@ -144,12 +125,12 @@ docserver = require( 'http' ).createServer( docapp );

//Start the server
server.listen(PORT, function() {
global.logger.log('--- cl-rest api server is ready and listening on port: ' + PORT + ' ---');
global.logger.warn('--- cl-rest api server is ready and listening on port: ' + PORT + ' ---');
})

//Start the docserver
docserver.listen(DOCPORT, function() {
global.logger.log('--- cl-rest doc server is ready and listening on port: ' + DOCPORT + ' ---');
global.logger.warn('--- cl-rest doc server is ready and listening on port: ' + DOCPORT + ' ---');
})

exports.closeServer = function(){
Expand Down
Loading

0 comments on commit 243494f

Please sign in to comment.