Archived
2
This commit is contained in:
vle2fe 2020-01-14 13:25:13 +01:00
parent 4d3e1bd0ac
commit 918e337a2a
9 changed files with 2558 additions and 0 deletions

13
mainfest.yml Normal file
View File

@ -0,0 +1,13 @@
---
applications:
- name: digital-fingerprint-of-plastics-api
instances: 1
memory: 256M
stack: cflinuxfs3
buildpacks:
- nodejs_buildpack
env:
NODE_ENV: production
OPTIMIZE_MEMORY: true
services:
- dfopdb

53
oas.yml Normal file
View File

@ -0,0 +1,53 @@
openapi: 3.0.2
info:
title: Digital fingerprint of plastics - API
version: 1.0.0
description: Gives access to the project database
servers:
- url: http://localhost:3000
description: local server
- url: https://digital-fingerprint-of-plastics-api.apps.de1.bosch-iot-cloud.com/
description: server on the BIC
tags:
- name: /
paths:
/:
get:
summary: Root method
tags:
- /
responses:
200:
description: Server is working
content:
application/json:
schema:
properties:
message:
type: string
example: 'API server up and running!'
500:
$ref: '#/components/responses/500'
components:
responses:
500:
description: Internal server error
content:
application/json:
schema:
properties:
message:
type: string
example: 'Internal server error'
error:
type: boolean
example: true

2348
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "dfop-api",
"version": "1.0.0",
"description": "API for the digital fingerprint of plastics mongodb",
"main": "index.js",
"scripts": {
"test": "mocha dist/**/**.spec.js",
"start": "tsc && node dist/index.js",
"dev": "nodemon -e ts --exec \"npm run start\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^13.1.6",
"cfenv": "^1.2.2",
"express": "^4.17.1",
"mongoose": "^5.8.7",
"swagger-ui-express": "^4.1.2",
"tslint": "^5.20.1",
"typescript": "^3.7.4",
"yamljs": "^0.3.0"
},
"devDependencies": {
"mocha": "^7.0.0",
"nodemon": "^2.0.2",
"should": "^13.2.3",
"supertest": "^4.0.2"
}
}

58
src/index.ts Normal file
View File

@ -0,0 +1,58 @@
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}`);
});

19
src/routes/root.spec.ts Normal file
View File

@ -0,0 +1,19 @@
import supertest from 'supertest';
import should from 'should/as-function';
let server = supertest.agent('http://localhost:3000');
describe('Testing /', () => {
it('returns the message object', done => {
server
.get('/')
.expect('Content-type', /json/)
.expect(200)
.end(function(err, res) {
should(res.statusCode).equal(200);
should(res.body).be.eql({message: 'API server up and running!'});
done();
});
});
});

9
src/routes/root.ts Normal file
View File

@ -0,0 +1,9 @@
import express from 'express';
const router = express.Router();
router.get('/', (req, res) => {
res.json({message: 'API server up and running!'});
});
module.exports = router;

18
tsconfig.json Normal file
View File

@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "dist",
"sourceMap": true,
"esModuleInterop": true
},
"files": [
"./node_modules/@types/node/index.d.ts"
],
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}

9
tslint.json Normal file
View File

@ -0,0 +1,9 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": { },
"rules": { },
"rulesDirectory": []
}