Skip to content

Commit 34e30ff

Browse files
Update node modules and added v2 apis
1 parent 91e28b2 commit 34e30ff

File tree

21 files changed

+596
-102
lines changed

21 files changed

+596
-102
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ This project covers basic necessities of most APIs.
1111
* http/2 support for websites and apis
1212
* Doc generation with apidoc
1313
* linting using standard
14+
* Contains two versions of API
1415

1516
Please note, if you are planning to use this boilerplate for creating a web application then the 'https' protocol should be used to access its pages.
1617

1718
Visit `https://localhost:3000/` to access the root page.
1819

1920
## Requirements
20-
* node > v7.8.0
21+
* node > v8.9.4
2122
* MongoDB
2223

2324
## Installation
@@ -62,9 +63,18 @@ git clone https://github.com/SystangoTechnologies/Koach.git
6263
│ ├── index.js # Config entrypoint
6364
│ └── passport.js # Passportjs config of strategies
6465
├── src # Source code
65-
│ ├── modules
66-
│ │ ├── controller.js # Module-specific controllers
67-
│ │ └── router.js # Router definitions for module
66+
│ ├── modules # Module-specific controllers
67+
│ │ ├── common # Contains common modules
68+
│ │ │ ├─── home
69+
│ │ │ └─ index.js
70+
│ │ ├── v1 # Version 1 of APIs
71+
│ │ │ ├─ Auth
72+
│ │ │ ├─ User
73+
│ │ │ └─ index.js
74+
│ │ └─── v2 # Version 2 of APIs
75+
│ │ ├─ Auth
76+
│ │ ├─ User
77+
│ │ └─ index.js
6878
│ ├── models # Mongoose models
6979
│ └── middleware # Custom middleware
7080
│ └── validators # Validation middleware
@@ -88,7 +98,7 @@ Visit [https://localhost:3000/swagger](https://localhost:3000/swagger) to view S
8898

8999
## Performance Comparison
90100
The environment for the test caes are following-
91-
* Node Version: **7.9.0**
101+
* Node Version: **8.9.4**
92102
* Number of Users: **1500**
93103
* Ramp-up Period: **300 seconds**
94104
* Loop Count: **100**
@@ -98,6 +108,7 @@ The environment for the test caes are following-
98108

99109
## Contributors
100110
[Arpit Khandelwal](https://github.com/arpit-systango)
111+
[Anurag Vikram Singh](https://github.com/avsingh-systango)
101112

102113
## License
103114
MIT.

bin/server.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ app.use(passport.initialize())
5151
app.use(passport.session())
5252

5353
// Using module wise routing
54-
const modules = require('../src/modules')
55-
modules(app)
54+
const modules1 = require('../src/modules/v1')
55+
const modules2 = require('../src/modules/v2')
56+
const common = require('../src/modules/common')
57+
modules1(app)
58+
modules2(app)
59+
common(app)
5660

5761
// Show swagger only if the NODE_ENV is development
5862
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'devlopment') {
@@ -61,7 +65,7 @@ if (!process.env.NODE_ENV || process.env.NODE_ENV === 'devlopment') {
6165

6266
// Using http2 to work with http/2 instead of http/1.x
6367
http2
64-
.createServer(options, app.callback())
68+
.createSecureServer(options, app.callback())
6569
.listen(config.port, () => {
6670
console.log(`Server started on ${config.port}`)
6771
})

package.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,48 +32,48 @@
3232
"url": "https://github.com/SystangoTechnologies/Koach.git"
3333
},
3434
"dependencies": {
35-
"apidoc": "^0.17.5",
36-
"babel-core": "^6.5.1",
37-
"babel-polyfill": "^6.5.0",
35+
"apidoc": "^0.17.6",
36+
"babel-core": "^6.26.0",
37+
"babel-polyfill": "^6.26.0",
3838
"babel-preset-es2015-node5": "^1.2.0",
39-
"babel-preset-stage-0": "^6.5.0",
40-
"bcrypt": "^1.0.2",
41-
"eslint-plugin-import": "^2.6.1",
42-
"eslint-plugin-node": "^5.1.0",
43-
"glob": "^7.0.0",
39+
"babel-preset-stage-0": "^6.24.1",
40+
"bcrypt": "^1.0.3",
41+
"eslint-plugin-import": "^2.9.0",
42+
"eslint-plugin-node": "^6.0.0",
43+
"glob": "^7.1.2",
4444
"gulp": "^3.9.1",
45-
"gulp-eslint": "^4.0.0",
46-
"gulp-watch": "^4.3.11",
47-
"http2": "^3.3.6",
48-
"jsonwebtoken": "^7.1.9",
49-
"koa": "^2.2.0",
45+
"gulp-eslint": "^4.0.2",
46+
"gulp-watch": "^5.0.0",
47+
"http2": "^3.3.7",
48+
"jsonwebtoken": "^8.1.1",
49+
"koa": "^2.5.0",
5050
"koa-bodyparser": "^4.2.0",
5151
"koa-convert": "^1.2.0",
52-
"koa-generic-session": "^1.10.1",
53-
"koa-helmet": "^3.1.0",
54-
"koa-logger": "^2.0.0",
52+
"koa-generic-session": "^2.0.1",
53+
"koa-helmet": "^3.3.0",
54+
"koa-logger": "^3.1.0",
5555
"koa-mount": "^3.0.0",
56-
"koa-passport": "^3.0.0",
57-
"koa-router": "^7.0.1",
58-
"koa-static": "^3.0.0",
59-
"mongoose": "^4.4.3",
56+
"koa-passport": "^4.0.1",
57+
"koa-router": "^7.4.0",
58+
"koa-static": "^4.0.2",
59+
"mongoose": "^5.0.6",
6060
"passport-local": "^1.0.0",
61-
"pm2": "^2.5.0",
62-
"require-dir": "^0.3.2"
61+
"pm2": "^2.10.1",
62+
"require-dir": "^1.0.0"
6363
},
6464
"devDependencies": {
65-
"babel-eslint": "^7.2.2",
66-
"babel-register": "^6.5.1",
67-
"chai": "^3.5.0",
68-
"dotenv": "^4.0.0",
69-
"eslint": "^3.4.0",
70-
"eslint-config-standard": "^10.2.1",
71-
"eslint-plugin-promise": "^3.5.0",
65+
"babel-eslint": "^8.2.2",
66+
"babel-register": "^6.26.0",
67+
"chai": "^4.1.2",
68+
"dotenv": "^5.0.0",
69+
"eslint": "^4.18.1",
70+
"eslint-config-standard": "^11.0.0",
71+
"eslint-plugin-promise": "^3.6.0",
7272
"eslint-plugin-standard": "^3.0.1",
7373
"gulp-nodemon": "^2.2.1",
7474
"gulp-yaml": "^1.0.1",
75-
"mocha": "^3.0.2",
76-
"nodemon": "^1.8.1",
75+
"mocha": "^5.0.1",
76+
"nodemon": "^1.15.1",
7777
"supertest": "^3.0.0"
7878
}
7979
}
File renamed without changes.
File renamed without changes.

src/modules/index.js renamed to src/modules/common/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ exports = module.exports = function initModules (app) {
66
if (err) { throw err }
77

88
matches.forEach((mod) => {
9+
console.log(`${mod}/router`)
910
const router = require(`${mod}/router`)
1011

1112
const routes = router.default
@@ -14,6 +15,7 @@ exports = module.exports = function initModules (app) {
1415

1516
routes.forEach((config) => {
1617
const {
18+
1719
method = '',
1820
route = '',
1921
handlers = []

src/modules/v1/auth/controller.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import passport from 'koa-passport'
2+
3+
/**
4+
* @apiDefine TokenError
5+
* @apiError Unauthorized Invalid JWT token
6+
*
7+
* @apiErrorExample {json} Unauthorized-Error:
8+
* HTTP/1.1 401 Unauthorized
9+
* {
10+
* "status": 401,
11+
* "error": "Unauthorized"
12+
* }
13+
*/
14+
15+
/**
16+
* @api {post} /v1/auth Authenticate user
17+
* @apiVersion 1.0.0
18+
* @apiName AuthUser
19+
* @apiGroup Auth
20+
*
21+
* @apiParam {String} username User username.
22+
* @apiParam {String} password User password.
23+
*
24+
* @apiExample Example usage:
25+
* curl -H "Content-Type: application/json" -X POST -d '{ "username": "johndoe, "password": "foo" }' localhost:3000/v1/auth
26+
*
27+
* @apiSuccess {Object} user User object
28+
* @apiSuccess {ObjectId} user._id User id
29+
* @apiSuccess {String} user.name User name
30+
* @apiSuccess {String} user.username User username
31+
* @apiSuccess {String} token Encoded JWT
32+
*
33+
* @apiSuccessExample {json} Success-Response:
34+
* HTTP/1.1 200 OK
35+
* {
36+
* "user": {
37+
* "_id": "56bd1da600a526986cf65c80"
38+
* "username": "johndoe"
39+
* },
40+
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
41+
* }
42+
*
43+
* @apiError Unauthorized Incorrect credentials
44+
*
45+
* @apiErrorExample {json} Error-Response:
46+
* HTTP/1.1 401 Unauthorized
47+
* {
48+
* "status": 401,
49+
* "error": "Unauthorized"
50+
* }
51+
*/
52+
53+
export async function authUser (ctx, next) {
54+
console.log("authUser")
55+
return passport.authenticate('local', (err, user) => {
56+
if (err || !user) {
57+
ctx.throw(401)
58+
}
59+
60+
const token = user.generateToken()
61+
62+
const response = user.toJSON()
63+
64+
delete response.password
65+
66+
ctx.body = {
67+
token,
68+
user: response
69+
}
70+
})(ctx, next)
71+
}
File renamed without changes.

src/modules/v1/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import glob from 'glob'
2+
import Router from 'koa-router'
3+
4+
exports = module.exports = function initModules (app) {
5+
glob(`${__dirname}/*`, { ignore: '**/index.js' }, (err, matches) => {
6+
if (err) { throw err }
7+
8+
matches.forEach((mod) => {
9+
const router = require(`${mod}/router`)
10+
11+
const routes = router.default
12+
const baseUrl = router.baseUrl
13+
const instance = new Router({ prefix: '/v1' + baseUrl })
14+
15+
routes.forEach((config) => {
16+
const {
17+
method = '',
18+
route = '',
19+
handlers = []
20+
} = config
21+
22+
const lastHandler = handlers.pop()
23+
24+
instance[method.toLowerCase()](route, ...handlers, async function (ctx) {
25+
await lastHandler(ctx)
26+
})
27+
28+
app
29+
.use(instance.routes())
30+
.use(instance.allowedMethods())
31+
})
32+
})
33+
})
34+
}

src/modules/users/controller.js renamed to src/modules/v1/users/controller.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import User from '../../models/users'
1+
import User from '../../../models/users'
22

33
/**
4-
* @api {post} /users Create a new user
4+
* @api {post} /v1/users Create a new user
55
* @apiPermission
66
* @apiVersion 1.0.0
77
* @apiName CreateUser
88
* @apiGroup Users
99
*
1010
* @apiExample Example usage:
11-
* curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users
11+
* curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:3000/v1/users
1212
*
1313
* @apiParam {Object} user User object (required)
1414
* @apiParam {String} user.username Username.
@@ -58,14 +58,14 @@ export async function createUser (ctx) {
5858
}
5959

6060
/**
61-
* @api {get} /users Get all users
61+
* @api {get} /v1/users Get all users
6262
* @apiPermission user
6363
* @apiVersion 1.0.0
6464
* @apiName GetUsers
6565
* @apiGroup Users
6666
*
6767
* @apiExample Example usage:
68-
* curl -H "Content-Type: application/json" -X GET localhost:5000/users
68+
* curl -H "Content-Type: application/json" -X GET localhost:3000/v1/users
6969
*
7070
* @apiSuccess {Object[]} users Array of user objects
7171
* @apiSuccess {ObjectId} users._id User id
@@ -90,14 +90,14 @@ export async function getUsers (ctx) {
9090
}
9191

9292
/**
93-
* @api {get} /users/:id Get user by id
93+
* @api {get} /v1/users/:id Get user by id
9494
* @apiPermission user
9595
* @apiVersion 1.0.0
9696
* @apiName GetUser
9797
* @apiGroup Users
9898
*
9999
* @apiExample Example usage:
100-
* curl -H "Content-Type: application/json" -X GET localhost:5000/users/56bd1da600a526986cf65c80
100+
* curl -H "Content-Type: application/json" -X GET localhost:3000/v1/users/56bd1da600a526986cf65c80
101101
*
102102
* @apiSuccess {Object} users User object
103103
* @apiSuccess {ObjectId} users._id User id
@@ -138,14 +138,14 @@ export async function getUser (ctx, next) {
138138
}
139139

140140
/**
141-
* @api {put} /users/:id Update a user
141+
* @api {put} /v1/users/:id Update a user
142142
* @apiPermission
143143
* @apiVersion 1.0.0
144144
* @apiName UpdateUser
145145
* @apiGroup Users
146146
*
147147
* @apiExample Example usage:
148-
* curl -H "Content-Type: application/json" -X PUT -d '{ "user": { "name": "Cool new Name" } }' localhost:5000/users/56bd1da600a526986cf65c80
148+
* curl -H "Content-Type: application/json" -X PUT -d '{ "user": { "name": "Cool new Name" } }' localhost:3000/v1/users/56bd1da600a526986cf65c80
149149
*
150150
* @apiParam {Object} user User object (required)
151151
* @apiParam {String} user.name Name.
@@ -190,14 +190,14 @@ export async function updateUser (ctx) {
190190
}
191191

192192
/**
193-
* @api {delete} /users/:id Delete a user
193+
* @api {delete} /v1/users/:id Delete a user
194194
* @apiPermission
195195
* @apiVersion 1.0.0
196196
* @apiName DeleteUser
197197
* @apiGroup Users
198198
*
199199
* @apiExample Example usage:
200-
* curl -H "Content-Type: application/json" -X DELETE localhost:5000/users/56bd1da600a526986cf65c80
200+
* curl -H "Content-Type: application/json" -X DELETE localhost:3000/v1/users/56bd1da600a526986cf65c80
201201
*
202202
* @apiSuccess {StatusCode} 200
203203
*

0 commit comments

Comments
 (0)