Skip to content

Commit

Permalink
Add partial code
Browse files Browse the repository at this point in the history
  • Loading branch information
vs4vijay committed Mar 12, 2020
1 parent 498bd2a commit 4586041
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
```
Redis
- use pool
Tests
Dockerize
CI/CD
Kubernetes
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1"
"express": "^4.17.1",
"ioredis": "4.16.0"
},
"devDependencies": {
"eslint": "^6.6.0",
Expand Down
5 changes: 3 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

const express = require('express');

const { CONFIG } = require('./config');
const { healthCheckRouter } = require('./controllers');
const { CONFIG, REDIS_CONFIG } = require('./config');
const { healthCheckRouter, AppController } = require('./controllers');

const app = express();

Expand All @@ -20,6 +20,7 @@ app.get(['/', '/api'], function(req, res, next) {

// Adding routes
app.use(CONFIG['BASE_PATH'], healthCheckRouter);
app.use(`${CONFIG['BASE_PATH']}/stats`, AppController.stats);

// Adding 404 route
app.get('*', (req, res) => {
Expand Down
8 changes: 7 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const CONFIG = {
TIMESTAMP: new Date().toTimeString()
};

const REDIS_CONFIG = {
HOST: process.env.REDIS_HOST,
PORT: process.env.REDIS_PORT || 6379
}

module.exports = {
CONFIG
CONFIG,
REDIS_CONFIG
};
24 changes: 24 additions & 0 deletions src/controllers/app.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const { CacheService } = require('../services');

class AppController {

constructor() {
this.cacheService = new CacheService();
}

stats(req, res) {
const hits = this.cacheService.get('stats:hits') || 0;
res.json({
success: true,
message: `Stats-Hits: ${hits}`
});
this.cacheService.set('stats', hits + 1);
}

}

module.exports = {
AppController
};
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const { healthCheckRouter } = require('./health-check.controller');
const { AppController } = require('./app.controller');

module.exports = {
healthCheckRouter
Expand Down
35 changes: 35 additions & 0 deletions src/services/cache.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const Redis = require('ioredis');

const { REDIS_CONFIG } = require('../config');

class CacheService {

constructor() {
// TODO: Have connection pooling and IoC
// this.redisConnection = redisConnection;
this.cache = new Redis({
HOST: REDIS_CONFIG.HOST,
PORT: REDIS_CONFIG.PORT
});
}

set(key, value) {
this.cache.set(key, value);
}

get(key) {
this.cache.get(key);
}

inc(key) {
this.cache
.pipeline();
}

}

module.exports = {
CacheService
};
6 changes: 5 additions & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
'use strict';

module.exports = { };
const { CacheService } = require('./cache.service');

module.exports = {
CacheService
};

0 comments on commit 4586041

Please sign in to comment.