Skip to content

Commit

Permalink
Refactor server creation, updating and deletion via API
Browse files Browse the repository at this point in the history
Fixes accidental duplicate servers
  • Loading branch information
Dahlgren committed Feb 7, 2022
1 parent 2415302 commit b3d0fa8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
41 changes: 32 additions & 9 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@ var Manager = function (config, logs) {

Manager.prototype = new events.EventEmitter()

Manager.prototype.addServer = function (options) {
Manager.prototype.addServer = function (options, cb) {
if (this.getServer(Server.generateId(options.title))) {
return cb(new Error('Server already exists'))
}

var server = this._addServer(options)
this.save()
return server
this.save(function (err) {
cb(err, server)
})
}

Manager.prototype.removeServer = function (id) {
Manager.prototype.updateServer = function (id, options, cb) {
var server = this.getServer(id)
var anotherServer = this.getServer(Server.generateId(options.title))

if (anotherServer !== null && anotherServer !== server) {
return cb(new Error('Server already exists'))
}

server.update(options)
this.save(function (err) {
cb(err, server)
})
}

Manager.prototype.removeServer = function (id, cb) {
var server = this.serversHash[id]

if (!server) {
Expand All @@ -31,13 +50,14 @@ Manager.prototype.removeServer = function (id) {
if (index > -1) {
this.serversArr.splice(index, 1)
}
this.save()

if (server.pid) {
server.stop()
}

return server
this.save(function (err) {
cb(err, server)
})
}

Manager.prototype._addServer = function (data) {
Expand Down Expand Up @@ -90,7 +110,7 @@ Manager.prototype.load = function () {
})
}

Manager.prototype.save = function () {
Manager.prototype.save = function (cb) {
var data = []
var self = this

Expand Down Expand Up @@ -128,9 +148,12 @@ Manager.prototype.save = function () {
fs.writeFile(filePath, JSON.stringify(data), function (err) {
if (err) {
console.error('Manager save error: ' + err)
} else {
self.emit('servers')
cb(err)
return
}

self.emit('servers')
cb()
})
}

Expand Down
12 changes: 7 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ var Server = function (config, logs, options) {
this.update(options)
}

Server.generateId = function (title) {
return slugify(title)
.replace(/[(|)]/g, '')
.replace(/\./g, '-')
}

Server.prototype = new events.EventEmitter()

Server.prototype.createServerTitle = function (title) {
Expand All @@ -37,10 +43,6 @@ Server.prototype.createServerTitle = function (title) {
return title
}

Server.prototype.generateId = function () {
return slugify(this.title).replace(/\./g, '-')
}

Server.prototype.update = function (options) {
this.additionalConfigurationOptions = options.additionalConfigurationOptions
this.admin_password = options.admin_password
Expand All @@ -62,7 +64,7 @@ Server.prototype.update = function (options) {
this.von = options.von
this.verify_signatures = options.verify_signatures

this.id = this.generateId()
this.id = Server.generateId(this.title)
this.port = parseInt(this.port, 10) // If port is a string then gamedig fails
}

Expand Down
29 changes: 21 additions & 8 deletions routes/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ module.exports = function (manager, mods) {
return
}

var server = manager.addServer(req.body)
res.json(server)
manager.addServer(req.body, function (err, server) {
if (err) {
return res.status(500).send(err)
}

res.status(201).send(server)
})
})

router.get('/:server', function (req, res) {
Expand All @@ -28,15 +33,23 @@ module.exports = function (manager, mods) {
return
}

var server = manager.getServer(req.params.server)
server.update(req.body)
manager.save()
res.json(server)
manager.update(req.params.server, req.body, function (err, server) {
if (err) {
return res.status(500).send(err)
}

res.status(200).send(server)
})
})

router.delete('/:server', function (req, res) {
var server = manager.removeServer(req.params.server)
res.json(server)
manager.removeServer(req.params.server, function (err, server) {
if (err) {
return res.status(500).send(err)
}

res.status(204).send()
})
})

router.post('/:server/start', function (req, res) {
Expand Down
13 changes: 10 additions & 3 deletions test/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ var Server = require('../../lib/server.js')

describe('Server', function () {
describe('generateId()', function () {
it('should include title', function () {
var server = new Server(null, null, { title: 'title.with.lot.of.dots' })
server.generateId().should.eql('title-with-lot-of-dots')
it('should generate id for title with dots', function () {
Server.generateId('title.with.lot.of.dots').should.eql('title-with-lot-of-dots')
})

it('should generate id title with brackets', function () {
Server.generateId('title [with] [lots of] [brackets]').should.eql('title-with-lots-of-brackets')
})

it('should generate id title with parentheses', function () {
Server.generateId('title (with) (lots of) (parentheses)').should.eql('title-with-lots-of-parentheses')
})
})

Expand Down

0 comments on commit b3d0fa8

Please sign in to comment.