Skip to content

Commit a2bba0f

Browse files
feat: Auto swagger module integration, improvement and bug fixes, updated npm modules and support of Node LTS 12.16
1 parent c472fe7 commit a2bba0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2288
-5317
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:10.15.3-alpine
1+
FROM node:12.16.1-alpine
22

33
WORKDIR /src
44

README.md

100644100755
+20-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ git clone https://github.com/SystangoTechnologies/Koach.git
5656
* [Babel](https://github.com/babel/babel)
5757
* [ESLint](http://eslint.org/)
5858
* [PM2](https://github.com/Unitech/pm2/)
59-
* [Swagger](https://github.com/swagger-api/)
59+
* [Swagger](https://github.com/SystangoTechnologies/swagger-generator-koa/blob/master/README.md)
6060

6161
## Structure
6262
```
@@ -76,17 +76,31 @@ git clone https://github.com/SystangoTechnologies/Koach.git
7676
├── src # Source code
7777
│ ├── modules # Module-specific controllers
7878
│ │ ├── common # Contains common modules
79-
│ │ │ ├─── home
79+
│ │ │ ├─── home
8080
│ │ │ └─ index.js
8181
│ │ ├── v1 # Version 1 of APIs
8282
│ │ │ ├─ Auth
83-
│ │ │ ├─ User
84-
│ │ │ └─ index.js
83+
│ │ │ ├─ User
84+
│ │ │ └─ index.js
8585
│ │ └─── v2 # Version 2 of APIs
8686
│ │ ├─ Auth
87-
│ │ ├─ User
87+
│ │ ├─ User
8888
│ │ └─ index.js
8989
│ ├── models # Mongoose models
90+
| ├── requestModel
91+
| | ├── v1
92+
| | | ├── auth.js
93+
| | | └── users.js
94+
| | └── v2
95+
| | ├── auth.js
96+
| | └── users.js
97+
| ├── responseModel
98+
| | ├── v1
99+
| | | ├── auth.js
100+
| | | └── users.js
101+
| | └── v2
102+
| | ├── auth.js
103+
| | └── users.js
90104
│ └── middleware # Custom middleware
91105
│ └── validators # Validation middleware
92106
└── test # Unit tests
@@ -109,7 +123,7 @@ Prerequisite For Docker Configuration : Docker and docker compose must be instal
109123
Steps to run app in docker container :
110124
1. CD to project dir
111125
2. Create build using cmd: $ docker-compose build
112-
3. Start the server in daemon thread using cmd: $ docker-compose up -d
126+
3. Start the server in daemon thread using cmd: $ docker-compose up -d
113127
4. Stop the server using cmd : $ docker-compose down
114128

115129
## Documentation

bin/server.js

+49-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import helmet from 'koa-helmet'
1111
// import http2 from 'http2'
1212
// import fs from 'fs'
1313
import config from '../config'
14-
import { errorMiddleware } from '../src/middleware'
14+
import {
15+
errorMiddleware
16+
} from '../src/middleware'
17+
import {
18+
serveSwagger
19+
} from 'swagger-generator-koa'
1520

1621
const app = new Koa()
1722
app.keys = [config.session]
@@ -42,6 +47,20 @@ app.use(bodyParser())
4247
app.use(session())
4348
app.use(errorMiddleware())
4449

50+
// error handler
51+
app.use(async (ctx, next) => {
52+
try {
53+
await next();
54+
} catch (err) {
55+
ctx.status = err.status || err.code || 500;
56+
ctx.body = {
57+
error: err.code,
58+
message: err.message,
59+
errors: err.errors
60+
};
61+
}
62+
});
63+
4564
// Mount static API documents generated by api-generator
4665
app.use(mount('/docs', serve(`${process.cwd()}/docs`)))
4766

@@ -54,16 +73,36 @@ app.use(passport.session())
5473
const modules1 = require('../src/modules/v1')
5574
const modules2 = require('../src/modules/v2')
5675
const common = require('../src/modules/common')
57-
modules1(app)
58-
modules2(app)
59-
common(app)
6076

61-
// Show swagger only if the NODE_ENV is development
62-
console.log('env', process.env.NODE_ENV)
63-
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
64-
console.log('env2', process.env.NODE_ENV)
65-
app.use(mount('/swagger', serve(`${process.cwd()}/swagger`)))
66-
}
77+
Promise.all([modules1(app), modules2(app), common(app)]).then(function (values) {
78+
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
79+
const options = {
80+
title: 'swagger-generator-koa',
81+
version: '1.0.0',
82+
host: 'localhost:3000',
83+
basePath: '/',
84+
schemes: ['http', 'https'],
85+
securityDefinitions: {
86+
Bearer: {
87+
description: 'Example value:- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU5MmQwMGJhNTJjYjJjM',
88+
type: 'apiKey',
89+
name: 'Authorization',
90+
in: 'header'
91+
}
92+
},
93+
security: [{
94+
Bearer: []
95+
}],
96+
defaultSecurity: 'Bearer'
97+
};
98+
serveSwagger(app, '/swagger', options, {
99+
requestModelPath: './src/requestModel',
100+
responseModelPath: './src/responseModel'
101+
});
102+
}
103+
}).catch((error) => {
104+
throw error;
105+
});
67106

68107
// Using http2 to work with http/2 instead of http/1.x
69108
// http2

gulpfile.js

-12
This file was deleted.

0 commit comments

Comments
 (0)