<commit_before>// app/routes.js

const debug = require("debug")("lncliweb:routes");
const logger = require("winston");
const request = require("request");
const graphviz = require("graphviz");
const commandExistsSync = require("command-exists").sync;

const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10;
const DEFAULT_FINAL_CLTV_DELTA = 144;

// expose the routes to our app with module.exports
module.exports = function (app, lightning, db, config) {

        var lightningRPCAdapter = function(methodName, options) {
            return async function(req, res) {

                options = options || {};

                if (options.isLimitedToAuthorizedUser && req.limituser) {
                    return res.sendStatus(403);
                }

                var params = {};
                if (options.preHook) {
                    params = options.preHook(req);
                }

                try {
                    logger.info(methodName, params);
                    let response = await lightning.call(methodName, params);
                    if (options.postHook) {
                        response = options.postHook(req, response);
                    }
                    res.json(response);
                } catch(e) {
                    res.json({ error: e });
                }
            }
        };

	// api ---------------------------------------------------------------------
	app.get("/api/lnd/getnetworkinfo", lightningRPCAdapter("getNetworkInfo"));
	app.post("/api/lnd/getnodeinfo", lightningRPCAdapter("getNodeInfo"));
	app.get("/api/lnd/listpeers", lightningRPCAdapter("listPeers"));
	app.get("/api/lnd/listhannels", lightningRPCAdapter("listChannels"));
	app.get("/api/lnd/listpeers", lightningRPCAdapter("listPeers"));
	app.get("/api/lnd/listchannels", lightningRPCAdapter("listChannels"));
	app.get("/api/lnd/pendingchannels", lightningRPCAdapter("pendingChannels"));
	app.get("/api/lnd/listpayments", lightningRPCAdapter("listPayments"));
	app.get("/api/lnd/listinvoices", lightningRPCAdapter("listInvoices"));
	app.get("/api/lnd/forwardinghistory", lightningRPCAdapter("forwardingHistory"));
	app.get("/api/lnd/walletbalance", lightningRPCAdapter("walletBalance"));
	app.get("/api/lnd/channelbalance", lightningRPCAdapter("channelBalance"));

	app.get("/api/lnd/getinfo", lightningRPCAdapter("getInfo", {
            postHook: (req, response) => {
                if ((!response.uris || response.uris.length === 0) && (config.lndAddress)) {
                    response.uris = [response.identity_pubkey + "@" + config.lndAddress];
                }
                return response;
            }
        }));

	app.get("/api/lnd/connectPeer", lightningRPCAdapter("connectPeer", {
            isLimitedToAuthorizedUser: true,<commit_msg>added documentation for helper functions<commit_after>// app/routes.js

const debug = require("debug")("lncliweb:routes");
const logger = require("winston");
const request = require("request");
const graphviz = require("graphviz");
const commandExistsSync = require("command-exists").sync;

const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10;
const DEFAULT_FINAL_CLTV_DELTA = 144;

// expose the routes to our app with module.exports
module.exports = function (app, lightning, db, config) {

        /*
         * Creates an adapter between Express requests and Lightning gRPC requests via LightningManager.
         *
         * @param {string} methodName - the RPC call to perform on the Lightning service
         * @param {?bool} options.isLimitedToAuthorizedUser - forces request to come from an autorized client
         * @param {?function} options.preHook - if present, calls the function associated with this variable, and feeds
                                                the result as parameters to the RPC call.
         * @param {?function} options.postHook - if present, calls the function associated with this variable, and transforms
                                                 the result from the RPC call. This function must return a valid Object
         */
        var lightningRPCAdapter = function(methodName, options) {
            return async function(req, res) {

                options = options || {};

                // if isLimitedToAuthorizedUser is true, we check if the `limituser` flag
                // is set on the request, and short-circuit the request if the user is not
                // authorized.
                if (options.isLimitedToAuthorizedUser && req.limituser) {
                    return res.sendStatus(403);
                }

                // By default, input parameters are empty. if preHook was defined, we call
                // this and use the result and input parameters
                var params = {};
                if (options.preHook) {
                    params = options.preHook(req);
                }

                try {
                    let response = await lightning.call(methodName, params);

                    // If result needs to be manipulated before it's returned
                    // to the client (because postHook is defined), call postHook
                    // and use the result as payload to return via JSON
                    if (options.postHook) {
                        response = options.postHook(req, response);
                    }
                    res.json(response);
                } catch(e) {
                    res.json({ error: e });
                }
            }
        };

	// api ---------------------------------------------------------------------
	app.get("/api/lnd/getnetworkinfo", lightningRPCAdapter("getNetworkInfo"));
	app.post("/api/lnd/getnodeinfo", lightningRPCAdapter("getNodeInfo"));
	app.get("/api/lnd/listpeers", lightningRPCAdapter("listPeers"));
	app.get("/api/lnd/listhannels", lightningRPCAdapter("listChannels"));
	app.get("/api/lnd/listpeers", lightningRPCAdapter("listPeers"));
	app.get("/api/lnd/listchannels", lightningRPCAdapter("listChannels"));
	app.get("/api/lnd/pendingchannels", lightningRPCAdapter("pendingChannels"));
	app.get("/api/lnd/listpayments", lightningRPCAdapter("listPayments"));
	app.get("/api/lnd/listinvoices", lightningRPCAdapter("listInvoices"));
	app.get("/api/lnd/forwardinghistory", lightningRPCAdapter("forwardingHistory"));
	app.get("/api/lnd/walletbalance", lightningRPCAdapter("walletBalance"));
	app.get("/api/lnd/channelbalance", lightningRPCAdapter("channelBalance"));

	app.get("/api/lnd/getinfo", lightningRPCAdapter("getInfo", {
            postHook: (req, response) => {
                if ((!response.uris || response.uris.length === 0) && (config.lndAddress)) {
                    response.uris = [response.identity_pubkey + "@" + config.lndAddress];
                }
                return response;
            }
        }));

	app.get("/api/lnd/connectpeer", lightningRPCAdapter("connectPeer", {
            isLimitedToAuthorizedUser: true,
{'fraction_non_alphanumeric': 0.14039907460960094, 'fraction_numerical': 0.0026026604973973396, 'mean_word_length': 2.1978733240869164, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '598385', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1754, 'n_words': 398}