Skip to content

Commit d0b7d07

Browse files
committed
Adding user system
1 parent b6a59dd commit d0b7d07

19 files changed

+417
-63
lines changed

app.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ app.set('views', __dirname + '/views');
1717
app.set('view engine', 'ejs');
1818
app.set('env', config.env);
1919

20+
var Knex = require('knex');
21+
var db = Knex.initialize({
22+
client: 'mysql',
23+
connection: {
24+
host : config.db.host,
25+
user : config.db.user,
26+
password : config.db.pass,
27+
database : config.db.name,
28+
charset : 'utf8'
29+
}
30+
});
31+
2032
/**
2133
* Set up Middleware
2234
*/
2335
app.use(express.favicon());
2436
app.use(express.logger('dev'));
2537
app.use(express.bodyParser());
2638
app.use(express.methodOverride());
39+
app.use(express.cookieParser());
40+
app.use(express.session({secret: 'DU2787GpjHDGGe72ZDBjh23jkDFs'}));
2741
app.use(app.router);
2842
app.use(require('stylus').middleware(__dirname + '/public'));
2943
app.use(express.static(path.join(__dirname, 'public')));
@@ -41,7 +55,8 @@ var supervisordapi = require('supervisord');
4155
var routes = require('./routes')({
4256
'app': app,
4357
'config': config,
44-
'supervisordapi': supervisordapi
58+
'supervisordapi': supervisordapi,
59+
'db': db
4560
});
4661

4762
/**

config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
var config = {};
22

3+
// Db
4+
config.db = {
5+
host: 'localhost',
6+
user: 'root',
7+
pass: '',
8+
name: 'nodervisor',
9+
};
10+
311
// Application
412
config.port = 3000;
513
config.settingsFile = 'settings.json';
6-
config.env = 'development';
14+
config.env = 'production';
715

816
// Read and write settings
917
config.readSettings = function(){

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"ejs": "*",
1111
"stylus": "*",
1212
"supervisord": "*",
13-
"async": "*"
13+
"async": "*",
14+
"mysql": "*",
15+
"knex": "*",
16+
"bcrypt": "*"
1417
}
1518
}

public/css/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ nav a:hover {
213213

214214
nav a {
215215
float: left;
216-
width: 27%;
216+
width: 20%;
217217
margin: 0 1.7%;
218218
padding: 12px 2%;
219219
margin-bottom: 0;

public/partials/hosts-listing.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for (state in hostSummary) {
3737
var hostDisplay = '';
3838
if (hostCollapsed == 'down') { hostDisplay = 'display: none;' }
3939
%>
40-
<table class="table table-hover table-bordered" style="<%= hostDisplay %>" id="<%= hostName %>">
40+
<table class="table table-hover table-striped" style="<%= hostDisplay %>" id="<%= hostName %>">
4141
<tr>
4242
<th style="width: 50%;">Process Name</th>
4343
<th style="width: 10%;">State</th>

routes/ajax_supervisorctl.js

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,43 @@ exports.ajax_supervisorctl = function(params) {
77
var supervisordapi = params.supervisordapi;
88

99
return function(req, res) {
10-
var querystring = require('querystring');
11-
var host = req.param('host');
12-
var process = req.param('process');
13-
var action = req.param('action');
1410

15-
if (config.settings.hosts[host] !== undefined) {
16-
var supclient = supervisordapi.connect(config.settings.hosts[host].host);
17-
switch (action) {
18-
case 'stop': {
19-
supclient.stopProcessGroup(process, function(){
20-
res.send({result: 'success'});
21-
});
22-
}
23-
break;
24-
case 'start': {
25-
supclient.startProcess(process, function(){
26-
res.send({result: 'success'});
27-
});
28-
}
29-
break;
30-
case 'restartAll': {
31-
supclient.stopAllProcesses(true, function(){
32-
supclient.startAllProcesses(true, function(){
11+
if (!req.session.loggedIn) {
12+
res.send({error: 'Not logged in'});
13+
} else {
14+
15+
var querystring = require('querystring');
16+
var host = req.param('host');
17+
var process = req.param('process');
18+
var action = req.param('action');
19+
20+
if (config.settings.hosts[host] !== undefined) {
21+
var supclient = supervisordapi.connect(config.settings.hosts[host].host);
22+
switch (action) {
23+
case 'stop': {
24+
supclient.stopProcessGroup(process, function(){
3325
res.send({result: 'success'});
3426
});
35-
});
27+
}
28+
break;
29+
case 'start': {
30+
supclient.startProcess(process, function(){
31+
res.send({result: 'success'});
32+
});
33+
}
34+
break;
35+
case 'restartAll': {
36+
supclient.stopAllProcesses(true, function(){
37+
supclient.startAllProcesses(true, function(){
38+
res.send({result: 'success'});
39+
});
40+
});
41+
}
42+
break;
3643
}
37-
break;
44+
} else {
45+
res.send({result: 'error', message: 'Host not found'});
3846
}
39-
} else {
40-
res.send({result: 'error', message: 'Host not found'});
4147
}
4248
};
4349
};

routes/ajax_supervisord.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,30 @@ exports.ajax_supervisord = function(params) {
77
var supervisordapi = params.supervisordapi;
88
var async = require('async');
99
return function(req, res) {
10+
11+
if (!req.session.loggedIn) {
12+
res.send({error: 'Not logged in'});
13+
} else {
1014

11-
var supervisords = {};
12-
var hosts = [];
13-
for (var hostName in config.settings.hosts) {
14-
hosts.push(config.settings.hosts[hostName]);
15-
}
16-
async.each(hosts, function(host, callback){
17-
var supclient = supervisordapi.connect(host.host);
18-
var processinfo = supclient.getAllProcessInfo(function(err, result){
19-
if (err === null) {
20-
supervisords[host.name] = result;
21-
return callback();
22-
} else {
23-
supervisords[host.name] = err;
24-
return callback();
25-
}
15+
var supervisords = {};
16+
var hosts = [];
17+
for (var hostName in config.settings.hosts) {
18+
hosts.push(config.settings.hosts[hostName]);
19+
}
20+
async.each(hosts, function(host, callback){
21+
var supclient = supervisordapi.connect(host.host);
22+
var processinfo = supclient.getAllProcessInfo(function(err, result){
23+
if (err === null) {
24+
supervisords[host.name] = result;
25+
return callback();
26+
} else {
27+
supervisords[host.name] = err;
28+
return callback();
29+
}
30+
});
31+
}, function(err){
32+
res.send(supervisords);
2633
});
27-
}, function(err){
28-
res.send(supervisords);
29-
});
34+
}
3035
};
3136
};

routes/settings.js renamed to routes/hosts.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
* GET/POST settings page
33
*/
44

