Skip to content

Commit

Permalink
feat: Auto swagger module integration, improvement and bug fixes, upd…
Browse files Browse the repository at this point in the history
…ated npm modules and support of Node LTS 12.16
  • Loading branch information
vpatidar-systango committed Mar 16, 2020
1 parent c472fe7 commit a2bba0f
Show file tree
Hide file tree
Showing 41 changed files with 2,288 additions and 5,317 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:10.15.3-alpine
FROM node:12.16.1-alpine

WORKDIR /src

Expand Down
26 changes: 20 additions & 6 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ git clone https://github.com/SystangoTechnologies/Koach.git
* [Babel](https://github.com/babel/babel)
* [ESLint](http://eslint.org/)
* [PM2](https://github.com/Unitech/pm2/)
* [Swagger](https://github.com/swagger-api/)
* [Swagger](https://github.com/SystangoTechnologies/swagger-generator-koa/blob/master/README.md)

## Structure
```
Expand All @@ -76,17 +76,31 @@ git clone https://github.com/SystangoTechnologies/Koach.git
├── src # Source code
│ ├── modules # Module-specific controllers
│ │ ├── common # Contains common modules
│ │ │ ├─── home
│ │ │ ├─── home
│ │ │ └─ index.js
│ │ ├── v1 # Version 1 of APIs
│ │ │ ├─ Auth
│ │ │ ├─ User
│ │ │ └─ index.js
│ │ │ ├─ User
│ │ │ └─ index.js
│ │ └─── v2 # Version 2 of APIs
│ │ ├─ Auth
│ │ ├─ User
│ │ ├─ User
│ │ └─ index.js
│ ├── models # Mongoose models
| ├── requestModel
| | ├── v1
| | | ├── auth.js
| | | └── users.js
| | └── v2
| | ├── auth.js
| | └── users.js
| ├── responseModel
| | ├── v1
| | | ├── auth.js
| | | └── users.js
| | └── v2
| | ├── auth.js
| | └── users.js
│ └── middleware # Custom middleware
│ └── validators # Validation middleware
└── test # Unit tests
Expand All @@ -109,7 +123,7 @@ Prerequisite For Docker Configuration : Docker and docker compose must be instal
Steps to run app in docker container :
1. CD to project dir
2. Create build using cmd: $ docker-compose build
3. Start the server in daemon thread using cmd: $ docker-compose up -d
3. Start the server in daemon thread using cmd: $ docker-compose up -d
4. Stop the server using cmd : $ docker-compose down

## Documentation
Expand Down
59 changes: 49 additions & 10 deletions bin/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import helmet from 'koa-helmet'
// import http2 from 'http2'
// import fs from 'fs'
import config from '../config'
import { errorMiddleware } from '../src/middleware'
import {
errorMiddleware
} from '../src/middleware'
import {
serveSwagger
} from 'swagger-generator-koa'

const app = new Koa()
app.keys = [config.session]
Expand Down Expand Up @@ -42,6 +47,20 @@ app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())

// error handler
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || err.code || 500;
ctx.body = {
error: err.code,
message: err.message,
errors: err.errors
};
}
});

// Mount static API documents generated by api-generator
app.use(mount('/docs', serve(`${process.cwd()}/docs`)))

Expand All @@ -54,16 +73,36 @@ app.use(passport.session())
const modules1 = require('../src/modules/v1')
const modules2 = require('../src/modules/v2')
const common = require('../src/modules/common')
modules1(app)
modules2(app)
common(app)

// Show swagger only if the NODE_ENV is development
console.log('env', process.env.NODE_ENV)
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
console.log('env2', process.env.NODE_ENV)
app.use(mount('/swagger', serve(`${process.cwd()}/swagger`)))
}
Promise.all([modules1(app), modules2(app), common(app)]).then(function (values) {
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
const options = {
title: 'swagger-generator-koa',
version: '1.0.0',
host: 'localhost:3000',
basePath: '/',
schemes: ['http', 'https'],
securityDefinitions: {
Bearer: {
description: 'Example value:- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU5MmQwMGJhNTJjYjJjM',
type: 'apiKey',
name: 'Authorization',
in: 'header'
}
},
security: [{
Bearer: []
}],
defaultSecurity: 'Bearer'
};
serveSwagger(app, '/swagger', options, {
requestModelPath: './src/requestModel',
responseModelPath: './src/responseModel'
});
}
}).catch((error) => {
throw error;
});

// Using http2 to work with http/2 instead of http/1.x
// http2
Expand Down
12 changes: 0 additions & 12 deletions gulpfile.js

This file was deleted.

Loading

0 comments on commit a2bba0f

Please sign in to comment.