Skip to content
This repository
Browse code

changed documentation handler to cache read markdown documents rather…

… than read from file every time
  • Loading branch information...
commit 796fbfdf2bab9dc341f97435e3a2fccb18e408aa 1 parent fa5f224
Paul Jensen paulbjensen authored

Showing 1 changed file with 20 additions and 11 deletions. Show diff stats Hide diff stats

  1. +20 11 server/rpc/general.coffee
31 server/rpc/general.coffee
@@ -7,21 +7,30 @@ _ = require 'underscore'
7 7 fetchUserFromSession = ss.api.app.helpers.fetchUserFromSession
8 8 config = ss.api.app.config
9 9
  10 +docData = {}
  11 +
  12 +renderDocument = (id, data, req, res) ->
  13 + if id.match("api/")?
  14 + fetchUserFromSession req, res, (user) ->
  15 + data = data.replace /API_KEY/g , user.apiKey
  16 + data = data.replace /DASHKU_API_URL/g , config.apiHost
  17 + res status: "success", content: md data
  18 + else
  19 + res status: "success", content: md data
  20 +
10 21 exports.actions = (req, res, ss) ->
11 22
12 23 req.use 'session'
13 24
14 25 # This fetches a markdown document from the documentation, and returns the rendered html
15 26 getDocument: (id) ->
16   - fs.readFile "#{__dirname}/../docs/#{id}.md", 'utf8', (err, data) ->
17   - if !err
18   - if id.match("api/")?
19   - fetchUserFromSession req, res, (user) ->
20   - data = data.replace /API_KEY/g , user.apiKey
21   - data = data.replace /DASHKU_API_URL/g , config.apiHost
22   -
23   - res status: "success", content: md data
  27 + if docData[id]?
  28 + data = docData[id]
  29 + renderDocument id, data, req, res
  30 + else
  31 + fs.readFile "#{__dirname}/../docs/#{id}.md", 'utf8', (err, data) ->
  32 + if !err
  33 + docData[id] = data
  34 + renderDocument id, data, req, res
24 35 else
25   - res status: "success", content: md data
26   - else
27   - res status: "failure", content: "Document not found"
  36 + res status: "failure", content: "Document not found"

0 comments on commit 796fbfd

Please sign in to comment.
Something went wrong with that request. Please try again.