-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
186 lines (170 loc) · 5.73 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -----------------------------------------------------------------------------
* Copyright (c) 2023, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* No Patent Rights, Trademark Rights and/or other Intellectual Property
* Rights other than the rights under this license are granted.
* All other rights reserved.
*
* For any other rights, a separate agreement needs to be closed.
*
* For more information please contact:
* Fraunhofer FOKUS
* Kaiserin-Augusta-Allee 31
* 10589 Berlin, Germany
* https://www.fokus.fraunhofer.de/go/fame
* -----------------------------------------------------------------------------
*/
import dotenv from 'dotenv'
dotenv.config()
import axios from 'axios'
import { errHandler } from "clm-core"
import express from 'express'
const swaggerUi = require('swagger-ui-express');
const app = express()
const PORT = process.env.PORT
import swaggerJsdoc from 'swagger-jsdoc'
/**
* @openapi
* components:
* schemas:
* relation:
* type: object
* properties:
* fromType:
* type: string
* description: The type of the node
* default: fromTypeNode
* toType:
* type: string
* description: The type of the target node
* default: toTypeNode
* fromId:
* type: string
* description: The id of the node
* default: fromNodeId
* toId:
* type: string
* description: The id of the target node
* default: toNodeId
* order:
* type: number
* description: The order of the relation. Used for example ordering the enrollments of a group/user
* default: 0
* parameters:
* accessToken:
* name: x-access-token
* in: header
* description: The access token
* required: true
* example: exampleAccessToken
* schema:
* type: string
* securitySchemes:
* bearerAuth:
* type: http
* scheme: bearer
* refreshAuth:
* type: apiKey
* in: header
* name: x-refresh-token
*/
let options: swaggerJsdoc.Options = {
definition: {
openapi: '3.0.0',
info: {
title: 'CLM-OPEN-CORE API',
version: '1.0.0',
description: 'API endpoints the CLM-OPEN-CORE microservices provide',
},
servers: [
{
"url": "{scheme}://{hostname}:{port}{path}",
"description": "Targeting the microservice itself",
"variables": {
"hostname": {
"default": "localhost",
},
"port": {
"default": `${process.env.PORT}`
},
"path": {
"default": ""
},
"scheme": {
"default": "http",
}
}
},
],
security: [{
bearerAuth: [],
}]
},
apis: [
'./src/controllers/*.ts'
]
}
if (process.env.DEPLOY_URL) {
options!.definition!.servers.unshift({
"url": process.env.DEPLOY_URL,
"description": "The production API server"
})
}
const swaggerSpecification: any = swaggerJsdoc(options)
app.use(express.json())
app.use(errHandler);
app.get('/health', (req, res) => {
res.send('OK')
})
app.get('/swagger.json', async (req, res, next) => {
await getRoutes()
return res.json(swaggerSpecification)
})
app.use('/api', swaggerUi.serve, swaggerUi.setup(
null, {
explorer: true, swaggerOptions: {
url:
process.env.DEPLOY_URL ? process.env.DEPLOY_URL + '/swagger.json' :
'http://localhost:' + `${PORT}` + '/swagger.json',
}
}
));
async function getRoutes() {
let OPEN_API_ENDPOINTS
try {
console.log(process.env.OPEN_API_ENDPOINTS)
OPEN_API_ENDPOINTS = JSON.parse(process.env.OPEN_API_ENDPOINTS as string || '[]')
} catch (err) {
console.error(err)
console.error('Error while parsing OPEN_API_ENDPOINTS')
}
for (const url of OPEN_API_ENDPOINTS) {
try {
const response = await axios.get(`${url}`)
const data = response.data
swaggerSpecification.paths = { ...swaggerSpecification?.paths, ...data.paths }
swaggerSpecification.components.schemas = { ...swaggerSpecification?.components?.schemas, ...data.components.schemas }
swaggerSpecification.components.securitySchemes = { ...swaggerSpecification?.components?.securitySchemes, ...data.components.securitySchemes }
swaggerSpecification.components.parameters = { ...swaggerSpecification?.components?.parameters, ...data.components.parameters }
} catch (err: any) {
const code = err?.response?.status || 500
console.error(`Error while fetching swagger.json from microservice '${url}' with status code ${code}`)
}
}
}
app.listen(PORT, () => {
console.log(`Swagger server is running on port ${PORT}`)
})