Skip to content

Commit

Permalink
chore: add swagger (#19) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReshetovItsMe committed Jul 11, 2023
1 parent 8b80e9e commit bcd82e6
Show file tree
Hide file tree
Showing 5 changed files with 1,531 additions and 1,229 deletions.
13 changes: 10 additions & 3 deletions gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
},
"dependencies": {
"@grpc/grpc-js": "^1.7.3",
"@hapi/hapi": "^20.2.2",
"@hapi/hapi": "^21.3.2",
"@hapi/inert": "^7.1.0",
"@hapi/vision": "^7.0.2",
"google-protobuf": "^3.21.2",
"hapi-pino": "^12.0.1",
"hapi-swagger": "^17.1.0",
"ioredis": "^5.2.3",
"joi": "^17.9.2",
"pino": "^8.7.0",
"pino-pretty": "^9.1.1",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/google-protobuf": "^3.15.6",
"@types/hapi__hapi": "^20.0.13",
"@types/hapi__hapi": "^21.0.0",
"@types/hapi__inert": "^5.2.6",
"@types/hapi__joi": "^17.1.9",
"@types/hapi__vision": "^5.5.4",
"@types/jest": "^29.5.1",
"@types/node": "^20.1.5",
"@types/node": "^20.4.1",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.13.0",
"@typescript-eslint/parser": "^5.0.0",
Expand Down
26 changes: 26 additions & 0 deletions gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import Pino from 'hapi-pino';
import logger from './logger';
import { message } from './routes';
import Pretty from 'pino-pretty';
import HapiSwagger from 'hapi-swagger';
import Inert from '@hapi/inert';
import Vision from '@hapi/vision';
import Package from '../package.json';

const pretty = Pretty({
colorize: true,
Expand All @@ -14,6 +18,28 @@ export const init = async (): Promise<Server> => {
host: process.env.HOST || 'localhost',
});

const swaggerOptions: HapiSwagger.RegisterOptions = {
info: {
title: 'Secret Message Gateway API Documentation',
version: Package.version,
},
};

const plugins: Array<Hapi.ServerRegisterPluginObject<any>> = [
{
plugin: Inert,
},
{
plugin: Vision,
},
{
plugin: HapiSwagger,
options: swaggerOptions,
},
];

await server.register(plugins);

server.route(message);
server.route({
method: '*',
Expand Down
19 changes: 19 additions & 0 deletions gateway/src/routes/message.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { ServerRoute } from '@hapi/hapi';
import Joi from 'joi';
import { messageHandler } from '../handlers';

const messageRoutes: Array<ServerRoute> = [
{
method: 'POST',
path: '/message',
handler: messageHandler.sendMessage,
options: {
description: 'Encrypt message',
notes: 'Return a message id',
tags: ['api'],
validate: {
payload: Joi.object(),
},
},
},
{
method: 'GET',
path: '/message',
handler: messageHandler.getMessage,
options: {
description: 'Decrypt message',
notes: 'Return decrypted message',
tags: ['api'],
validate: {
query: Joi.object({
messageId: Joi.string(),
}),
},
},
},
];

Expand Down
49 changes: 21 additions & 28 deletions gateway/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
{
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es2019",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
]
"compilerOptions": {
"resolveJsonModule": true,
"declaration": true,
"module": "commonjs",
"esModuleInterop": true,
"target": "es2019",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": ["node_modules/*"]
},
"typeRoots": ["node_modules/@types"],
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"lib": ["es2020"]
},
"typeRoots": [
"node_modules/@types"
],
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"lib": [
"es2020"
]
},
"include": [
"src/**/*"
]
"include": ["src/**/*"]
}
Loading

0 comments on commit bcd82e6

Please sign in to comment.