Skip to content

Commit

Permalink
Added OpenAPI specification for wiq_0 gateway service (#17)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
augustocristian authored Mar 17, 2024
1 parent 304dd15 commit 227b3f5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
110 changes: 110 additions & 0 deletions gatewayservice/openapi.yaml
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion gatewayservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 227b3f5

Please sign in to comment.