Restructured app into separate files

This commit is contained in:
Björn Dahlgren 2014-04-04 08:37:43 +00:00
parent 35cc081f0e
commit 7b863098a9
6 changed files with 93 additions and 46 deletions

52
main.js
View File

@ -1,12 +1,9 @@
var express = require('express'),
fs = require('fs'),
slug = require('slug');
Resource = require('express-resource');
var config = require('./config'),
Manager = require('./manager');
var config = require('./config');
var app = express();
var manager = new Manager();
app.use(express.logger('dev'));
app.use(express.cookieParser());
@ -14,52 +11,17 @@ app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.get('/api/missions', function (req, res){
var path = config.path + '/mpmissions';
fs.readdir(path, function (err, files) {
if (err) {
res.send(err);
} else {
res.send(files);
}
});
});
app.get('/api/mods', function (req, res){
fs.readdir(config.path, function (err, files) {
if (err) {
res.send(err);
} else {
var mods = files.filter(function (file) {
return file.charAt(0) == "@";
}).map(function (mod) {
return { name: mod }
});
res.send(mods);
}
});
});
app.get('/api/servers', function (req, res){
res.send(manager.servers);
});
app.post('/api/servers', function (req, res){
var title = req.body.title;
var id = slug(title);
res.send(manager.addServer(id, title));
});
app.resource('api/missions', require('./routes/missions'));
app.resource('api/mods', require('./routes/mods'));
app.resource('api/servers', require('./routes/servers'));
app.resource('api/settings', require('./routes/settings'));
app.get('/api/servers/:id/start', function (req, res){
manager.runServer();
res.send({status:"ok"});
});
app.get('/api/settings', function (req, res){
res.send(config);
});
app.get('/*', function (req, res){
app.get('/', function (req, res){
res.sendfile(__dirname + '/public/index.html');
});

View File

@ -5,6 +5,7 @@
"private": true,
"dependencies": {
"express": "3.x",
"slug": "~0.4.0"
"slug": "~0.4.0",
"express-resource": "~1.0.0"
}
}

26
routes/missions.js Normal file
View File

@ -0,0 +1,26 @@
var fs = require('fs');
var config = require('./../config');
exports.index = function(req, res){
var path = config.path + '/mpmissions';
fs.readdir(path, function (err, files) {
if (err) {
res.send(err);
} else {
res.send(files);
}
});
};
exports.create = function(req, res){
res.send('create mission');
};
exports.show = function(req, res){
res.send('download mission ' + req.params.mission);
};
exports.destroy = function(req, res){
res.send('destroy mission ' + req.params.mission);
};

30
routes/mods.js Normal file
View File

@ -0,0 +1,30 @@
var fs = require('fs');
var config = require('./../config');
exports.index = function(req, res){
fs.readdir(config.path, function (err, files) {
if (err) {
res.send(err);
} else {
var mods = files.filter(function (file) {
return file.charAt(0) == "@";
}).map(function (mod) {
return { name: mod }
});
res.send(mods);
}
});
};
exports.create = function(req, res){
res.send('create mod');
};
exports.show = function(req, res){
res.send('show mod ' + req.params.mod);
};
exports.destroy = function(req, res){
res.send('destroy mod ' + req.params.mod);
};

23
routes/servers.js Normal file
View File

@ -0,0 +1,23 @@
var slug = require('slug');
var Manager = require('./../manager');
var manager = new Manager();
exports.index = function (req, res){
res.send(manager.servers);
};
exports.create = function (req, res){
var title = req.body.title;
var id = slug(title);
res.send(manager.addServer(id, title));
};
exports.update = function(req, res){
res.send('update server ' + req.params.server);
};
exports.destroy = function(req, res){
res.send('destroy server ' + req.params.server);
};

5
routes/settings.js Normal file
View File

@ -0,0 +1,5 @@
var config = require('./../config');
exports.index = function (req, res){
res.send(config);
};