UNPKG

thingy-sci-base

Version:

A basic express abstraction for service thingies to directly inject post routes and handlers.

120 lines (100 loc) 3.07 kB
// Generated by CoffeeScript 2.7.0 //########################################################### var Pipe, PipeWrap, Server, app, attachSCIFunctions, listenForRequests, mountMiddleWare, oldListen, port, routes; import net from 'net'; //########################################################### //region MonkeyPatch Server Prototype, from https://github.com/rubenv/node-systemd Server = net.Server.prototype; PipeWrap = process.binding('pipe_wrap'); Pipe = PipeWrap.Pipe; oldListen = Server.listen; //########################################################### Server.listen = function() { var backlog, callback, self; self = this; backlog; callback; if (arguments[0] === 'systemd') { if (typeof arguments[1] === 'function') { callback = arguments[1]; } else if (typeof arguments[1] === 'number') { backlog = arguments[1]; callback = arguments[2]; } if (!process.env.LISTEN_FDS || parseInt(process.env.LISTEN_FDS, 10) !== 1) { throw new Error('No or too many file descriptors received.'); } if (callback) { self.once('listening', callback); } if ((PipeWrap.constants != null) && (PipeWrap.constants.SOCKET != null)) { self._handle = new Pipe(PipeWrap.constants.SOCKET); } else { self._handle = new Pipe(); } self._handle.open(3); self._listen2(null, -1, -1, backlog); } else { oldListen.apply(self, arguments); } return self; }; import express from 'express'; //########################################################### //region internalProperties routes = null; port = null; //########################################################### app = express(); app.set("trust proxy", 1); app.disable("etag"); app.use(express.json()); //endregion //################################################################ mountMiddleWare = function(middleWare) { var fun, i, len; if (typeof middleWare === "function") { app.use(middleWare); return; } if (middleWare.length != null) { for (i = 0, len = middleWare.length; i < len; i++) { fun = middleWare[i]; app.use(fun); } return; } }; //########################################################### attachSCIFunctions = function() { var fun, route; for (route in routes) { fun = routes[route]; app.post(`/${route}`, fun); } }; //################################################################ listenForRequests = function() { if (process.env.SOCKETMODE) { app.listen("systemd"); } else { app.listen(port); } }; //########################################################### export var getExpressApp = function() { return app; }; //########################################################### export var prepareAndExpose = function(middleWare, leRoutes, lePort = 3333) { if (typeof leRoutes !== "object") { throw new Error("No routes Object provided!"); } routes = leRoutes; port = process.env.PORT || lePort; if (middleWare != null) { mountMiddleWare(middleWare); } attachSCIFunctions(); listenForRequests(); };