Server can now save json presets
authordsc <dsc@wikimedia.org>
Wed, 7 Mar 2012 19:41:49 +0000 (11:41 -0800)
committerdsc <dsc@wikimedia.org>
Wed, 7 Mar 2012 19:41:49 +0000 (11:41 -0800)
.gitignore
lib/main.co
lib/server/server.co
www/schema/dygraph.yaml [moved from www/graph/dygraph-options.yaml with 100% similarity]

index 7484718..49be8ee 100644 (file)
@@ -32,3 +32,4 @@ dist/
 tmp/
 var/
 static/tmp
+www/presets/*.json
\ No newline at end of file
index 1cf6af4..e9e6960 100644 (file)
@@ -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]) ->
index 44c8d93..2b0edb7 100755 (executable)
@@ -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) ->