Moving /graphslist to /graphs.json, handled in graph.co controller rather than direct...
authorAndrew Otto <acotto@gmail.com>
Wed, 18 Apr 2012 15:33:44 +0000 (11:33 -0400)
committerAndrew Otto <acotto@gmail.com>
Wed, 18 Apr 2012 15:33:44 +0000 (11:33 -0400)
lib/main-graph-list.co
lib/server/controllers/graph.co
lib/server/server.co

index e724e4d..a726510 100644 (file)
 # appends the rendered view to the DOM.
 main = (graph_list_data) ->
     graphs = new GraphList graph_list_data
-    view = new GraphListView { 'collection': graphs }
+    view   = new GraphListView { 'collection': graphs }
     $ '#content .inner' .append view.el
 
 # URL to query for saved graph JSON data.
-graph_list_url = '/graphslist'
+graph_list_url = '/graphs.json'
 
 jQuery.ajax do
     url : graph_list_url
index 3503779..7dcea18 100644 (file)
@@ -2,9 +2,13 @@ _        = require 'underscore'
 fs       = require 'fs'
 path     = require 'path'
 yaml     = require 'js-yaml'
+Seq      = require 'seq'
 
 {existsSync:exists} = path
 {mkdirp, mkdirpAsync} = require '../mkdirp'
+{ readJSONFilesAsync,
+} = require '../files'
+
 Controller = require '../controller'
 
 
@@ -59,9 +63,18 @@ class GraphController extends Controller
             console.error "GraphController.autoload(#id, #{typeof cb}) -->\nerr"
             cb err
     
-    # GET /graphs
+    # GET /graphs.:format?
     index: (req, res) ->
-        res.render 'graph/index'
+        # if format is json, then return the graph JSON
+        if (req.params.format == 'json')
+            pattern = @dataDir + "/*.json"
+            Seq()
+                # find list of data/graph/*.json files, send back the objects
+                .seq            -> readJSONFilesAsync pattern, this
+                # remove filepath info
+                .seq   (graphs) -> res.send _.values graphs
+        else
+            res.render 'graph/index'
     
     # GET /graphs/:graph
     show: (req, res) ->
@@ -131,6 +144,6 @@ class GraphController extends Controller
                 res.send { result:"error", message:msg }, 501
             else
                 res.send { result:"ok" }
-    
+
 
 module.exports = exports = GraphController
index 7a53335..1e2f288 100755 (executable)
@@ -6,7 +6,7 @@ glob   = require 'glob'
 {existsSync:exists} = path
 {exec, spawn} = require 'child_process'
 {mkdirp, mkdirpAsync} = require './mkdirp'
-{ readFilesAsync, readJSONFilesAsync,
+{ readFilesAsync,
 } = require './files'
 
 
@@ -177,17 +177,6 @@ app.get '/datasources/all', (req, res, next) ->
             console.error that if err.stack
             res.send { error:String(err), partial_data:data }
 
-# FIXME:  Make /graphs.json do this instead of graphslist.
-#      :  Can I use Seq .par to read in data and also parse the JSON?
-# Sends a JSON list of saved graphs.  These
-# can be used to instantiate a GraphList of Graph Models.
-app.get '/graphslist' (req, res) ->
-    Seq()
-        # find list of data/graph/*.json files, send back the objects
-        .seq            -> readJSONFilesAsync "#DATA/graphs/*.json", this
-        # remove filepath info
-        .seq   (graphs) -> res.send _.values graphs
-
 app.get '/', (req, res) ->
     res.render 'dashboard'
 
@@ -216,7 +205,6 @@ app.all '/webhook/post-update', (req, res) ->
         else
             res.send "$ #cmd\n\n#stdout\n\n#stderr", 200
 
-
 # }}}
 
 exports import {