app.rb 4.2 KB

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