Skip to content
This repository was archived by the owner on Feb 6, 2019. It is now read-only.

Commit 305262a

Browse files
committed
Refactor code
1 parent f5fce52 commit 305262a

File tree

10 files changed

+121
-126
lines changed

10 files changed

+121
-126
lines changed

src/controllers/home.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import swaggerSpec from '../utils/swagger';
2+
3+
/**
4+
* GET /api/swagger.json
5+
*/
6+
export function getSwaggerSpec(req, res) {
7+
res.json(swaggerSpec);
8+
}
9+
10+
11+
/**
12+
* @swagger
13+
* definitions:
14+
* App:
15+
* title: App
16+
* type: object
17+
* properties:
18+
* app:
19+
* type: string
20+
* apiVersion:
21+
* type: string
22+
*/
23+
24+
/**
25+
* @swagger
26+
* /:
27+
* get:
28+
* summary: Get API version
29+
* description: App version
30+
* produces:
31+
* - application/json
32+
* tags:
33+
* - Base
34+
* responses:
35+
* 200:
36+
* description: Application and API version
37+
* schema:
38+
* title: Users
39+
* type: object
40+
* $ref: '#/definitions/App'
41+
*/
42+
43+
export function getAppInfo(req, res) {
44+
res.json({
45+
app: req.app.locals.title,
46+
apiVersion: req.app.locals.version
47+
});
48+
}

src/controllers/status.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
import { Router } from 'express';
21
import * as statusService from '../services/status';
32

4-
const router = Router();
5-
63
/**
7-
* GET /api/status
4+
* Get the latest status of all the services.
5+
*
6+
* @param {Object} req
7+
* @param {Object} res
8+
* @param {Object} next
89
*/
9-
router.get('/', (req, res, next) => {
10+
export function getStatus(req, res, next) {
1011
statusService.fetchLatestStatuses()
11-
.then(data => res.json({ data }))
12-
.catch(err => next(err));
13-
});
14-
15-
16-
/**
17-
* GET /api/status/:id
18-
*/
19-
router.get('/:id', (req, res, next) => {
20-
statusService.getStatus(req.params.id)
21-
.then(data => res.json({ data }))
12+
.then(data => res.json(data))
2213
.catch(err => next(err));
23-
});
14+
}
2415

25-
export default router;

src/db.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/env.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import helmet from 'helmet';
44
import morgan from 'morgan';
55
import express from 'express';
66

7-
import './db';
8-
import './env';
7+
import pkg from '../package';
98
import routes from './routes';
109
import logger from './utils/logger';
1110
import bodyParser from 'body-parser';
@@ -14,14 +13,12 @@ import * as errorHandler from './middlewares/errorHandler';
1413

1514
const app = express();
1615

17-
const APP_PORT = (process.env.NODE_ENV === 'test' ? process.env.TEST_APP_PORT : process.env.APP_PORT) || '3000';
18-
const APP_HOST = process.env.APP_HOST || '0.0.0.0';
16+
const PORT = process.env.PORT || '3000';
1917

20-
app.set('port', APP_PORT);
21-
app.set('host', APP_HOST);
18+
app.set('port', PORT);
2219

23-
app.locals.title = process.env.APP_NAME;
24-
app.locals.version = process.env.APP_VERSION;
20+
app.locals.title = pkg.name;
21+
app.locals.version = pkg.version;
2522

2623
app.use(cors());
2724
app.use(helmet());
@@ -36,8 +33,8 @@ app.use('/api', routes);
3633
app.use(errorHandler.genericErrorHandler);
3734
app.use(errorHandler.notFoundError);
3835

