2020-01-14 13:25:13 +01:00
|
|
|
import cfenv from 'cfenv';
|
|
|
|
import express from 'express';
|
|
|
|
import mongoose from 'mongoose';
|
|
|
|
import swagger from 'swagger-ui-express';
|
|
|
|
import yaml from 'yamljs';
|
|
|
|
|
|
|
|
|
|
|
|
// 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}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-04-20 16:17:43 +02:00
|
|
|
|
2020-01-14 13:25:13 +01:00
|
|
|
// 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
|
|
|
|
app.use('/api', swagger.serve, swagger.setup(
|
2020-04-20 16:13:49 +02:00
|
|
|
yaml.load('./oas.yaml'),
|
2020-01-14 13:25:13 +01:00
|
|
|
{
|
|
|
|
defaultModelsExpandDepth: -1,
|
|
|
|
customCss: '.swagger-ui .topbar { display: none }'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// hook up server to port
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Listening on http;//localhost:${port}`);
|
|
|
|
});
|