Start server from web ui

This commit is contained in:
Björn Dahlgren 2014-04-06 21:09:52 +00:00
parent 8ed68c0a4d
commit b620ac5d55
7 changed files with 69 additions and 31 deletions

View File

@ -11,15 +11,14 @@ app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
var servers = require('./routes/servers');
app.resource('api/missions', require('./routes/missions'));
app.resource('api/mods', require('./routes/mods'));
app.resource('api/servers', require('./routes/servers'));
app.resource('api/servers', 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/servers/:server/start', servers.start);
app.get('/', function (req, res){
res.sendfile(__dirname + '/public/index.html');

View File

@ -41,15 +41,25 @@ Server.prototype.start = function() {
}
function Manager() {
this.servers = [];
this.serversArr = [];
this.serversHash = {}
};
Manager.prototype.addServer = (function (id, title) {
mods = [];
port = 2302;
var server = new Server(id, title, port, mods)
this.servers.push(server);
this.serversArr.push(server);
this.serversHash[id] = server;
return server;
});
module.exports = Manager;
Manager.prototype.getServer = (function (id) {
return this.serversHash[id];
});
Manager.prototype.getServers = (function () {
return this.serversArr;
});
module.exports = Manager;

View File

@ -1,7 +1,7 @@
define(function (require) {
"use strict";
var $ = require('jquery'),
Backbone = require('backbone'),
LayoutView = require('app/views/layout'),
@ -13,7 +13,7 @@ define(function (require) {
Missions = require('app/collections/missions'),
Mods = require('app/collections/mods'),
Servers = require('app/collections/servers'),
$body = $('body'),
missions = new Missions(),
mods = new Mods(),
@ -23,28 +23,28 @@ define(function (require) {
homeView = new HomeView({servers: servers}),
missionsListView = new MissionsListView({collection: missions}),
modsListView = new ModsListView({collection: mods});
return Backbone.Router.extend({
routes: {
"missions": "missions",
"mods": "mods",
"servers/:id": "server",
"": "home",
},
initialize: function () {
layoutView.navigation.show(navigationView);
missions.fetch();
mods.fetch();
servers.fetch();
},
home: function () {
layoutView.content.show(homeView);
homeView.delegateEvents();
},
missions: function () {
layoutView.content.show(missionsListView);
missionsListView.delegateEvents();
@ -54,11 +54,11 @@ define(function (require) {
layoutView.content.show(modsListView);
modsListView.delegateEvents();
},
server: function () {
layoutView.content.show(new ServerView({model: servers.first()}));
server: function (id) {
layoutView.content.show(new ServerView({model: servers.get(id)}));
}
});
});
});

View File

@ -1,15 +1,32 @@
define(function (require) {
"use strict";
var $ = require('jquery'),
_ = require('underscore'),
Backbone = require('backbone'),
Marionette = require('marionette'),
tpl = require('text!tpl/servers/info.html');
tpl = require('text!tpl/servers/info.html');
return Marionette.ItemView.extend({
template: _.template(tpl),
events: {
"click #start": "start"
},
start: function (event) {
var self = this;
event.preventDefault();
$.ajax({
url: "/api/servers/" + this.model.get('id') + "/start",
type: 'GET',
success: function (resp) {
console.log(resp);
},
error: $.noop
});
},
});
});
});

View File

@ -1 +1,3 @@
<h2><%-title%></h2>
<h2><%-title%></h2>
<a class="btn btn-primary" href="#" id="start">Start</a>

View File

@ -10,4 +10,4 @@
<div class="tab-pane active" id="info"></div>
<div class="tab-pane" id="mods"></div>
<div class="tab-pane" id="settings"></div>
</div>
</div>

View File

@ -5,7 +5,7 @@ var Manager = require('./../manager');
var manager = new Manager();
exports.index = function (req, res){
res.send(manager.servers);
res.send(manager.getServers());
};
exports.create = function (req, res){
@ -14,6 +14,10 @@ exports.create = function (req, res){
res.send(manager.addServer(id, title));
};
exports.show = function (req, res){
res.send(manager.getServer(req.params.server));
};
exports.update = function(req, res){
res.send('update server ' + req.params.server);
};
@ -21,3 +25,9 @@ exports.update = function(req, res){
exports.destroy = function(req, res){
res.send('destroy server ' + req.params.server);
};
exports.start = function (req, res){
var server = manager.getServer(req.params.server);
server.start();
res.send({status:"ok"});
};