app.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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' => 'private, 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'])
  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, :private
  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 %r{\A/(\w+)(\-[\w\-]+)?(/)?(.+)?\z} do |doc, type, slash, rest|
  111. return 404 unless @doc = settings.docs[doc]
  112. if !rest && !slash
  113. redirect "/#{doc}#{type}/"
  114. elsif rest && rest.end_with?('/')
  115. redirect "#{doc}#{type}#{slash}#{rest[0...-1]}"
  116. else
  117. erb :other
  118. end
  119. end
  120. not_found do
  121. send_file File.join(settings.public_folder, '404.html'), status: status
  122. end
  123. error do
  124. send_file File.join(settings.public_folder, '500.html'), status: status
  125. end
  126. end