From 1325a510fcdbd6fa4bb4ab3f9af77e3e94b7c114 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 10 Jan 2016 00:55:22 +0400 Subject: [PATCH] add mock for persistent data api --- plugins/c9.vfs.standalone/standalone.js | 49 ++++++++++++++++++++++++- settings/standalone.js | 2 +- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/plugins/c9.vfs.standalone/standalone.js b/plugins/c9.vfs.standalone/standalone.js index 06ab4a35..cd2019e7 100644 --- a/plugins/c9.vfs.standalone/standalone.js +++ b/plugins/c9.vfs.standalone/standalone.js @@ -183,7 +183,6 @@ function plugin(options, imports, register) { api.get("/configs/require_config.js", function(req, res, next) { var config = res.getOptions().requirejsConfig || {}; - config.waitSeconds = 240; res.writeHead(200, {"Content-Type": "application/javascript"}); res.end("requirejs.config(" + JSON.stringify(config) + ");"); @@ -212,6 +211,54 @@ function plugin(options, imports, register) { api.get("/api.json", {name: "api"}, frontdoor.middleware.describeApi(api)); + api.get("/api/project/:pid/persistent/:apikey", { + params: { + pid: { type: "number" }, + apikey: { type: "string" } + } + }, persistentDataApiMock); + api.put("/api/project/:pid/persistent/:apikey", { + params: { + data: { type: "string", source: "body" }, + pid: { type: "number" }, + apikey: { type: "string" }, + } + }, persistentDataApiMock); + api.get("/api/user/persistent/:apikey", { + params: { + apikey: { type: "string" } + } + }, persistentDataApiMock); + api.put("/api/user/persistent/:apikey", { + params: { + data: { type: "string", source: "body" }, + apikey: { type: "string" }, + } + }, persistentDataApiMock); + + function persistentDataApiMock(req, res, next) { + var name = (req.params.pid || 0) + "-" + req.params.apikey; + var data = req.params.data; + console.log(name, data) + if (/[^\w+=\-]/.test(name)) + return next(new Error("Invalid apikey")); + var path = join(options.installPath, ".c9", "persistent"); + var method = req.method.toLowerCase() + if (method == "get") { + res.writeHead(200, {"Content-Type": "application/octet-stream"}); + var stream = fs.createReadStream(path + "/" + name); + stream.pipe(res); + } else if (method == "put") { + require("mkdirp")(path, function(e) { + fs.writeFile(path + "/" + name, data, "", function(err) { + if (err) return next(err); + res.writeHead(200, {"Content-Type": "application/octet-stream"}); + res.end(""); + }); + }); + } + } + // fake authentication api.authenticate = api.authenticate || function() { return function(req, res, next) { diff --git a/settings/standalone.js b/settings/standalone.js index 340b0894..2d078567 100644 --- a/settings/standalone.js +++ b/settings/standalone.js @@ -63,7 +63,7 @@ module.exports = function(manifest, installPath) { ideBaseUrl: "http://c9.io", previewUrl: "/preview", dashboardUrl: "https://c9.io/dashboard.html", - apiUrl: "https://api.c9.dev", + apiUrl: "/api", homeUrl: "/home", collab: false, installed: true,