Koa ILP
Koa middleware to charge for requests with ILP
Koa-ILP uses HTTP-ILP to charge for requests.
// first, we start with an ordinary Koa appconst Koa = const router = const parser = const app = // next, we import koa-ilp and automagically get a plugin using ilp-plugin. We// construct our ilp middleware from the plugin.const Ilp = const plugin = const ilp = plugin // ENDPOINT 1: Hello // Our first endpoint, '/hello', has a price of 1000 (in base units of our ledger.// by default, ilp-plugin connects us to the XRP testnet so these are XRP drops).// If we were connected to a dollar ledger with a scale of 9, these would be 1000// nano-dollars.const price = 1000 // We use ilp.options to create an options endpoint which returns payment instructions,// enabling clients to fund a token on our server using interledger.routeroptions'/hello' ilpoptions price // Next we use ilp.paid to create an actual paid endpoint. The route's code won't be// run unless the client has paid 1000 XRP drops to us first.router // ENDPOINT 2: Random // Our next endpoint, '/random', charges for random bytes. The price depends on how// many bytes are requested, so the price is a function instead of an integer.const PRICE_PER_RANDOM_BYTE = 20const priceFunction = { return +ctxquerysize || 16 * PRICE_PER_RANDOM_BYTE} // ilp.paid and ilp.options take a priceFunction in place of a static price. Otherwise// this code works the same as the /hello endpoint.routeroptions'/random' ilpoptions price: priceFunction router // ENDPOINT 3: File // Our next endpoint, '/file/:file', charges a user to read a file off our machine.// This depends on file size too, but reading a file is an async operation. We can// define an async price function this time.const fs = const PRICE_PER_BYTE = 10const asyncPriceFunction = async { const stats = await fs return statssize * PRICE_PER_BYTE} // The async price function is passed in in place of the price. Otherwise this code// functions the same as /hello and /random.routeroptions'/file/:file' ilpoptions price: asyncPriceFunction router app