app.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. Bundler.require :app
  4. class App < Sinatra::Application
  5. Bundler.require environment
  6. require 'sinatra/cookies'
  7. require 'tilt/erubis'
  8. Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
  9. configure do
  10. set :sentry_dsn, ENV['SENTRY_DSN']
  11. set :protection, except: [:frame_options, :xss_header]
  12. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  13. set :sprockets, Sprockets::Environment.new(root)
  14. set :cdn_origin, ''
  15. set :assets_prefix, 'assets'
  16. set :assets_path, -> { File.join(public_folder, assets_prefix) }
  17. set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
  18. set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
  19. require 'yajl/json_gem'
  20. set :docs_prefix, 'docs'
  21. set :docs_origin, -> { File.join('', docs_prefix) }
  22. set :docs_path, -> { File.join(public_folder, docs_prefix) }
  23. set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
  24. set :default_docs, %w(css dom dom_events html http javascript)
  25. set :docs, -> {
  26. Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
  27. doc['full_name'] = doc['name'].dup
  28. doc['full_name'] << " #{doc['version']}" if doc['version']
  29. doc['slug_without_version'] = doc['slug'].split('~').first
  30. [doc['slug'], doc]
  31. }]
  32. }
  33. set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
  34. set :news, -> { JSON.parse(File.read(news_path)) }
  35. set :csp, false
  36. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  37. sprockets.append_path(path)
  38. end
  39. Sprockets::Helpers.configure do |config|
  40. config.environment = sprockets
  41. config.prefix = "/#{assets_prefix}"
  42. config.public_path = public_folder
  43. config.protocol = :relative
  44. end
  45. end
  46. configure :test, :development do
  47. require 'active_support/per_thread_registry'
  48. require 'active_support/cache'
  49. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
  50. end
  51. configure :development do
  52. register Sinatra::Reloader
  53. use BetterErrors::Middleware
  54. BetterErrors.application_root = File.expand_path('..', __FILE__)
  55. BetterErrors.editor = :sublime
  56. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' *; font-src data:; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  57. end
  58. configure :production do
  59. set :static, false
  60. set :cdn_origin, 'https://cdn.devdocs.io'
  61. set :docs_origin, '//docs.devdocs.io'
  62. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' http://cdn.devdocs.io https://cdn.devdocs.io https://www.google-analytics.com https://secure.gaug.es http://*.jquery.com https://*.jquery.com; font-src data:; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  63. use Rack::ConditionalGet
  64. use Rack::ETag
  65. use Rack::Deflater
  66. use Rack::Static,
  67. root: 'public',
  68. urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp /mathml.css),
  69. header_rules: [
  70. [:all, {'Cache-Control' => 'no-cache, max-age=0'}],
  71. ['/assets', {'Cache-Control' => 'public, max-age=604800'}],
  72. ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
  73. ['/mathml.css', {'Cache-Control' => 'public, max-age=604800'}],
  74. ['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
  75. sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
  76. sprockets.css_compressor = :sass
  77. Sprockets::Helpers.configure do |config|
  78. config.digest = true
  79. config.asset_host = 'cdn.devdocs.io'
  80. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  81. end
  82. end
  83. configure :test do
  84. set :docs_manifest_path, -> { File.join(root, 'test', 'files', 'docs.json') }
  85. end
  86. helpers do
  87. include Sinatra::Cookies
  88. include Sprockets::Helpers
  89. def canonical_origin
  90. "http://#{request.host_with_port}"
  91. end
  92. def browser
  93. @browser ||= Browser.new(request.user_agent)
  94. end
  95. UNSUPPORTED_IE_VERSIONS = %w(6 7 8 9).freeze
  96. def unsupported_browser?
  97. browser.ie? && UNSUPPORTED_IE_VERSIONS.include?(browser.version)
  98. end
  99. def docs
  100. @docs ||= begin
  101. cookie = cookies[:docs]
  102. if cookie.nil?
  103. settings.default_docs
  104. else
  105. cookie.split('/')
  106. end
  107. end
  108. end
  109. def find_doc(slug)
  110. settings.docs[slug] || begin
  111. settings.docs.each do |_, doc|
  112. return doc if doc['slug_without_version'] == slug
  113. end
  114. nil
  115. end
  116. end
  117. def user_has_docs?(slug)
  118. docs.include?(slug) || begin
  119. slug = "#{slug}~"
  120. docs.any? { |_slug| _slug.start_with?(slug) }
  121. end
  122. end
  123. def doc_index_urls
  124. docs.each_with_object [] do |slug, result|
  125. if doc = settings.docs[slug]
  126. result << File.join('', settings.docs_prefix, slug, 'index.json') + "?#{doc['mtime']}"
  127. end
  128. end
  129. end
  130. def doc_index_page?
  131. @doc && (request.path == "/#{@doc['slug']}/" || request.path == "/#{@doc['slug_without_version']}/")
  132. end
  133. def query_string_for_redirection
  134. request.query_string.empty? ? nil : "?#{request.query_string}"
  135. end
  136. def main_stylesheet_path
  137. stylesheet_paths[dark_theme? ? :dark : :default]
  138. end
  139. def alternate_stylesheet_path
  140. stylesheet_paths[dark_theme? ? :default : :dark]
  141. end
  142. def stylesheet_paths
  143. @stylesheet_paths ||= {
  144. default: stylesheet_path('application'),
  145. dark: stylesheet_path('application-dark')
  146. }
  147. end
  148. def app_size
  149. @app_size ||= cookies[:size].nil? ? '18rem' : "#{cookies[:size]}px"
  150. end
  151. def app_layout
  152. cookies[:layout]
  153. end
  154. def app_theme
  155. @app_theme ||= cookies[:dark].nil? ? 'default' : 'dark'
  156. end
  157. def dark_theme?
  158. app_theme == 'dark'
  159. end
  160. def redirect_via_js(path) # courtesy of HTML5 App Cache
  161. response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
  162. redirect '/', 302
  163. end
  164. def supports_js_redirection?
  165. browser.modern? && !cookies.empty?
  166. end
  167. end
  168. before do
  169. halt erb :unsupported if unsupported_browser?
  170. end
  171. OUT_HOST = 'out.devdocs.io'.freeze
  172. before do
  173. if request.host == OUT_HOST && !request.path.start_with?('/s/')
  174. query_string = "?#{request.query_string}" unless request.query_string.empty?
  175. redirect "http://devdocs.io#{request.path}#{query_string}", 302
  176. end
  177. end
  178. get '/manifest.appcache' do
  179. content_type 'text/cache-manifest'
  180. expires 0, :'no-cache'
  181. erb :manifest
  182. end
  183. get '/' do
  184. return redirect '/' unless request.query_string.empty? # courtesy of HTML5 App Cache
  185. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  186. erb :index
  187. end
  188. %w(offline about news help).each do |page|
  189. get "/#{page}" do
  190. if supports_js_redirection?
  191. redirect_via_js "/#{page}"
  192. else
  193. redirect "/#/#{page}", 302
  194. end
  195. end
  196. end
  197. get '/search' do
  198. redirect "/#q=#{params[:q]}"
  199. end
  200. get '/ping' do
  201. 200
  202. end
  203. %w(docs.json application.js application.css).each do |asset|
  204. class_eval <<-CODE, __FILE__, __LINE__ + 1
  205. get '/#{asset}' do
  206. redirect asset_path('#{asset}', protocol: 'http')
  207. end
  208. CODE
  209. end
  210. {
  211. '/s/maxcdn' => 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  212. '/s/shopify' => 'https://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  213. '/s/jetbrains' => 'https://www.jetbrains.com/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  214. '/s/jetbrains/ruby' => 'https://www.jetbrains.com/ruby/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  215. '/s/jetbrains/python' => 'https://www.jetbrains.com/pycharm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  216. '/s/jetbrains/c' => 'https://www.jetbrains.com/clion/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  217. '/s/jetbrains/web' => 'https://www.jetbrains.com/webstorm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  218. '/s/code-school' => 'http://www.codeschool.com/?utm_campaign=devdocs&utm_content=homepage&utm_source=devdocs&utm_medium=sponsorship',
  219. '/s/tw' => 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%20API%20documentation%20browser%20with%20offline%20mode%20and%20instant%20search%3A',
  220. '/s/fb' => 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io',
  221. '/s/re' => 'https://www.reddit.com/submit?url=http%3A%2F%2Fdevdocs.io&title=All-in-one%20API%20documentation%20browser%20with%20offline%20mode%20and%20instant%20search&resubmit=true'
  222. }.each do |path, url|
  223. class_eval <<-CODE, __FILE__, __LINE__ + 1
  224. get '#{path}' do
  225. redirect '#{url}'
  226. end
  227. CODE
  228. end
  229. get %r{\A/feed(?:\.atom)?\z} do
  230. content_type 'application/atom+xml'
  231. settings.news_feed
  232. end
  233. DOC_REDIRECTS = {
  234. 'iojs' => 'node',
  235. 'yii1' => 'yii~1.1',
  236. 'python2' => 'python~2.7',
  237. 'xpath' => 'xslt_xpath',
  238. 'angular~2.0_typescript' => 'angular~2_typescript',
  239. 'angular~1.5' => 'angularjs~1.5',
  240. 'angular~1.4' => 'angularjs~1.4',
  241. 'angular~1.3' => 'angularjs~1.3',
  242. 'angular~1.2' => 'angularjs~1.2',
  243. 'codeigniter~3.0' => 'codeigniter~3'
  244. }
  245. get %r{\A/([\w~\.%]+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
  246. doc.sub! '%7E', '~'
  247. return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}", 301 if DOC_REDIRECTS.key?(doc)
  248. return redirect "/angularjs/api#{rest}", 301 if doc == 'angular' && rest.start_with?('/ng')
  249. return 404 unless @doc = find_doc(doc)
  250. if rest.nil?
  251. redirect "/#{doc}#{type}/#{query_string_for_redirection}"
  252. elsif rest.length > 1 && rest.end_with?('/')
  253. redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
  254. elsif user_has_docs?(doc) && supports_js_redirection?
  255. redirect_via_js(request.path)
  256. else
  257. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  258. erb :other
  259. end
  260. end
  261. not_found do
  262. send_file File.join(settings.public_folder, '404.html'), status: status
  263. end
  264. error do
  265. send_file File.join(settings.public_folder, '500.html'), status: status
  266. end
  267. configure do
  268. require 'rss'
  269. feed = RSS::Maker.make('atom') do |maker|
  270. maker.channel.id = 'tag:devdocs.io,2014:/feed'
  271. maker.channel.title = 'DevDocs'
  272. maker.channel.author = 'DevDocs'
  273. maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
  274. maker.channel.links.new_link do |link|
  275. link.rel = 'self'
  276. link.href = 'http://devdocs.io/feed.atom'
  277. link.type = 'application/atom+xml'
  278. end
  279. maker.channel.links.new_link do |link|
  280. link.rel = 'alternate'
  281. link.href = 'http://devdocs.io/'
  282. link.type = 'text/html'
  283. end
  284. news.each_with_index do |news, i|
  285. maker.items.new_item do |item|
  286. item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
  287. item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
  288. item.description do |desc|
  289. desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="http://devdocs.io/')
  290. desc.type = 'html'
  291. end
  292. item.updated = "#{news.first}T14:00:00Z"
  293. item.published = "#{news.first}T14:00:00Z"
  294. item.links.new_link do |link|
  295. link.rel = 'alternate'
  296. link.href = 'http://devdocs.io/'
  297. link.type = 'text/html'
  298. end
  299. end
  300. end
  301. end
  302. set :news_feed, feed.to_s
  303. end
  304. end