From 227b3f5e2c7b5f8dc9ff10f1f9840f5953566c3f Mon Sep 17 00:00:00 2001 From: Augusto <49859508+augustocristian@users.noreply.github.com> Date: Sun, 17 Mar 2024 21:27:33 +0100 Subject: [PATCH] Added OpenAPI specification for wiq_0 gateway service (#17) - Included an openapi.yaml file with the v0.2.0 of the specification - Some changes in the gateway.js to create the swagger sever - Added --pull always option to the README --- README.md | 2 +- gatewayservice/gateway-service.js | 17 +++++ gatewayservice/openapi.yaml | 110 ++++++++++++++++++++++++++++++ gatewayservice/package.json | 5 +- 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 gatewayservice/openapi.yaml diff --git a/README.md b/README.md index fb89a4a1..806935e5 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ deploy: wget https://raw.githubusercontent.com/arquisoft/wiq_0/master/docker-compose.yml -O docker-compose.yml wget https://raw.githubusercontent.com/arquisoft/wiq_0/master/.env -O .env docker compose --profile prod down - docker compose --profile prod up -d + docker compose --profile prod up -d --pull always ``` This action uses three secrets that must be configured in the repository: diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 88b84c8f..84d7d8fa 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -2,6 +2,11 @@ const express = require('express'); const axios = require('axios'); const cors = require('cors'); const promBundle = require('express-prom-bundle'); +//libraries required for OpenAPI-Swagger +const swaggerUi = require('swagger-ui-express'); +const fs = require("fs") +const YAML = require('yaml') + const app = express(); const port = 8000; @@ -41,9 +46,21 @@ app.post('/adduser', async (req, res) => { } }); +// Read the OpenAPI YAML file synchronously +const file = fs.readFileSync('./openapi.yaml', 'utf8'); + +// Parse the YAML content into a JavaScript object representing the Swagger document +const swaggerDocument = YAML.parse(file); + +// Serve the Swagger UI documentation at the '/api-doc' endpoint +// This middleware serves the Swagger UI files and sets up the Swagger UI page +// It takes the parsed Swagger document as input +app.use('/api-doc', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); + // Start the gateway service const server = app.listen(port, () => { console.log(`Gateway Service listening at http://localhost:${port}`); }); + module.exports = server diff --git a/gatewayservice/openapi.yaml b/gatewayservice/openapi.yaml new file mode 100644 index 00000000..c253e705 --- /dev/null +++ b/gatewayservice/openapi.yaml @@ -0,0 +1,110 @@ +openapi: 3.0.0 +info: + title: Gatewayservice API + description: Gateway OpenAPI specification. + version: 0.2.0 +servers: + - url: http://localhost:8000 + description: Development server + - url: http://SOMEIP:8000 + description: Production server +paths: + /adduser: + post: + summary: Add a new user to the database. + operationId: addUser + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + username: + type: string + description: User ID. + password: + type: string + description: User password. + responses: + '200': + description: User added successfully. + '400': + description: Failed to add user. + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Error information. + /health: + get: + summary: Check the health status of the service. + operationId: checkHealth + responses: + '200': + description: Service is healthy. + content: + application/json: + schema: + type: object + properties: + status: + type: string + description: Health status. + /login: + post: + summary: Log in to the system. + operationId: loginUser + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + username: + type: string + description: User ID. + password: + type: string + description: User password. + responses: + '200': + description: Login successful. Returns user token, username, and creation date. + content: + application/json: + schema: + type: object + properties: + token: + type: string + description: User token. + username: + type: string + description: Username. + createdAt: + type: string + description: Creation date. + '401': + description: Invalid credentials. + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Invalid Credentials. + '500': + description: Internal server error. + content: + application/json: + schema: + type: object + properties: + error: + type: string + description: Error information. diff --git a/gatewayservice/package.json b/gatewayservice/package.json index fd16c42c..1d5f9df7 100644 --- a/gatewayservice/package.json +++ b/gatewayservice/package.json @@ -21,7 +21,10 @@ "axios": "^1.6.5", "cors": "^2.8.5", "express": "^4.18.2", - "express-prom-bundle": "^7.0.0" + "express-openapi": "^12.1.3", + "express-prom-bundle": "^7.0.0", + "swagger-ui-express": "^5.0.0", + "yaml": "^2.4.1" }, "devDependencies": { "jest": "^29.7.0",