app.rb 4.1 KB

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