From d3e1d67e878c4e15615765fc80dfc60b717c4b71 Mon Sep 17 00:00:00 2001 From: Andrew Otto Date: Wed, 18 Apr 2012 11:33:44 -0400 Subject: [PATCH] Moving /graphslist to /graphs.json, handled in graph.co controller rather than directly in routing in server.co --- lib/main-graph-list.co | 4 ++-- lib/server/controllers/graph.co | 19 ++++++++++++++++--- lib/server/server.co | 14 +------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/main-graph-list.co b/lib/main-graph-list.co index e724e4d..a726510 100644 --- a/lib/main-graph-list.co +++ b/lib/main-graph-list.co @@ -10,11 +10,11 @@ # 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 diff --git a/lib/server/controllers/graph.co b/lib/server/controllers/graph.co index 3503779..7dcea18 100644 --- a/lib/server/controllers/graph.co +++ b/lib/server/controllers/graph.co @@ -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 diff --git a/lib/server/server.co b/lib/server/server.co index 7a53335..1e2f288 100755 --- a/lib/server/server.co +++ b/lib/server/server.co @@ -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 { -- 1.7.0.4