From 9786d7eff52ac2d83d3660d1e5a09e9c2fcf8bfa Mon Sep 17 00:00:00 2001 From: calzoneman Date: Thu, 25 Apr 2013 11:00:06 -0500 Subject: [PATCH] start working on API --- api.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ server.js | 5 +++ 2 files changed, 101 insertions(+) create mode 100644 api.js diff --git a/api.js b/api.js new file mode 100644 index 00000000..d19dea67 --- /dev/null +++ b/api.js @@ -0,0 +1,96 @@ +/* +The MIT License (MIT) +Copyright (c) 2013 Calvin Montgomery + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +var Server = require("./server.js"); +var Logger = require("./logger.js"); +var apilog = new Logger.Logger("api.log"); + +var jsonHandlers = { + "channeldata": handleChannelData +}; + +function handle(path, req, res) { + var parts = path.split("/"); + var last = parts[parts.length - 1]; + var params = {}; + if(last.indexOf("?") != -1) { + parts[parts.length - 1] = last.substring(0, last.indexOf("?")); + var plist = last.substring(last.indexOf("?") + 1).split("&"); + for(var i = 0; i < plist.length; i++) { + var kv = plist[i].split("="); + if(kv.length != 2) { + res.send(400); + return; + } + params[unescape(kv[0])] = unescape(kv[1]); + } + } + for(var i = 0; i < parts.length; i++) { + parts[i] = unescape(parts[i]); + } + + if(parts.length != 2) { + res.send(400); + return; + } + + if(parts[0] == "json") { + if(!(parts[1] in jsonHandlers)) { + res.end(JSON.stringify({ + error: "Unknown endpoint: " + parts[1] + }, null, 4)); + return; + } + jsonHandlers[parts[1]](params, req, res); + } + else { + res.send(400); + } +} +exports.handle = handle; + +function handleChannelData(params, req, res) { + var cname = params["channel"] || ""; + var data; + if(!cname.match(/^[a-zA-Z0-9]+$/)) { + data = { + error: "Invalid channel name" + }; + } + else { + data = { + name: cname, + loaded: (cname in Server.channels) + }; + + if(data.loaded) { + var chan = Server.channels[cname]; + data.title = chan.media ? chan.media.title : "-"; + data.usercount = chan.users.length; + data.users = []; + for(var i = 0; i < chan.users.length; i++) { + if(chan.users[i].name) { + data.users.push(chan.users[i].name); + } + } + data.chat = []; + for(var i = 0; i < chan.chatbuffer.length; i++) { + data.chat.push(chan.chatbuffer[i]); + } + } + } + + var response = JSON.stringify(data, null, 4); + + res.setHeader("Content-Type", "text/plain"); + res.setHeader("Content-Length", response.length); + res.end(response); +} diff --git a/server.js b/server.js index 8896753d..e157ba88 100644 --- a/server.js +++ b/server.js @@ -17,6 +17,7 @@ var Logger = require("./logger.js"); Logger.syslog.log("Starting CyTube v" + VERSION); var Config = require("./config.js"); var express = require("express"); +var API = require("./api.js"); var app = express(); app.get("/r/:channel(*)", function(req, res, next) { @@ -29,6 +30,10 @@ app.get("/r/:channel(*)", function(req, res, next) { } }); +app.get("/api/:apireq(*)", function(req, res, next) { + API.handle(req.url.substring(5), req, res); +}); + app.get("/:thing(*)", function(req, res, next) { res.sendfile(__dirname + "/www/" + req.params.thing); });