/* eslint-disable no-unused-vars */ 'use strict'; // Ref: http://javascript.tutorialhorizon.com/2014/09/20/organizing-your-expressjs-routes-in-separate-files/ // Global variables : Since they are not declared using the var keyword const express = require('express'); require('express-async-errors'); const debug = require('debug')('agm:server'), compression = require('compression'), path = require('path'), fs = require('fs-extra'), http = require('http'), spdy = require('spdy'), // Use Node-spdy for HTTP2 support instead of express's https app = express(), env = require('./helpers/env'), { checkUser } = require('./middlewares/app_validator.js'), { ErrorHandler } = require('./middlewares/error_handler.js'), error = require('./helpers/error'); http.globalAgent.maxSockets = Infinity; const MAX_REQ_BDY_MB = env.MAX_REQ_BDY_MB || '150mb' // Load ENV vars specified in the .env file (within the same folder) debug("Is in Production: ", env.PRODUCTION); app.isProd = env.PRODUCTION; process.setMaxListeners(0); error.registerUnCaughtProcessErrorsHandler(process, path.join(__dirname, 'agm_server.rlog')); require('./helpers/db/connect.js')(); if(env.INV_IMG_VIR_DIR && env.INV_UPLOAD_DIR) { // Map static resources app.use(env.INV_IMG_VIR_DIR, express.static(env.INV_UPLOAD_DIR, { maxAge: 31557600 })); } // for parsing application/x-www-form-urlencoded app.use(express.urlencoded({ limit: MAX_REQ_BDY_MB, extended: true, parameterLimit: 100000 })); // Handle webhook events from Stripe require('./routes/subscription_webhooks')(express, app); function shouldCompress(req, res) { if (req.headers['x-no-compression']) { return false; // don't compress responses with this request header } // fallback to standard filter function return compression.filter(req, res); } // Enable the compression middleware app.use(compression({ filter: shouldCompress })); // Parsers for POST JSON data app.use(express.json({ limit: MAX_REQ_BDY_MB })); // Allow all CORS requests // Ref at http://restlet.com/company/blog/2015/12/15/understanding-and-using-cors app.use(function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Headers', 'Origin, Methods, X-Requested-With, Content-Type, Content-Disposition, Accept'); res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS'); next(); }); async function setupRoutes() { // const resLogger = async (req, res, next) => { // debug('Request:', req.url); // next(); // }; // Handle routes with middelware functions app.use(checkUser/*, resLogger*/); // REGISTER OUR ROUTES require('./routes')(app); app.use(ErrorHandler); app.get('/*', (req, res) => { // console.log(req.path); res.status(404).send({ status: 404, error: 'Not found' }).end(); }); // Avoid logging aborted requests // Ref: https://github.com/nodejs/help/issues/2155 app.use((err, req, res, next) => { if (err && err.code === 'ECONNABORTED') { res.status(400).end(); // Don't process this error any further to avoid its logging } else next(err); }); } /** * Preload some common libs, ussually written in ESM modules that require asynchronously loaded or import.. * instead of using requires in CommonJS ones. */ async function preloadLibs() { let mod = await import('@mickeyjohn/geodesy/utm.js'); app.locals.UTM = mod.default; app.locals.LatLonUTM = mod.LatLon; // More accuracy, for converting from ll to utm app.locals.Dms = mod.Dms; mod = await import('@mickeyjohn/geodesy/latlon-spherical.js'); app.locals.LatLonSP = mod.default; } const port = env.AGM_PORT || '4000'; // Create a HTTPS - HTTP2 server spdy.createServer({ key: fs.readFileSync(env.SSL_KEY), cert: fs.readFileSync(env.SSL_CERT) }, app) .listen(port, async (error) => { const onAppErr = (err) => { debug(error); process.exit(1); } if (error) return onAppErr(error); try { await preloadLibs(); await setupRoutes(); // Start Job Importer queue consumer // const jobQueuer = require('./helpers/job_queue').getInstance(); // jobQueuer.start(); console.log(`Server is running on port ${port}`) debug(`HTTPS-v2 Agmission Server listening on port ${port}`); } catch (error) { onAppErr(error); } });