Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eksa - entertainme-redis #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
65 changes: 64 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
# entertainme
# Entertainme
This is a demo simple app for implementing redis, and knowing how fast this developed app respond with and without redis

## Movies Server's End Points
| No | Method | End Point | Description |
| -- | ---------|-------------|-----------------------|
| 1 | GET | / | Get all Movies data |
| 2 | POST | / | Post new data |
| 3 | PUT | /:id | Update specific data |
| 4 | DELETE | /:id | Delete specific data |

Access the endpoints by visiting [http://localhost:3001/api/movies](http://localhost:3001/api/movies)

## TV Series Server's End Points
| No | Method | End Point | Description |
| -- | ------ |-------------|-------------------------|
| 1 | GET | / | Get all TV Series data |
| 2 | POST | / | Post new data |
| 3 | PUT | /:id | Update specific data |
| 4 | DELETE | /:id | Delete specific data |

Access the endpoints by visiting [http://localhost:3002/api/tvseries](http://localhost:3002/api/tvseries)

## Entertainme Server's End Points
| No | Method | End Point | Description |
| -- | ------ |---------------|-----------------------------------|
| 1 | GET | / | Get all TV Series and Movies data |
| 2 | GET | /all | Get all TV Series and Movies data |
| 3 | GET | /movies | Get all Movies data |
| 4 | GET | /series | Get all TV Series data |
| 5 | POST | /movies | Post new data movie |
| 6 | POST | /series | Post new data tv-series |
| 7 | PUT | /movies/:id | Update specific data movie |
| 8 | PUT | /series/:id | Update specific data tv-series |
| 9 | DELETE | /movies/:id | Delete specific data movie |
| 10 | DELETE | /series/:id | Delete specific data tv-series |

Access the endpoints by visiting [http://localhost:3000/api/entertainme](http://localhost:3000/api/entertainme)

## Usage
Go to each server folders, **movie**, **tv-series**, and **main**
Run this command on your command line

> npm install
> npm run dev


## Benchmarking Result
And this is the benchmarking table to know how the app run.

| Method | Experiment | Without Redis (ms) | With Redis (ms) |
| ------- | ------------- | ------------------- |-----------------|
| GET | 1 | 72.6ms | 48.4ms |
| GET | 2 | 12.5ms | 6.14ms |
| GET | 3 | 26.1ms | 3.53ms |
| POST | 1 | 20.1ms | 13.7ms |
| POST | 2 | 28.6ms | 10.9ms |
| POST | 3 | 10.4ms | 19.0ms |
| PUT | 1 | 33.5ms | 14.6ms |
| PUT | 2 | 20.5ms | 12.5ms |
| PUT | 3 | 42ms | 17.6ms |
| DELETE | 1 | 15.7ms | 13.2ms |
| DELETE | 2 | 7.84ms | 9.41ms |
| DELETE | 3 | 8.01ms | 8.65ms |
2 changes: 2 additions & 0 deletions server/main/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT = 3000
JWT_KEY = entertainme@2018
21 changes: 21 additions & 0 deletions server/main/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const redis = require('redis')
const clientRedis = redis.createClient()
const cacheRedis = require('./middlewares/chache')

const app = express()

clientRedis.on('ready', (err) => {
err ? console.error('redis client connection error : ', err) : console.log('redis client connection success')
})

app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.use('/api/entertainme', cacheRedis, require('./routes/entertains'))

module.exports = app
90 changes: 90 additions & 0 deletions server/main/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('movie:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port, () => console.log(`Entertainme service listening on port ${port}!`));
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
150 changes: 150 additions & 0 deletions server/main/controllers/entertains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
const axios = require('axios')
const redis = require('redis')
const clientRedis = redis.createClient()

const urlMovie = 'http://localhost:3001/api/movies'
const urlTvseries = 'http://localhost:3002/api/tvseries'

module.exports = {
readAllData: async (req, res) => {
try {
const movies = await axios.get(urlMovie)
const series = await axios.get(urlTvseries)
// console.log('movies', movies)
const data = {
movies: movies.data,
series: series.data
}
// console.log('readAllData', datas)
clientRedis.set('entertainme', JSON.stringify({
message: 'success to get all data movies and tv-series',
data
}), 'EX', 300)

res.status(200).json({
message: 'success to get all data movies and tv-series',
data
})
} catch (err) {
res.status(500).json('an error occured while getting all data: ', err)
}
},

readMoviesData: async (req, res) => {
try {
const movies = await axios.get(urlMovie)
clientRedis.set('entertainme', JSON.stringify(movies.data), 'EX', 300)
res.status(200).json(movies.data)
} catch (err) {
res.status(500).json('an error occured while getting movie data: ', err)
}
},

readTvseriesData: async (req, res) => {
try {
const series = await axios.get(urlTvseries)
clientRedis.set('entertainme', JSON.stringify(series.data), 'EX', 300)
res.status(200).json(series.data)
} catch (err) {
res.status(500).json('an error occured while getting tv-series data: ', err)
}
},

createMovieData: async (req, res) => {
try {
const movie = await axios.post(urlMovie, {
title : req.body.title,
overview : req.body.overview,
poster_path : req.body.poster_path,
popularity : req.body.popularity,
tag : req.body.tag,
status : req.body.status
})

// clientRedis.get('entertainme', function(err, reply) {
// if (reply) {
// let entertainme = JSON.parse(reply)
// entertainme.movies.data.push(movie.data)
// clientRedis.set('entertainme', JSON.stringify(entertainme), 'EX', 300)
// } else {
// clientRedis.set('entertainme', JSON.stringify(movie.data), 'EX', 300)
// }
// })

res.status(200).json(movie.data)
} catch (err) {
res.status(500).json('an error occured while adding movie data: ', err)
}
},

createTvseriesData: async (req, res) => {
try {
const series = await axios.post(urlTvseries, {
title : req.body.title,
overview : req.body.overview,
poster_path : req.body.poster_path,
popularity : req.body.popularity,
tag : req.body.tag,
status : req.body.status
})
res.status(200).json(series.data)
readAllData(req, res)
} catch (err) {
res.status(500).json('an error occured while adding tv-series data: ', err)
}
},

updateMovieData: async (req, res) => {
try {
const movie = await axios.put(`${urlMovie}/${req.params.id}`, {
title : req.body.title,
overview : req.body.overview,
poster_path : req.body.poster_path,
popularity : req.body.popularity,
tag : req.body.tag,
status : req.body.status
})
res.status(200).json(movie.data)
readAllData(req, res)
} catch (err) {
res.status(500).json('an error occured while update movie data: ', err)
}
},

updateTvseriesData: async (req, res) => {
try {
const movie = await axios.put(`${urlTvseries}/${req.params.id}`, {
title : req.body.title,
overview : req.body.overview,
poster_path : req.body.poster_path,
popularity : req.body.popularity,
tag : req.body.tag,
status : req.body.status
})
res.status(200).json(movie.data)
readAllData(req, res)
} catch (err) {
res.status(500).json('an error occured while update tv-series data: ', err)
}
},

deleteMovieData: async (req, res) => {
try {
const movie = await axios.delete(`${urlMovie}/${req.params.id}`)
res.status(200).json({ message: 'success delete record in movies' })
readAllData(req, res)
} catch (err) {
res.status(500).json('an error occured while delete movie data: ', err)
}
},

deleteTvseriesData: async (req, res) => {
try {
const series = await axios.delete(`${urlTvseries}/${req.params.id}`)
res.status(200).json({ message: 'success delete record in tv-series' })
readAllData(req, res)
} catch (err) {
res.status(500).json('an error occured while delete tv-series data: ', err)
}
}
}
17 changes: 17 additions & 0 deletions server/main/middlewares/chache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const redis = require('redis')
const clientRedis = redis.createClient()

module.exports = (req, res, next) => {
clientRedis.get('entertainme', function(err, reply) {
if (err) {
// console.log('middlewares error reply ', err)
next(err)
} else if (!reply) {
// console.log('middlewares not reply ')
next()
} else {
// console.log('middlewares reply ', reply)
res.json(JSON.parse(reply))
}
})
}
1 change: 1 addition & 0 deletions server/main/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading