-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
61 lines (45 loc) · 1.62 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as express from "express";
import * as http from 'http';
import * as bodyParser from "body-parser";
import swaggerUi = require('swagger-ui-express');
import cors from 'cors';
import fs = require('fs');
import Routes from "./opencde-api/routes";
class OpenCDEAPI {
public app: express.Application;
private swaggerFile: any =(process.cwd()+"/swagger/openapi.json-Swagger20.json");
private swaggerData: any = fs.readFileSync(this.swaggerFile, 'utf8');
private swaggerDocument = JSON.parse(this.swaggerData);
constructor() {
this.app = express.default();
this.middleware();
this.configure_routes();
const server: http.Server = http.createServer(this.app);
const port: Number = 3000;
server.listen(port, () => {
});
}
private middleware(): void {
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: false }));
// middleware to allow cross-origin requests
this.app.use(cors());
}
private configure_routes(): void {
// @ts-ignore
this.app.get("/", (req, res) => {
res.send("API Works!!!!!");
});
// user route
this.app.use("/cde/0.1", Routes);
// swagger docs
this.app.use('/cde/0.1/docs', swaggerUi.serve,
swaggerUi.setup(this.swaggerDocument));
this.app.use('/cde/0.1/documents/content',express.static(process.cwd()+'/documents/files'))
// handle undefined routes
this.app.use("*", (req, res) => {
res.send("Make sure url is correct!");
});
}
}
new OpenCDEAPI();