39-
app.listen(app.get('port'), app.get('host'), () => {
40-
logger().info('info', `Server started at http://${app.get('host')}:${app.get('port')}`);
36+
app.listen(app.get('port'), () => {
37+
logger().info(`Server listening is on port ${app.get('port')}`);
4138
});
4239

4340
export default app;

src/models/Status.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/models/StatusLog.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getClient } from '../utils/db';
2+
3+
const db = getClient();
4+
5+
class StatusLog extends db.Model {
6+
7+
get tableName() {
8+
return 'status_logs';
9+
}
10+
11+
get hasTimestamps() {
12+
return true;
13+
}
14+
15+
static fetchLatestStatuses() {
16+
return this
17+
.query(qb => qb.groupBy('name').orderBy('created_at', 'DSC'))
18+
.fetchAll();
19+
}
20+
}
21+
22+
export default StatusLog;

src/routes.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
import { Router } from 'express';
2-
import swaggerSpec from './utils/swagger';
3-
import statusController from './controllers/status';
2+
import * as homeController from './controllers/home';
3+
import * as statusController from './controllers/status';
44

5-
/**
6-
* Contains all API routes for the application.
7-
*/
8-
let router = Router();
5+
const router = Router();
96

10-
/**
11-
* GET /api/swagger.json
12-
*/
13-
router.get('/swagger.json', (req, res) => {
14-
res.json(swaggerSpec);
15-
});
16-
17-
/**
18-
* @swagger
19-
* definitions:
20-
* App:
21-
* title: App
22-
* type: object
23-
* properties:
24-
* app:
25-
* type: string
26-
* apiVersion:
27-
* type: string
28-
*/
29-
30-
/**
31-
* @swagger
32-
* /:
33-
* get:
34-
* summary: Get API version
35-
* description: App version
36-
* produces:
37-
* - application/json
38-
* tags:
39-
* - Base
40-
* responses:
41-
* 200:
42-
* description: Application and API version
43-
* schema:
44-
* title: Users
45-
* type: object
46-
* $ref: '#/definitions/App'
47-
*/
48-
router.get('/', (req, res) => {
49-
res.json({
50-
app: req.app.locals.title,
51-
apiVersion: req.app.locals.version
52-
});
53-
});
54-
55-
router.use('/status', statusController);
7+
router.get('/swagger.json', homeController.getSwaggerSpec);
8+
router.get('/', homeController.getAppInfo);
9+
router.get('/status', statusController.getStatus);
5610

5711
export default router;

src/services/status.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Boom from 'boom';
2-
import Status from '../models/Status';
2+
import StatusLog from '../models/StatusLog';
33

44
/**
5-
* Get all status.
5+
* Get all status logs.
66
*
77
* @return {Promise}
88
*/
99
export function getAllStatus() {
10-
return Status.fetchAll();
10+
return StatusLog.fetchAll();
1111
}
1212

1313
/**
@@ -17,19 +17,18 @@ export function getAllStatus() {
1717
* @return {Promise}
1818
*/
1919
export function fetchLatestStatuses() {
20-
return Status.fetchLatestStatuses();
20+
return StatusLog.fetchLatestStatuses();
2121
}
2222

2323
/**
24-
* Get a service status.
24+
* Get a service status logs.
2525
*
2626
* @param {string|Number} id
2727
* @return {Promise}
2828
*/
2929
export async function getStatus(id) {
3030
try {
31-
32-
let status = await new Status({ id }).fetch();
31+
let status = await new StatusLog({ id }).fetch();
3332

3433
if (!status) {
3534
throw new Boom.notFound('Service not found');

src/utils/db.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import knex from 'knex';
2+
import bookshelf from 'bookshelf';
3+
import * as config from '../config/config';
4+
5+
/**
6+
* Create a new database client.
7+
* Return the same client if it is already created.
8+
*
9+
* @returns {Object}
10+
*/
11+
let db;
12+
13+
export function getClient() {
14+
if (db) {
15+
return db;
16+
}
17+
18+
const dbConfig = config.get().db;
19+
20+
db = bookshelf(knex(dbConfig));
21+
db.plugin(['bookshelf-camelcase']);
22+
23+
return db;
24+
}

0 commit comments

Comments
 (0)