UNPKG

decentralized-internet

Version:

An NPM library of programs to create decentralized web and distributed computing projects

1 lines 1.31 kB
"use strict";const fs=require("fs"),http=require("http"),path=require("path"),bodyParser=require("body-parser"),express=require("express"),flaschenpost=require("flaschenpost"),p2p=require("../../lib/p2p"),serverBase={run:function(e){const s=flaschenpost.getLogger(),r=fs.readFileSync(path.join(__dirname,"..","..","keys","localhost.selfsigned","certificate.pem")),t=fs.readFileSync(path.join(__dirname,"..","..","keys","localhost.selfsigned","privateKey.pem"));process.env.NODE_TLS_REJECT_UNAUTHORIZED="0";const o=p2p.peer({host:"localhost",port:e.p2p.port,privateKey:t,certificate:r,metadata:{host:"localhost",port:e.http.port},wellKnownPeers:[{host:"localhost",port:e.p2p.portJoin}],serviceInterval:e.serviceInterval});o.on("status::*",e=>{s.info("Changed status.",e)}),o.on("environment::successor",e=>{s.info("Changed successor.",{successor:e})}),o.on("environment::predecessor",e=>{s.info("Changed predecessor.",{predecessor:e})}),o.handle.process=function(e,r){s.info("Processing job.",e),r(null,{endpoint:o.self})};const n=express();n.use(bodyParser.json()),n.post("/job",(e,s)=>{o.getPeerFor(e.body.value,(r,t)=>{if(r)return s.sendStatus(500);o.remote(t).run("process",e.body,(e,r)=>{if(e)return s.sendStatus(500);s.send(r)})})}),http.createServer(n).listen(e.http.port)}};module.exports=serverBase;