-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
638 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var mongoose = require('mongoose'); | ||
var mongo_url = process.env.MONGOHQ_URL || 'mongodb://localhost/livebots_dev'; | ||
|
||
require('./models/bots'); | ||
require('./models/commands'); | ||
|
||
module.exports = function() { | ||
console.log('CONNECTING TO MONGO'); | ||
mongoose.connect(mongo_url); | ||
var db = mongoose.connection; | ||
|
||
db.on('error', console.error.bind(console, 'connection error:')); | ||
db.once('open', function (){ | ||
console.log('Successfuly connected to mongoDB'); | ||
}); | ||
}; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var botSchema = new mongoose.Schema({ | ||
bot_id: {type: String, unique: true}, | ||
bot_name: String, | ||
bot_key: String, | ||
bot_state: {type: Boolean, default: false}, | ||
bot_visible: Boolean, // After having the user | ||
address: String, | ||
commands: [String], // List of commands available to execute | ||
livefeedurl: String, | ||
photourl: String, | ||
description: String, | ||
}); | ||
|
||
botSchema.statics.findByBotId = function (botId, cb) { | ||
this.find({ bot_id: botId }, cb); | ||
}; | ||
|
||
botSchema.statics.findAll = function (cb) { | ||
this.find({},cb); | ||
}; | ||
|
||
|
||
|
||
|
||
var Bot = module.exports = mongoose.model('Bot', botSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var mongoose = require('mongoose'); | ||
|
||
var botSchema = require('./bots'); | ||
|
||
var commandSchema = module.exports = new mongoose.Schema({ | ||
bot_id: String, | ||
bot_key: String, | ||
issued: { type: Date, default: Date.now, index: true }, | ||
command: String, | ||
consumed: {type: Boolean, default: false} | ||
}); | ||
|
||
commandSchema.statics.findByBotId = function (botId, cb) { | ||
this.find({ bot_id: botId }, cb); | ||
} | ||
|
||
commandSchema.statics.findLast = function (botId, cb) { | ||
// this.find({},cb); | ||
this.find({bot_id: botId}).sort('-issued').limit(1).exec(cb); | ||
|
||
// Person | ||
// .find({ occupation: /host/ }) | ||
// .where('name.last').equals('Ghost') | ||
// .where('age').gt(17).lt(66) | ||
// .where('likes').in(['vaporizing', 'talking']) | ||
// .limit(10) | ||
// .sort('-occupation') | ||
// .select('name occupation') | ||
// .exec(callback); | ||
|
||
}; | ||
|
||
var Command = module.exports = mongoose.model('Command', commandSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var restify = require('restify'); | ||
var joi = require('joi'); | ||
var routes = require('./routes'); | ||
var db = require('./db'); | ||
|
||
exports.init = init; | ||
|
||
function init(server, cb) { | ||
|
||
// joi.settings.allowUnknown = true; | ||
|
||
server.log.level('info'); | ||
|
||
server.use(restify.throttle({ | ||
burst: 120, | ||
rate: 50, | ||
ip: true | ||
})); | ||
server.use(restify.acceptParser(server.acceptable)); | ||
server.use(restify.queryParser()); | ||
server.use(restify.bodyParser()); | ||
server.use(restify.jsonp()); | ||
server.use(restify.gzipResponse()); | ||
|
||
db(); | ||
routes.init(server); | ||
|
||
cb(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var env = require('./env'); | ||
var pkgjson = require('./package.json'); | ||
|
||
var restify = require('restify'); | ||
|
||
var server = exports.server = | ||
restify.createServer({ | ||
name: pkgjson.name, | ||
level: 'info', | ||
stream: process.stdout, | ||
serializers: restify.bunyan.serializers, | ||
}); | ||
|
||
exports.init = init; | ||
|
||
|
||
function init(cb) { | ||
env.init(server, function() { | ||
|
||
/// Listen | ||
|
||
var port = 3000; | ||
|
||
server.listen(port, function() { | ||
var startLog = { | ||
port: port, | ||
version: pkgjson.version, | ||
log_level: server.log.level() | ||
}; | ||
|
||
startLog[pkgjson.name] = 'OK'; | ||
server.log.info(startLog, 'API Server has started'); | ||
if (cb) cb(); | ||
}); | ||
}); | ||
} | ||
|
||
if (require.main == module) { | ||
init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = uuid; | ||
|
||
function uuid() { | ||
return (~~(Math.random() * 1e9)).toString(36) + Date.now(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "api.livebots.cc", | ||
"version": "0.0.0", | ||
"description": "api for the live-nodebots api", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node_modules/.bin/nodemon index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/nko4/live-nodebots" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"nodebots" | ||
], | ||
"author": [ | ||
"David Dias <[email protected]>", | ||
"Francisco Dias <>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nko4/live-nodebots/issues" | ||
}, | ||
"dependencies": { | ||
"restify": "~2.6.0", | ||
"bunyan": "~0.22.0", | ||
"async": "~0.2.9", | ||
"mongoose": "~3.8.0", | ||
"joi": "~2.0.3" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "~0.7.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var joi = require('joi'); | ||
var async = require('async'); | ||
var restify = require('restify'); | ||
var Bot = require('./../../db/models/bots.js'); | ||
var uuid = require('./../../lib/uuid'); | ||
|
||
exports = module.exports = create; | ||
exports.validate = validate; | ||
|
||
/// validate | ||
|
||
function validate(req, res, next) { | ||
var err = joi.validate(req.params, schema); | ||
if (err) res.send(new restify.InvalidArgumentError(err.message)); | ||
else next(); | ||
} | ||
|
||
var schema = { | ||
bot_id: joi.string().required(), | ||
bot_name: joi.string().required(), | ||
commands: joi.array().includes(joi.string()), | ||
address: joi.string(), | ||
description: joi.string(), | ||
photourl: joi.string(), | ||
livefeedurl: joi.string(), | ||
bot_visible: joi.boolean() | ||
}; | ||
|
||
/// create bot | ||
|
||
function create(req, res, next) { | ||
|
||
var bot = {} | ||
|
||
async.series([ | ||
createBot, | ||
saveBot, | ||
], done); | ||
|
||
function createBot(cb) { | ||
bot.bot_id = req.params.bot_id; | ||
bot.bot_name = req.params.bot_name; | ||
bot.bot_key = uuid(); | ||
bot.bot_state = false; | ||
bot.bot_visible = req.params.bot_visible || true; | ||
if (req.params.address) bot.address = req.params.address; | ||
if (req.params.commands) bot.commands = req.params.commands; | ||
if (req.params.description) bot.description = req.params.description; | ||
if (req.params.photourl) bot.photourl = req.params.photourl; | ||
if (req.params.livefeedurl) bot.livefeedurl = req.params.livefeedurl; | ||
|
||
cb(); | ||
} | ||
|
||
function saveBot(cb) { | ||
var newBot = new Bot(bot); | ||
|
||
newBot.save(function (err, res){ | ||
if (err) cb(err); | ||
console.log('bot saved sucessfuly',res); | ||
cb(); | ||
}) | ||
|
||
} | ||
|
||
function done(err) { | ||
if (err) { | ||
res.send(new restify.InvalidArgumentError(err.detail)); | ||
} else res.send(bot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var joi = require('joi'); | ||
var async = require('async'); | ||
var restify = require('restify'); | ||
var Bot = require('./../../db/models/bots.js'); | ||
var assert = require('assert'); | ||
|
||
exports = module.exports = edit; | ||
exports.validate = validate; | ||
|
||
/// validate | ||
|
||
function validate(req, res, next) { | ||
var err = joi.validate(req.params, schema); | ||
if (err) res.send(new restify.InvalidArgumentError(err.message)); | ||
else next(); | ||
} | ||
|
||
var schema = { | ||
current_bot_id: joi.string().required(), | ||
bot_id: joi.string(), | ||
bot_name: joi.string(), | ||
commands: joi.array().includes(joi.string()), | ||
address: joi.string(), | ||
description: joi.string(), | ||
photourl: joi.string(), | ||
livefeedurl: joi.string(), | ||
bot_visible: joi.boolean() | ||
}; | ||
|
||
/// create bot | ||
|
||
function edit(req, res, next) { | ||
var currentBotId = req.params.current_bot_id; | ||
assert(currentBotId, 'must have the current bot ID'); | ||
|
||
var bot; | ||
|
||
async.series([ | ||
getBot, | ||
editBot, | ||
saveBot | ||
], done); | ||
|
||
function getBot(cb) { | ||
Bot.findByBotId(currentBotId, gotBot); | ||
|
||
function gotBot(err, result) { | ||
if (err) cb(err); | ||
bot = result[0]; | ||
cb(); | ||
} | ||
} | ||
|
||
function editBot(cb) { | ||
|
||
if (req.params.bot_id) bot.bot_id = req.params.bot_id; | ||
if (req.params.bot_name) bot.bot_name = req.params.bot_name; | ||
if (req.params.bot_key) bot.bot_key = req.params.bot_key; | ||
if (req.params.bot_state !== null) bot.bot_state = req.params.bot_state; | ||
if (req.params.address) bot.address = req.params.address; | ||
if (req.params.commands) bot.commands = req.params.commands; | ||
if (req.params.livefeedurl) bot.livefeedurl = req.params.livefeedurl; | ||
if (req.params.photourl) bot.photourl = req.params.photourl; | ||
if (req.params.description) bot.description = req.params.description; | ||
cb(); | ||
} | ||
|
||
function saveBot(cb) { | ||
bot.save(function (err, res){ | ||
if (err) cb(err); | ||
console.log('bot edited sucessfuly', res); | ||
cb(); | ||
}); | ||
} | ||
|
||
function done(err) { | ||
if (err) { | ||
res.send(new restify.InvalidArgumentError(err.detail)); | ||
} else res.send(bot); | ||
} | ||
} |
Oops, something went wrong.