import cfenv from 'cfenv'; import express from 'express'; import mongoose from 'mongoose'; import swagger from 'swagger-ui-express'; import jsonRefParser, {JSONSchema} from '@apidevtools/json-schema-ref-parser'; // tell if server is running in debug or production environment console.log(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : '===== DEVELOPMENT ====='); // get mongodb address from server, otherwise set to localhost let connectionString: string = ""; if(process.env.NODE_ENV === 'production') { let services = cfenv.getAppEnv().getServices(); for (let service in services) { if(services[service].tags.indexOf("mongodb") >= 0) { connectionString = services[service]["credentials"].uri; } } } else { connectionString = 'mongodb://localhost/dfopdb'; } mongoose.connect(connectionString, {useNewUrlParser: true, useUnifiedTopology: true}); // connect to mongodb let db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', () => { console.log(`Connected to ${connectionString}`); }); // create Express app const app = express(); app.disable('x-powered-by'); // get port from environment, defaults to 3000 const port = process.env.PORT || 3000; // require routes app.use('/', require('./routes/root')); // Swagger UI let oasDoc: JSONSchema = {}; jsonRefParser.bundle('oas/oas.yaml', (err, doc) => { if(err) throw err; oasDoc = doc; oasDoc.paths = oasDoc.paths.allOf.reduce((s, e) => Object.assign(s, e)); swagger.setup(oasDoc, {defaultModelsExpandDepth: -1, customCss: '.swagger-ui .topbar { display: none }'}); }); app.use('/api', swagger.serve, swagger.setup(oasDoc, {defaultModelsExpandDepth: -1, customCss: '.swagger-ui .topbar { display: none }'})); // hook up server to port app.listen(port, () => { console.log(`Listening on http;//localhost:${port}`); });