5-
exports.settings = function(params) {
5+
exports.hosts = function(params) {
66
var config = params.config;
77
return function(req, res) {
8+
9+
if (!req.session.loggedIn) {
10+
res.redirect('/login');
11+
}
12+
813
var fs = require('fs');
914
var saved = false;
1015
var settings = params.config.settings;
@@ -33,8 +38,8 @@ exports.settings = function(params) {
3338
saved = true;
3439
}
3540

36-
res.render('settings', {
37-
title: 'Nodervisor - Settings',
41+
res.render('hosts', {
42+
title: 'Nodervisor - Hosts',
3843
settings: settings,
3944
saved: saved
4045
});

routes/index.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,34 @@ function routes(params) {
2323
* Link routes to items in routes array
2424
*/
2525

26-
// Downloads page
26+
// Home page
2727
app.get('/', routes['supervisord']());
2828
app.get('/ajax/supervisord', routes['ajax_supervisord'](params));
2929

30-
// Setup page
31-
app.get('/settings', routes['settings'](params));
32-
app.post('/settings', routes['settings'](params));
30+
// Hosts page
31+
app.get('/hosts', routes['hosts'](params));
32+
app.post('/hosts', routes['hosts'](params));
3333

34-
// Download Control Page
34+
// Users page
35+
app.get('/users', routes['users'](params));
36+
app.post('/users', routes['users'](params));
37+
38+
// New User page
39+
/*
40+
app.get('/user/new', routes['users'](params));
41+
app.post('/user/new', routes['users'](params)); */
42+
// User edit page
43+
app.get('/user/:idUser', routes['users'](params));
44+
app.post('/user/:idUser', routes['users'](params));
45+
46+
// Login page
47+
app.get('/login', routes['login'](params));
48+
app.post('/login', routes['login'](params));
49+
50+
// Logout page
51+
app.get('/logout', routes['logout']());
52+
53+
// Supervisor Control Page
3554
app.get('/ajax/supervisorctl', routes['ajax_supervisorctl'](params));
3655
}
3756

routes/login.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* GET/POST login page
3+
*/
4+
5+
exports.login = function(params) {
6+
return function(req, res) {
7+
8+
if (req.session.loggedIn) {
9+
res.redirect('/');
10+
}
11+
12+
if (req.body.submit !== undefined) {
13+
var email = req.body.email;
14+
params.db('users')
15+
.where('Email', email)
16+
.exec(function(err, user){
17+
var error = 'Password failed';
18+
if (user !== null) {
19+
bcrypt = require('bcrypt');
20+
req.session.loggedIn = bcrypt.compareSync(req.body.password, user[0].Password);
21+
} else {
22+
error = 'Email not found';
23+
}
24+
25+
if (!req.session.loggedIn) {
26+
res.render('login', {
27+
title: 'Nodervisor - Login',
28+
error: error
29+
});
30+
} else {
31+
req.session.user = user[0];
32+
res.redirect('/');
33+
}
34+
});
35+
} else {
36+
res.render('login', {
37+
title: 'Nodervisor - Login',
38+
});
39+
}
40+
};
41+
};

0 commit comments

Comments
 (0)