From 89d3df52943dbb74c07b6d97ffc864b848d19d42 Mon Sep 17 00:00:00 2001 From: dsc Date: Wed, 7 Mar 2012 11:41:49 -0800 Subject: [PATCH] Server can now save json presets --- .gitignore | 1 + lib/main.co | 2 +- lib/server/server.co | 24 +++++++++++++++++++++++- www/graph/dygraph-options.yaml | 1 - www/schema/dygraph.yaml | 1 + 5 files changed, 26 insertions(+), 3 deletions(-) delete mode 120000 www/graph/dygraph-options.yaml create mode 120000 www/schema/dygraph.yaml diff --git a/.gitignore b/.gitignore index 7484718..49be8ee 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ dist/ tmp/ var/ static/tmp +www/presets/*.json \ No newline at end of file diff --git a/lib/main.co b/lib/main.co index 1cf6af4..e9e6960 100644 --- a/lib/main.co +++ b/lib/main.co @@ -56,7 +56,7 @@ main = -> # Load data files -Seq([ <[ CHART_OPTIONS_SPEC /graph/dygraph-options.json ]>, +Seq([ <[ CHART_OPTIONS_SPEC /schema/dygraph.json ]>, <[ ROOT_VIS_DATA /presets/root.json ]> ]) .parEach_ (next, [key, url]) -> diff --git a/lib/server/server.co b/lib/server/server.co index 44c8d93..2b0edb7 100755 --- a/lib/server/server.co +++ b/lib/server/server.co @@ -121,9 +121,31 @@ app.configure -> app.get '/', (req, res) -> res.render 'dashboard' +app.post '/graph/save', (req, res, next) -> + if not req.body + return res.send {result:"error", message:"JSON required!"}, 501 + + data = req.body + {slug} = data + if not slug + return res.send {result:"error", message:"slug required!"}, 501 + + err <- fs.writeFile "#WWW/presets/#slug.json", JSON.stringify(data), "utf8" + if err + res.send { result:"error", message:err.message or String(err) }, 501 + else + res.send {result:"ok"} + + +app.get '/graph/:slug\.json', (req, res, next) -> + req.url .= replace /^\/graph\//i, '/presets/' + next() + app.get '/preset/:slug', (req, res, next) -> + {slug} = req.params if exists("#WWW/presets/#slug.yaml") or exists("#WWW/presets/#slug.json") - req.url += '.json' + req.url .= replace /^\/preset\/[^\/?]/i, "/presets/#slug.json" + # req.url += '.json' next() app.get '/graph/:slug/?', (req, res, next) -> diff --git a/www/graph/dygraph-options.yaml b/www/graph/dygraph-options.yaml deleted file mode 120000 index b800728..0000000 --- a/www/graph/dygraph-options.yaml +++ /dev/null @@ -1 +0,0 @@ -../../msc/dygraph-options/data.yaml \ No newline at end of file diff --git a/www/schema/dygraph.yaml b/www/schema/dygraph.yaml new file mode 120000 index 0000000..b800728 --- /dev/null +++ b/www/schema/dygraph.yaml @@ -0,0 +1 @@ +../../msc/dygraph-options/data.yaml \ No newline at end of file -- 1.7.0.4