app.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. require 'bundler/setup'
  2. Bundler.require :app
  3. class App < Sinatra::Application
  4. Bundler.require environment
  5. require 'sinatra/cookies'
  6. Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
  7. configure do
  8. set :sentry_dsn, ENV['SENTRY_DSN']
  9. set :protection, except: [:frame_options, :xss_header]
  10. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  11. set :sprockets, Sprockets::Environment.new(root)
  12. set :assets_prefix, 'assets'
  13. set :assets_path, -> { File.join(public_folder, assets_prefix) }
  14. set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
  15. set :assets_compile, %w(*.png docs.js application.js application.css)
  16. require 'yajl/json_gem'
  17. set :docs_prefix, 'docs'
  18. set :docs_host, -> { File.join('', docs_prefix) }
  19. set :docs_path, -> { File.join(public_folder, docs_prefix) }
  20. set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
  21. set :docs, -> { Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc| [doc['slug'], doc] }] }
  22. set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
  23. set :news, -> { JSON.parse(File.read(news_path)) }
  24. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  25. sprockets.append_path(path)
  26. end
  27. Sprockets::Helpers.configure do |config|
  28. config.environment = sprockets
  29. config.prefix = "/#{assets_prefix}"
  30. config.public_path = public_folder
  31. end
  32. end
  33. configure :test, :development do
  34. require 'active_support/per_thread_registry'
  35. require 'active_support/cache'
  36. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets')
  37. end
  38. configure :development do
  39. register Sinatra::Reloader
  40. use BetterErrors::Middleware
  41. BetterErrors.application_root = File.expand_path('..', __FILE__)
  42. BetterErrors.editor = :sublime
  43. end
  44. configure :production do
  45. set :static, false
  46. set :docs_host, 'http://docs.devdocs.io'
  47. use Rack::ConditionalGet
  48. use Rack::ETag
  49. use Rack::Deflater
  50. use Rack::Static,
  51. root: 'public',
  52. urls: %w(/assets /docs /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp),
  53. header_rules: [
  54. [:all, {'Cache-Control' => 'no-cache, max-age=0'}],
  55. ['/assets', {'Cache-Control' => 'public, max-age=604800'}],
  56. ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
  57. ['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
  58. sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
  59. sprockets.css_compressor = :sass
  60. Sprockets::Helpers.configure do |config|
  61. config.digest = true
  62. config.asset_host = 'maxcdn.devdocs.io'
  63. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  64. end
  65. end
  66. helpers do
  67. include Sinatra::Cookies
  68. include Sprockets::Helpers
  69. def browser
  70. @browser ||= Browser.new ua: request.user_agent
  71. end
  72. def unsupported_browser?
  73. browser.ie? && %w(6 7 8 9).include?(browser.version)
  74. end
  75. def doc_index_urls
  76. cookie = cookies[:docs]
  77. return [] if cookie.nil? || cookie.empty?
  78. cookie.split('/').inject [] do |result, slug|
  79. if doc = settings.docs[slug]
  80. result << File.join('', settings.docs_prefix, doc['index_path']) + "?#{doc['mtime']}"
  81. end
  82. result
  83. end
  84. end
  85. def doc_index_page?
  86. @doc && request.path == "/#{@doc['slug']}/"
  87. end
  88. def query_string_for_redirection
  89. request.query_string.empty? ? nil : "?#{request.query_string}"
  90. end
  91. end
  92. before do
  93. halt erb :unsupported if unsupported_browser?
  94. end
  95. get '/manifest.appcache' do
  96. content_type 'text/cache-manifest'
  97. expires 0, :'no-cache'
  98. erb :manifest
  99. end
  100. get '/' do
  101. return redirect '/' unless request.query_string.empty?
  102. erb :index
  103. end
  104. %w(offline about news help).each do |page|
  105. get "/#{page}" do
  106. redirect "/#/#{page}", 302
  107. end
  108. end
  109. get '/search' do
  110. redirect "/#q=#{params[:q]}"
  111. end
  112. get '/ping' do
  113. 200
  114. end
  115. get '/s/maxcdn' do
  116. redirect 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs'
  117. end
  118. get '/s/shopify' do
  119. redirect 'http://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs'
  120. end
  121. get %r{\A/feed(?:\.atom)?\z} do
  122. content_type 'application/atom+xml'
  123. settings.news_feed
  124. end
  125. get '/s/tw' do
  126. redirect 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%2C%20quickly%20searchable%20API%20docs%3A'
  127. end
  128. get '/s/fb' do
  129. redirect 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io'
  130. end
  131. get '/s/re' do
  132. redirect 'http://www.reddit.com/submit?url=http%3A%2F%2Fdevdocs.io&title=All-in-one%2C%20quickly%20searchable%20API%20docs&resubmit=true'
  133. end
  134. get %r{\A/(\w+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
  135. return 404 unless @doc = settings.docs[doc]
  136. if rest.nil?
  137. redirect "/#{doc}#{type}/#{query_string_for_redirection}"
  138. elsif rest.length > 1 && rest.end_with?('/')
  139. redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
  140. else
  141. erb :other
  142. end
  143. end
  144. not_found do
  145. send_file File.join(settings.public_folder, '404.html'), status: status
  146. end
  147. error do
  148. send_file File.join(settings.public_folder, '500.html'), status: status
  149. end
  150. configure do
  151. require 'rss'
  152. feed = RSS::Maker.make('atom') do |maker|
  153. maker.channel.id = 'tag:devdocs.io,2014:/feed'
  154. maker.channel.title = 'DevDocs'
  155. maker.channel.author = 'DevDocs'
  156. maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
  157. maker.channel.links.new_link do |link|
  158. link.rel = 'self'
  159. link.href = 'http://devdocs.io/feed.atom'
  160. link.type = 'application/atom+xml'
  161. end
  162. maker.channel.links.new_link do |link|
  163. link.rel = 'alternate'
  164. link.href = 'http://devdocs.io/'
  165. link.type = 'text/html'
  166. end
  167. news.each_with_index do |news, i|
  168. maker.items.new_item do |item|
  169. item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
  170. item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
  171. item.description do |desc|
  172. desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="http://devdocs.io/')
  173. desc.type = 'html'
  174. end
  175. item.updated = "#{news.first}T14:00:00Z"
  176. item.published = "#{news.first}T14:00:00Z"
  177. item.links.new_link do |link|
  178. link.rel = 'alternate'
  179. link.href = 'http://devdocs.io/'
  180. link.type = 'text/html'
  181. end
  182. end
  183. end
  184. end
  185. set :news_feed, feed.to_s
  186. end
  187. end