Permalink
Browse files

changed documentation handler to cache read markdown documents rather…

… than read from file every time
  • Loading branch information...
1 parent fa5f224 commit 796fbfdf2bab9dc341f97435e3a2fccb18e408aa @paulbjensen paulbjensen committed Apr 29, 2013
Showing with 20 additions and 11 deletions.
  1. +20 −11 server/rpc/general.coffee
View
31 server/rpc/general.coffee
@@ -7,21 +7,30 @@ _ = require 'underscore'
fetchUserFromSession = ss.api.app.helpers.fetchUserFromSession
config = ss.api.app.config
+docData = {}
+
+renderDocument = (id, data, req, res) ->
+ if id.match("api/")?
+ fetchUserFromSession req, res, (user) ->
+ data = data.replace /API_KEY/g , user.apiKey
+ data = data.replace /DASHKU_API_URL/g , config.apiHost
+ res status: "success", content: md data
+ else
+ res status: "success", content: md data
+
exports.actions = (req, res, ss) ->
req.use 'session'
# This fetches a markdown document from the documentation, and returns the rendered html
getDocument: (id) ->
- fs.readFile "#{__dirname}/../docs/#{id}.md", 'utf8', (err, data) ->
- if !err
- if id.match("api/")?
- fetchUserFromSession req, res, (user) ->
- data = data.replace /API_KEY/g , user.apiKey
- data = data.replace /DASHKU_API_URL/g , config.apiHost
-
- res status: "success", content: md data
+ if docData[id]?
+ data = docData[id]
+ renderDocument id, data, req, res
+ else
+ fs.readFile "#{__dirname}/../docs/#{id}.md", 'utf8', (err, data) ->
+ if !err
+ docData[id] = data
+ renderDocument id, data, req, res
else
- res status: "success", content: md data
- else
- res status: "failure", content: "Document not found"
+ res status: "failure", content: "Document not found"

0 comments on commit 796fbfd

Please sign in to comment.