app.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. require 'bundler/setup'
  2. Bundler.require :app
  3. class App < Sinatra::Application
  4. Bundler.require environment
  5. require 'sinatra/cookies'
  6. configure do
  7. set :sentry_dsn, ENV['SENTRY_DSN']
  8. set :protection, except: [:frame_options, :xss_header]
  9. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  10. set :sprockets, Sprockets::Environment.new(root)
  11. set :assets_prefix, 'assets'
  12. set :assets_path, -> { File.join(public_folder, assets_prefix) }
  13. set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
  14. set :assets_compile, %w(*.png docs.js application.js application.css)
  15. require 'yajl/json_gem'
  16. set :docs_prefix, 'docs'
  17. set :docs_host, -> { File.join('', docs_prefix) }
  18. set :docs_path, -> { File.join(public_folder, docs_prefix) }
  19. set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
  20. set :docs, -> { Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc| [doc['slug'], doc] }] }
  21. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  22. sprockets.append_path(path)
  23. end
  24. Sprockets::Helpers.configure do |config|
  25. config.environment = sprockets
  26. config.prefix = "/#{assets_prefix}"
  27. config.public_path = public_folder
  28. end
  29. end
  30. configure :development do
  31. register Sinatra::Reloader
  32. require 'active_support/cache'
  33. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets')
  34. use BetterErrors::Middleware
  35. BetterErrors.application_root = File.expand_path('..', __FILE__)
  36. BetterErrors.editor = :sublime
  37. end
  38. configure :production do
  39. set :static, false
  40. set :docs_host, 'http://docs.devdocs.io'
  41. use Rack::ConditionalGet
  42. use Rack::ETag
  43. use Rack::Deflater
  44. use Rack::Static,
  45. root: 'public',
  46. urls: %w(/assets /docs /images /favicon.ico /robots.txt /opensearch.xml),
  47. header_rules: [
  48. [:all, {'Cache-Control' => 'private, max-age=0'}],
  49. ['/assets', {'Cache-Control' => 'public, max-age=604800'}],
  50. ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
  51. ['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
  52. sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
  53. sprockets.css_compressor = :sass
  54. Sprockets::Helpers.configure do |config|
  55. config.digest = true
  56. config.asset_host = 'maxcdn.devdocs.io'
  57. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  58. end
  59. end
  60. helpers do
  61. include Sinatra::Cookies
  62. include Sprockets::Helpers
  63. def browser
  64. @browser ||= Browser.new ua: request.user_agent
  65. end
  66. def unsupported_browser?
  67. browser.ie? && browser.version.to_i <= 9
  68. end
  69. def doc_index_urls
  70. cookie = cookies[:docs]
  71. return [] if cookie.nil? || cookie.empty?
  72. cookie.split('/').inject [] do |result, slug|
  73. if doc = settings.docs[slug]
  74. result << File.join('', settings.docs_prefix, doc['index_path'])
  75. end
  76. result
  77. end
  78. end
  79. end
  80. before do
  81. halt erb :unsupported if unsupported_browser?
  82. end
  83. get '/manifest.appcache' do
  84. content_type 'text/cache-manifest'
  85. expires 0, :private
  86. erb :manifest
  87. end
  88. get '/' do
  89. return redirect '/' unless request.query_string.empty?
  90. erb :index
  91. end
  92. %w(about news help).each do |page|
  93. get "/#{page}" do
  94. redirect "/#/#{page}", 302
  95. end
  96. end
  97. get '/ping' do
  98. 200
  99. end
  100. get %r{\A/(\w+)(\-[\w\-]+)?(/)?(.+)?\z} do |doc, type, slash, rest|
  101. return 404 unless @doc = settings.docs[doc]
  102. if !rest && !slash
  103. redirect "/#{doc}#{type}/"
  104. elsif rest && rest.end_with?('/')
  105. redirect "#{doc}#{type}#{slash}#{rest[0...-1]}"
  106. else
  107. erb :other
  108. end
  109. end
  110. not_found do
  111. send_file File.join(settings.public_folder, '404.html'), status: status
  112. end
  113. error do
  114. send_file File.join(settings.public_folder, '500.html'), status: status
  115. end
  116. end