From 6a1c1927e2319e0ca27d37f897876bb751047c5b Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Fri, 8 Oct 2021 16:18:14 +0200 Subject: [PATCH] [mirotalksfu] - add swagger --- README.md | 5 +++++ app/api/swagger.yaml | 47 ++++++++++++++++++++++++++++++++++++++++++++ app/src/Server.js | 11 +++++++++++ package.json | 4 +++- 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 app/api/swagger.yaml diff --git a/README.md b/README.md index 777c7a98..df188fa0 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,13 @@ $ docker-compose down ```bash # The response will give you a entrypoint / Room URL for your meeting. $ curl -X POST "http://localhost:3010/api/v1/meeting" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" +$ curl -X POST "https://sfu.mirotalk.org/api/v1/meeting" -H "authorization: mirotalksfu_default_secret" -H "Content-Type: application/json" ``` +## API Documentation + +The API documentation uses [swagger](https://swagger.io/) at https://localhost:3010/api/v1/docs or check it on live [here](https://sfu.mirotalk.org/api/v1/docs). + ## Notes - Run the project on a `Linux or Mac` system as the `mediasoup` installation could have issues on `Windows`. diff --git a/app/api/swagger.yaml b/app/api/swagger.yaml new file mode 100644 index 00000000..7fb99dec --- /dev/null +++ b/app/api/swagger.yaml @@ -0,0 +1,47 @@ +swagger: '2.0' + +info: + title: MiroTalk SFU API + description: API description for external applications that integrates with MiroTalk SFU. + version: 1.0.0 + +basePath: /api/v1 + +schemes: + - https + - http + +paths: + /meeting: + post: + tags: + - 'meeting' + summary: 'Create meeting' + description: 'Create meeting' + consumes: + - 'application/json' + produces: + - 'application/json' + security: + - secretApiKey: [] + responses: + '200': + description: 'Meeting created' + schema: + $ref: '#/definitions/MeetingResponse' + '403': + description: 'Unauthorized!' + +securityDefinitions: + secretApiKey: + type: 'apiKey' + name: 'authorization' + in: 'header' + description: 'Format like this: authorization: {API_KEY_SECRET}' + +definitions: + MeetingResponse: + type: 'object' + properties: + meeting: + type: 'string' diff --git a/app/src/Server.js b/app/src/Server.js index c7835279..bf4942d8 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -14,6 +14,9 @@ const Peer = require('./Peer'); const ServerApi = require('./ServerApi'); const Logger = require('./Logger'); const log = new Logger('Server'); +const yamlJS = require('yamljs'); +const swaggerUi = require('swagger-ui-express'); +const swaggerDocument = yamlJS.load(path.join(__dirname + '/../api/swagger.yaml')); const app = express(); @@ -26,6 +29,9 @@ const httpsServer = https.createServer(options, app); const io = require('socket.io')(httpsServer); const host = 'https://' + 'localhost' + ':' + config.listenPort; // config.listenIp +const apiBasePath = '/api/v1'; // api endpoint path +const api_docs = host + apiBasePath + '/docs'; // api docs + // all mediasoup workers let workers = []; let nextMediasoupWorkerIdx = 0; @@ -97,6 +103,9 @@ app.get('/join/*', (req, res) => { // Api parse body data as json app.use(express.json()); +// api docs +app.use(apiBasePath + '/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); + // request meeting room endpoint app.post(['/api/v1/meeting'], (req, res) => { // check if user was authorized for the api call @@ -138,6 +147,7 @@ async function ngrokStart() { log.debug('Listening on', { server: host, tunnel: tunnel, + api_docs: api_docs, }); } catch (err) { log.error('Ngrok Start error: ', err); @@ -170,6 +180,7 @@ httpsServer.listen(config.listenPort, () => { } log.debug('Listening on', { server: host, + api_docs: api_docs, }); }); diff --git a/package.json b/package.json index 7c164970..fdf10154 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,9 @@ "mediasoup-client": "^3.6.42", "ngrok": "^4.2.2", "socket.io": "^4.2.0", - "uuid": "8.3.2" + "swagger-ui-express": "^4.1.6", + "uuid": "8.3.2", + "yamljs": "^0.3.0" }, "devDependencies": { "node-fetch": "^3.0.0",