Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/index.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

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}`);
});
// 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(
yaml.load('./oas.yml'),
{
defaultModelsExpandDepth: -1,
customCss: '.swagger-ui .topbar { display: none }'
}
)
);
// hook up server to port
app.listen(port, () => {
console.log(`Listening on http;//localhost:${port}`);
});