Skip to content

Commit df2e198

Browse files
author
Allen Wang
committed
initial setup
0 parents  commit df2e198

14 files changed

+1014
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app/node_modules/*

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
redis-sentinel-poc
2+
=====
3+
This repo is for server failover test using redis sentinel with nodejs

Diff for: app/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM node:alpine
2+
RUN mkdir -p /usr/src/app
3+
COPY . /usr/src/app
4+
WORKDIR /usr/src/app
5+
EXPOSE 3000
6+
CMD [ "npm", "start" ]

Diff for: app/app.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var express = require('express');
2+
var path = require('path');
3+
var favicon = require('serve-favicon');
4+
var logger = require('morgan');
5+
var cookieParser = require('cookie-parser');
6+
var bodyParser = require('body-parser');
7+
8+
var redis = require('redis');
9+
// var client = redis.createClient();
10+
11+
var session = require('express-session');
12+
var RedisStore = require('connect-redis')(session);
13+
14+
var app = express();
15+
16+
// view engine setup
17+
app.set('views', path.join(__dirname, 'views'));
18+
app.set('view engine', 'jade');
19+
20+
// uncomment after placing your favicon in /public
21+
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
22+
//app.use(logger('dev'));
23+
app.use(bodyParser.json());
24+
app.use(bodyParser.urlencoded({ extended: false }));
25+
app.use(cookieParser());
26+
27+
app.use(session({
28+
store: new RedisStore({
29+
host: 'redis',
30+
port: 6379,
31+
// client: client,
32+
ttl: 260
33+
}),
34+
secret: 'password',
35+
resave: false,
36+
saveUninitialized: true,
37+
}));
38+
39+
// app.use(session({
40+
// secret: 'keyboard cat',
41+
// resave: false,
42+
// saveUninitialized: true,
43+
// }));
44+
45+
app.use(express.static(path.join(__dirname, 'public')));
46+
47+
48+
app.get('/', function(req, res, next) {
49+
res.redirect('/main');
50+
});
51+
52+
app.get('/main', function(req, res, next) {
53+
if (!req.session.login) return res.redirect('/login');
54+
next();
55+
}, function(req, res, next) {
56+
res.render('index', { title: 'Server1' });
57+
});
58+
59+
app.get('/login', function(req, res, next) {
60+
res.render('login');
61+
});
62+
63+
app.post('/login', function(req, res, next) {
64+
req.session.login = true;
65+
res.redirect('/main');
66+
});
67+
68+
app.get('/logout', function(req, res, next) {
69+
delete req.session.login;
70+
res.redirect('/login');
71+
});
72+
73+
// catch 404 and forward to error handler
74+
app.use(function(req, res, next) {
75+
var err = new Error('Not Found');
76+
err.status = 404;
77+
next(err);
78+
});
79+
80+
// error handler
81+
app.use(function(err, req, res, next) {
82+
// set locals, only providing error in development
83+
res.locals.message = err.message;
84+
res.locals.error = req.app.get('env') === 'development' ? err : {};
85+
86+
// render the error page
87+
res.status(err.status || 500);
88+
res.render('error');
89+
});
90+
91+
module.exports = app;

Diff for: app/bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('test:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

Diff for: app/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "test",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"start": "node ./bin/www"
7+
},
8+
"dependencies": {
9+
"body-parser": "~1.18.2",
10+
"connect-redis": "^3.3.2",
11+
"cookie-parser": "~1.4.3",
12+
"debug": "~2.6.9",
13+
"express": "~4.15.5",
14+
"express-session": "^1.15.6",
15+
"jade": "~1.11.0",
16+
"morgan": "~1.9.0",
17+
"redis": "^2.8.0",
18+
"serve-favicon": "~2.4.5"
19+
}
20+
}

Diff for: app/public/stylesheets/style.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}

Diff for: app/views/error.jade

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends layout
2+
3+
block content
4+
h1= message
5+
h2= error.status
6+
pre #{error.stack}

Diff for: app/views/index.jade

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extends layout
2+
3+
block content
4+
h1= title
5+
p Welcome to #{title}
6+
7+
form(action="/logout", method="get")
8+
input(type="submit",value="logout")

Diff for: app/views/layout.jade

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
doctype html
2+
html
3+
head
4+
title= title
5+
link(rel='stylesheet', href='/stylesheets/style.css')
6+
body
7+
block content

Diff for: app/views/login.jade

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extends layout
2+
3+
block content
4+
h1 Please login first
5+
form(action="/login", method="post")
6+
input(name="username", placeholder="username")
7+
br
8+
input(type="password",name="username", placeholder="password")
9+
br
10+
input(type="submit",value="submit")
11+
12+

0 commit comments

Comments
 (0)