app.rb 6.9 KB

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