Browse Source

Replaced express.js with node's http library

doobs
Brandon Cornejo 11 years ago
parent
commit
b16dfae7a0
  1. 80
      lib/api.js

80
lib/api.js

@ -1,7 +1,4 @@
var express = require('express'),
bodyParser = require('body-parser'),
utils = require('./utils.js'),
https = require('https');
var http = require('http');
//---- Construct ----
function API(db, currency, options) {
@ -9,78 +6,61 @@ function API(db, currency, options) {
__self.db = db;
__self.currency = currency;
__self.port = options.port || 9000;
__self.statdir = options.statdir;
__self.check_data = 4;//minutes
__self.first_check = true;
__self.slogan = '';
}
// ---- Methods ----
API.prototype.start = function () {
var __self = this;
__self.srv = express();
__self.srv.set('view engine', 'jade');
__self.srv.set('views', 'web/templates');
__self.srv.use(bodyParser());
__self.srv.use(express.static('./web/public'));
// ---- Helpers ----
__self.srv.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
__self.srv.locals.slicelast = function(value){
return value.slice(0, -1);
};
// ---- Endpoints ----
__self.srv.get('/api/ladder/all', function(req, res){
__self.srv = http.createServer( function(req, res) {
if (req.method == "GET") {
if (req.url == "/api/ladder/all") {
// Returns the entire ladder from top to bottom
sql = 'SELECT * FROM viewers ORDER BY points DESC;';
__self.db.execute(sql, function(rows) {
var data = new Object();
data['viewers'] = rows;
data['total'] = rows.length;
res.send(data);
});
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
});
__self.srv.get('/api/ladder/:numarg([0-9]+)', function(req, res){
// Returns top int(:id) users from the ladder
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.params.numarg+';';
} else if (/^\/api\/ladder\/[0-9]+$/.test(req.url)) {
// Returns top X users from the ladder
sql = 'SELECT * FROM viewers ORDER BY points DESC LIMIT '+req.url.match(/\d+/g)+';';
__self.db.execute(sql, function(rows) {
var data = new Object();
data['viewers'] = rows;
data['total'] = rows.length;
res.send(data);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
});
});
__self.srv.get('/api/bet', function(req, res){
} else if (req.url == "/api/bet") {
// Returns all of the bet data together
data = new Object();
data['status'] = __self.currency.bets_status;
data['board'] = __self.currency.bets_board;
data['viewers'] = __self.currency.bets_viewers;
data['total_viewers'] = __self.currency.bets_viewers.length;
res.send(data);
});
__self.srv.get('/api/bet/status', function(req, res){
// Returns the current bet status
res.send(__self.currency.bets_status);
});
__self.srv.get('/api/bet/board', function(req, res){
// Returns the current bet board
res.send(__self.currency.bets_board);
});
__self.srv.get('/api/bet/viewers', function(req, res){
// Returns the current bet viewers
res.send(__self.currency.bets_viewers);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(data));
} else if (req.url == "/api/bet/status") {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(__self.currency.bets_status));
} else if (req.url == "/api/bet/board") {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(__self.currency.bets_board));
} else if (req.url == "/api/bet/viewers") {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(__self.currency.bets_viewers));
} else {
res.writeHead(404, {'Content-Type': 'application/json'});
res.end("404");
}
}
});
__self.srv.listen(__self.port);
console.log('Started website at '+__self.port);
__self.srv.listen(__self.port, '0.0.0.0');
console.log('Serving api data at '+__self.port);
};

Loading…
Cancel
Save