浏览代码

Actually improve app backend performance

Thibaut Courouble 8 年之前
父节点
当前提交
b3d1c3865a
共有 1 个文件被更改,包括 30 次插入17 次删除
  1. 30 17
      lib/app.rb

+ 30 - 17
lib/app.rb

@@ -21,27 +21,17 @@ class App < Sinatra::Application
     set :cdn_origin, ''
 
     set :assets_prefix, 'assets'
-    set :assets_path, -> { File.join(public_folder, assets_prefix) }
-    set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
+    set :assets_path, File.join(public_folder, assets_prefix)
+    set :assets_manifest_path, File.join(assets_path, 'manifest.json')
     set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
 
     require 'yajl/json_gem'
     set :docs_prefix, 'docs'
-    set :docs_origin, -> { File.join('', docs_prefix) }
-    set :docs_path, -> { File.join(public_folder, docs_prefix) }
-    set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
+    set :docs_origin, File.join('', docs_prefix)
+    set :docs_path, File.join(public_folder, docs_prefix)
+    set :docs_manifest_path, File.join(docs_path, 'docs.json')
     set :default_docs, %w(css dom dom_events html http javascript)
-    set :docs, -> {
-      Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
-        doc['full_name'] = doc['name'].dup
-        doc['full_name'] << " #{doc['version']}" if doc['version']
-        doc['slug_without_version'] = doc['slug'].split('~').first
-        [doc['slug'], doc]
-      }]
-    }
-
-    set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
-    set :news, -> { JSON.parse(File.read(news_path)) }
+    set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json')
 
     set :csp, false
 
@@ -103,7 +93,30 @@ class App < Sinatra::Application
   end
 
   configure :test do
-    set :docs_manifest_path, -> { File.join(root, 'test', 'files', 'docs.json') }
+    set :docs_manifest_path, File.join(root, 'test', 'files', 'docs.json')
+  end
+
+  def self.parse_docs
+    Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
+      doc['full_name'] = doc['name'].dup
+      doc['full_name'] << " #{doc['version']}" if doc['version']
+      doc['slug_without_version'] = doc['slug'].split('~').first
+      [doc['slug'], doc]
+    }]
+  end
+
+  def self.parse_news
+    JSON.parse(File.read(news_path))
+  end
+
+  configure :development, :test do
+    set :docs, -> { parse_docs }
+    set :news, -> { parse_news }
+  end
+
+  configure :production do
+    set :docs, parse_docs
+    set :news, parse_news
   end
 
   helpers do