-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 65813fb
Showing
10 changed files
with
297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
|