app.rb 6.5 KB

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