Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
faiyaz26 committed Aug 25, 2018
0 parents commit 65813fb
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless
Empty file added Dockerfile
Empty file.
10 changes: 10 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:8
WORKDIR /usr/src/app
ADD package.json .

RUN npm install
RUN npm install -g nodemon

EXPOSE 3000

ENTRYPOINT ["nodemon", "server.js"]
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
HOST: process.env.HOST || "0.0.0.0",
PORT: process.env.PORT || 3000,
REDIS_URI: process.env.REDIS_URI,
ENV: process.env.ENV || "dev",
}
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3"
networks:
chat:

services:
# Redis for socket.io message broker
redis:
image: redis
ports:
- 6379:6379

# The application image
app:
depends_on:
- redis
build:
context: .
dockerfile: Dockerfile-dev
volumes:
- .:/usr/src/app
environment:
ENV_NAME: dev
REDIS_URI: redis
ports:
- 3000:3000
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "rtn-k8s",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.16.3",
"socket.io": "^2.1.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1;"
},
"author": "Ahmad Faiyaz",
"license": "MIT"
}
62 changes: 62 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Real Time Notification - K8S</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css"/>
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="/">RTN-K8S</a>
</nav>

<main role="main" class="container" style="margin-top:10%">

<div id="app">
<h2>You are <span class="badge badge-pill badge-danger" v-if="!connection_status">Danger </span>connected to server!</h2>
<div class="row">
<h2>You have <span class="badge badge-pill badge-info">{{ notification_count }}</span> new notifications</h2>
<button class="btn btn-warning" style="margin-left: 15px;" v-on:click="markAllRead">Mark all as read</button>
<button class="btn btn-primary" style="margin-left: 15px;" v-on:click="generateNotification">Generate a notification</button>
</div>

<div v-if="server_hostname" style="margin-top: 20px">
<thead>
<tr>
<th>
Some extra information
</th>
</tr>
</thead>
<table class="table">
<tbody>
<tr>
<td>User Id</td>
<td>{{ user_id }}</td>
</tr>
<tr>
<td>Server hostname</td>
<td>{{ server_hostname }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</main><!-- /.container -->

<script src="main.js"></script>
</body>
</html>
Empty file added public/main.css
Empty file.
45 changes: 45 additions & 0 deletions public/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

var app = new Vue({
el: "#app",
data: {
connection_status: null,
user_id: null,
server_hostname : null,
notification_count : 0,
},
methods: {
markAllRead: function(){
this.notification_count = 0;
},
generateNotification: function(){
axios.post('/notify', {
user_id: this.user_id
}).then(function(data){
toastr["success"]("Notification generated!");
});
},
updateData(data){
if(data.user_id == this.user_id){
return;
}
this.notification_count += 1;
toastr["info"]("from user: " + data.user_id, "New notification!");
}
}
});

var notification_channel = 'notification_channel';
var socket = io();

socket.on('connect', () => {
app.connection_status = socket.connected;
app.user_id = socket.id;
});

socket.on('initial_data', function(data){
app.server_hostname = data.server_hostname;
});

socket.on(notification_channel, function(data){
app.updateData(data);
});
60 changes: 60 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Setup basic express server
var os = require('os')
var bodyParser = require('body-parser');
var express = require('express');
var path = require('path');

var config = require('./config');

var app = express();
app.use(bodyParser.json()); // for parsing application/json


var server = require('http').createServer(app);
var io = require('socket.io')(server);

// for serving the static client files
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
res.sendfile(__dirname + 'public/index.html');
});


var notification_channel = 'notification_channel';

io.on('connection', function(socket) { // new user connected
var data = {
"server_hostname": os.hostname()
}

socket.emit("initial_data", data);
});

app.post('/notify', function(req, res){
console.log("Got a request from client to notify");

var dt = new Date();
var utcDate = dt.toUTCString();

var emit_data = {
user_id: req.body.user_id,
server_timestamp: utcDate,
}
io.sockets.emit(notification_channel,emit_data);

res.send("done!");
});


server.listen(config.PORT, config.HOST, function() {
console.log('Server listening at %s:%d', config.HOST, config.PORT);
});

process.on('SIGTERM', function() {
console.log('Received SIGTERM, shutting down server');
server.close();
process.exit(0);
});


0 comments on commit 65813fb

Please sign in to comment.