diff --git a/.gitignore b/.gitignore index a7fc5f0..ec9fbe0 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,7 @@ build/Release # Dependency directory # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git -node_modules \ No newline at end of file +node_modules + +# Swap files +*.swp diff --git a/bin/data.js b/bin/data.js new file mode 100644 index 0000000..b97a603 --- /dev/null +++ b/bin/data.js @@ -0,0 +1,38 @@ +// Generated by CoffeeScript 1.10.0 +(function() { + 'use strict'; + var _empty, addData, goodApiKey, + indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + + goodApiKey = function(req) { + var headers; + headers = Object.keys(req.headers); + return indexOf.call(headers, "x-cloudmine-apikey") >= 0; + }; + + _empty = function(obj) { + return Object.keys(obj).length === 0; + }; + + addData = function(req) { + var payload; + payload = req.payload != null ? req.payload : {}; + req.data = { + apikey: req.headers["x-cloudmine-apikey"], + request: { + method: req.method.toUpperCase() + }, + input: payload, + params: _empty(req.query) ? payload : req.query + }; + if (!_empty(payload)) { + return req.data.request["content-type"] = req.mime; + } + }; + + module.exports = { + add: addData, + goodApiKey: goodApiKey + }; + +}).call(this); diff --git a/bin/server.js b/bin/server.js index 5c1639f..a187da1 100644 --- a/bin/server.js +++ b/bin/server.js @@ -1,7 +1,7 @@ // Generated by CoffeeScript 1.10.0 (function() { 'use strict'; - var Hapi, Server, badRequest, join; + var Hapi, Server, badRequest, data, join; Hapi = require('hapi'); @@ -9,6 +9,8 @@ join = require('path').join; + data = require('./data'); + Server = (function() { function Server() { this.server = new Hapi.Server(); @@ -51,11 +53,17 @@ Server.prototype._setupRoutes = function() { return this.server.route({ method: ['PUT', 'POST', 'GET'], - path: '/code/{name}', + path: '/v1/app/{appid}/run/{name}', handler: (function(_this) { return function(req, reply) { var snippet; snippet = _this.requiredFile[req.params.name]; + if (!data.goodApiKey(req)) { + return reply({ + errors: ["API Key invalid"] + }); + } + data.add(req); if (!snippet) { return reply(badRequest('Snippet Not Found!')); } diff --git a/lib/data.coffee b/lib/data.coffee new file mode 100644 index 0000000..0e279f9 --- /dev/null +++ b/lib/data.coffee @@ -0,0 +1,27 @@ +'use strict' +# +# CloudMine, Inc +# 2015 +# + +goodApiKey = (req) -> + headers = Object.keys req.headers + "x-cloudmine-apikey" in headers + +_empty = (obj) -> + Object.keys(obj).length == 0 + +addData = (req) -> + payload = if req.payload? then req.payload else {} + req.data = { + apikey: req.headers["x-cloudmine-apikey"] + request: { + method: req.method.toUpperCase() + } + input: payload + params: if _empty(req.query) then payload else req.query + } + + if not _empty(payload) then req.data.request["content-type"] = req.mime + +module.exports = { add: addData, goodApiKey: goodApiKey } diff --git a/lib/server.coffee b/lib/server.coffee index 4b91279..e398af3 100644 --- a/lib/server.coffee +++ b/lib/server.coffee @@ -7,6 +7,7 @@ Hapi = require 'hapi' {badRequest} = require 'boom' join = require('path').join +data = require './data' class Server @@ -38,9 +39,11 @@ class Server _setupRoutes: -> @server.route method: ['PUT', 'POST', 'GET'] - path: '/code/{name}' + path: '/v1/app/{appid}/run/{name}' handler: (req, reply)=> snippet = @requiredFile[req.params.name] + return reply({errors: ["API Key invalid"]}) unless data.goodApiKey req + data.add(req) return reply(badRequest('Snippet Not Found!')) unless snippet snippet(req, reply)