app.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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/erubi'
  8. require 'active_support/notifications'
  9. Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
  10. configure do
  11. use Rack::SslEnforcer, only_environments: ['production', 'test'], hsts: true, force_secure_cookies: false
  12. set :sentry_dsn, ENV['SENTRY_DSN']
  13. set :protection, except: [:frame_options, :xss_header]
  14. set :root, Pathname.new(File.expand_path('../..', __FILE__))
  15. set :sprockets, Sprockets::Environment.new(root)
  16. set :assets_prefix, 'assets'
  17. set :assets_path, File.join(public_folder, assets_prefix)
  18. set :assets_manifest_path, File.join(assets_path, 'manifest.json')
  19. set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
  20. require 'yajl/json_gem'
  21. set :docs_prefix, 'docs'
  22. set :docs_origin, File.join('', docs_prefix)
  23. set :docs_path, File.join(public_folder, docs_prefix)
  24. set :docs_manifest_path, File.join(docs_path, 'docs.json')
  25. set :default_docs, %w(css dom html http javascript)
  26. set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json')
  27. set :docs_aliases, {
  28. 'angular' => 'ng',
  29. 'angular.js' => 'ng',
  30. 'backbone.js' => 'bb',
  31. 'c++' => 'cpp',
  32. 'coffeescript' => 'cs',
  33. 'crystal' => 'cr',
  34. 'elixir' => 'ex',
  35. 'javascript' => 'js',
  36. 'julia' => 'jl',
  37. 'jquery' => '$',
  38. 'knockout.js' => 'ko',
  39. 'kubernetes' => 'k8s',
  40. 'less' => 'ls',
  41. 'lodash' => '_',
  42. 'löve' => 'love',
  43. 'marionette' => 'mn',
  44. 'markdown' => 'md',
  45. 'matplotlib' => 'mpl',
  46. 'modernizr' => 'mdr',
  47. 'moment.js' => 'mt',
  48. 'openjdk' => 'java',
  49. 'nginx' => 'ngx',
  50. 'numpy' => 'np',
  51. 'pandas' => 'pd',
  52. 'postgresql' => 'pg',
  53. 'python' => 'py',
  54. 'ruby.on.rails' => 'ror',
  55. 'ruby' => 'rb',
  56. 'rust' => 'rs',
  57. 'sass' => 'scss',
  58. 'tensorflow' => 'tf',
  59. 'typescript' => 'ts',
  60. 'underscore.js' => '_',
  61. }
  62. set :csp, false
  63. require 'docs'
  64. Docs.generate_manifest
  65. Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
  66. sprockets.append_path(path)
  67. end
  68. Sprockets::Helpers.configure do |config|
  69. config.environment = sprockets
  70. config.prefix = "/#{assets_prefix}"
  71. config.public_path = public_folder
  72. config.protocol = :relative
  73. end
  74. end
  75. configure :test, :development do
  76. require 'thor'
  77. load 'tasks/sprites.thor'
  78. SpritesCLI.new.invoke(:generate, [], :disable_optimization => true)
  79. require 'active_support/cache'
  80. sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
  81. end
  82. configure :development do
  83. register Sinatra::Reloader
  84. use BetterErrors::Middleware
  85. BetterErrors.application_root = File.expand_path('..', __FILE__)
  86. BetterErrors.editor = :sublime
  87. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' *; font-src 'none'; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  88. end
  89. configure :production do
  90. set :static, false
  91. set :docs_origin, '//documents.devdocs.io'
  92. set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' https://www.google-analytics.com https://secure.gaug.es https://*.jquery.com; font-src 'none'; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  93. use Rack::ConditionalGet
  94. use Rack::ETag
  95. use Rack::Deflater
  96. use Rack::Static,
  97. root: 'public',
  98. urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /mathml.css /manifest.json),
  99. header_rules: [
  100. [:all, { 'Cache-Control' => 'no-cache, max-age=0' }],
  101. ['/assets', { 'Cache-Control' => 'public, max-age=604800' }],
  102. ['/docs', { 'Cache-Control' => 'public, max-age=86400' }],
  103. ['/images', { 'Cache-Control' => 'public, max-age=86400' }],
  104. ['/favicon.ico', { 'Cache-Control' => 'public, max-age=86400' }],
  105. ['/robots.txt', { 'Cache-Control' => 'public, max-age=86400' }],
  106. ['/opensearch.xml', { 'Cache-Control' => 'public, max-age=86400' }],
  107. ['/mathml.css', { 'Cache-Control' => 'public, max-age=86400' }],
  108. ['/manifest.json', { 'Cache-Control' => 'public, max-age=86400' }]
  109. ]
  110. sprockets.js_compressor = Terser.new
  111. sprockets.css_compressor = :sass
  112. Sprockets::Helpers.configure do |config|
  113. config.digest = true
  114. config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
  115. end
  116. end
  117. configure :test do
  118. set :docs_manifest_path, File.join(root, 'test', 'files', 'docs.json')
  119. end
  120. def self.parse_docs
  121. Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
  122. doc['full_name'] = doc['name'].dup
  123. doc['full_name'] << " #{doc['version']}" if doc['version'] && !doc['version'].empty?
  124. doc['slug_without_version'] = doc['slug'].split('~').first
  125. [doc['slug'], doc]
  126. }]
  127. end
  128. def self.parse_news
  129. JSON.parse(File.read(news_path))
  130. end
  131. configure :development, :test do
  132. set :docs, -> { parse_docs }
  133. set :news, -> { parse_news }
  134. end
  135. configure :production do
  136. set :docs, parse_docs
  137. set :news, parse_news
  138. end
  139. helpers do
  140. include Sinatra::Cookies
  141. include Sprockets::Helpers
  142. def memoized_cookies
  143. @memoized_cookies ||= cookies.to_hash
  144. end
  145. def canonical_origin
  146. "https://#{request.host_with_port}"
  147. end
  148. def browser
  149. @browser ||= Browser.new(request.user_agent)
  150. end
  151. def unsupported_browser?
  152. browser.ie?
  153. end
  154. def docs
  155. @docs ||= begin
  156. cookie = memoized_cookies['docs']
  157. if cookie.nil?
  158. settings.default_docs
  159. else
  160. cookie.split('/')
  161. end
  162. end
  163. end
  164. def find_doc(slug)
  165. settings.docs[slug] || begin
  166. settings.docs.each do |_, doc|
  167. return doc if doc['slug_without_version'] == slug
  168. end
  169. nil
  170. end
  171. end
  172. def user_has_docs?(slug)
  173. docs.include?(slug) || begin
  174. slug = "#{slug}~"
  175. docs.any? { |_slug| _slug.start_with?(slug) }
  176. end
  177. end
  178. def doc_index_urls
  179. docs.each_with_object [] do |slug, result|
  180. if doc = settings.docs[slug]
  181. result << File.join('', settings.docs_prefix, slug, 'index.json') + "?#{doc['mtime']}"
  182. end
  183. end
  184. end
  185. def doc_index_page?
  186. @doc && (request.path == "/#{@doc['slug']}/" || request.path == "/#{@doc['slug_without_version']}/")
  187. end
  188. def query_string_for_redirection
  189. request.query_string.empty? ? nil : "?#{request.query_string}"
  190. end
  191. def service_worker_asset_urls
  192. @@service_worker_asset_urls ||= [
  193. javascript_path('application'),
  194. stylesheet_path('application'),
  195. image_path('sprites/docs.png'),
  196. image_path('sprites/docs@2x.png'),
  197. asset_path('docs.js'),
  198. App.production? ? nil : javascript_path('debug'),
  199. ].compact
  200. end
  201. # Returns a cache name for the service worker to use which changes if any of the assets changes
  202. # When a manifest exist, this name is only created once based on the asset manifest because it never changes without a server restart
  203. # If a manifest does not exist, it is created every time this method is called because the assets can change while the server is running
  204. def service_worker_cache_name
  205. if File.exist?(App.assets_manifest_path)
  206. if defined?(@@service_worker_cache_name)
  207. return @@service_worker_cache_name
  208. end
  209. digest = Sprockets::Manifest
  210. .new(nil, App.assets_manifest_path)
  211. .files
  212. .values
  213. .map {|file| file["digest"]}
  214. .join
  215. return @@service_worker_cache_name ||= Digest::MD5.hexdigest(digest)
  216. else
  217. paths = App.sprockets
  218. .each_file
  219. .to_a
  220. .reject {|file| file.start_with?(App.docs_path)}
  221. return App.sprockets.pack_hexdigest(App.sprockets.files_digest(paths))
  222. end
  223. end
  224. def redirect_via_js(path)
  225. response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
  226. redirect '/', 302
  227. end
  228. def supports_js_redirection?
  229. modern_browser?(browser) && !memoized_cookies.empty?
  230. end
  231. # https://github.com/fnando/browser#detecting-modern-browsers
  232. # https://github.com/fnando/browser/blob/v2.6.1/lib/browser/browser.rb
  233. # This restores the old browser gem `#modern?` functionality as it was in 2.6.1
  234. # It's possible this isn't even really needed any longer, these versions are quite old now
  235. def modern_browser?(browser)
  236. [
  237. browser.webkit?,
  238. browser.firefox? && browser.version.to_i >= 17,
  239. browser.ie? && browser.version.to_i >= 9 && !browser.compatibility_view?,
  240. browser.edge? && !browser.compatibility_view?,
  241. browser.opera? && browser.version.to_i >= 12,
  242. browser.firefox? && browser.device.tablet? && browser.platform.android? && b.version.to_i >= 14
  243. ].any?
  244. end
  245. end
  246. before do
  247. halt erb :unsupported if unsupported_browser?
  248. end
  249. OUT_HOST = 'out.devdocs.io'.freeze
  250. before do
  251. if request.host == OUT_HOST && !request.path.start_with?('/s/')
  252. query_string = "?#{request.query_string}" unless request.query_string.empty?
  253. redirect "https://devdocs.io#{request.path}#{query_string}", 302
  254. end
  255. end
  256. get '/service-worker.js' do
  257. content_type 'application/javascript'
  258. expires 0, :'no-cache'
  259. erb :'service-worker.js'
  260. end
  261. get '/' do
  262. return redirect "/#q=#{params[:q]}" if params[:q]
  263. return redirect '/' unless request.query_string.empty?
  264. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  265. erb :index
  266. end
  267. %w(settings offline about news help).each do |page|
  268. get "/#{page}" do
  269. if supports_js_redirection?
  270. redirect_via_js "/#{page}"
  271. else
  272. redirect "/#/#{page}", 302
  273. end
  274. end
  275. end
  276. get '/search' do
  277. redirect "/#q=#{params[:q]}"
  278. end
  279. get '/ping' do
  280. 200
  281. end
  282. %w(docs.json application.js application.css).each do |asset|
  283. class_eval <<-CODE, __FILE__, __LINE__ + 1
  284. get '/#{asset}' do
  285. redirect asset_path('#{asset}', protocol: 'http')
  286. end
  287. CODE
  288. end
  289. {
  290. '/s/maxcdn' => 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  291. '/s/shopify' => 'https://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
  292. '/s/jetbrains' => 'https://www.jetbrains.com/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  293. '/s/jetbrains/ruby' => 'https://www.jetbrains.com/ruby/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  294. '/s/jetbrains/python' => 'https://www.jetbrains.com/pycharm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  295. '/s/jetbrains/c' => 'https://www.jetbrains.com/clion/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  296. '/s/jetbrains/web' => 'https://www.jetbrains.com/webstorm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
  297. '/s/code-school' => 'https://www.codeschool.com/?utm_campaign=devdocs&utm_content=homepage&utm_source=devdocs&utm_medium=sponsorship',
  298. '/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',
  299. '/s/fb' => 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io',
  300. '/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'
  301. }.each do |path, url|
  302. class_eval <<-CODE, __FILE__, __LINE__ + 1
  303. get '#{path}' do
  304. redirect '#{url}'
  305. end
  306. CODE
  307. end
  308. %w(/maxcdn /maxcdn/).each do |path|
  309. class_eval <<-CODE, __FILE__, __LINE__ + 1
  310. get '#{path}' do
  311. 410
  312. end
  313. CODE
  314. end
  315. {
  316. '/tips' => '/help',
  317. '/css-data-types/' => '/css-values-units/',
  318. '/css-at-rules/' => '/?q=css%20%40',
  319. '/dom/window/setinterval' => '/dom/windoworworkerglobalscope/setinterval',
  320. '/html/article' => '/html/element/article',
  321. '/html-html5/' => 'html-elements/',
  322. '/html-standard/' => 'html-elements/',
  323. '/http-status-codes/' => '/http-status/',
  324. '/ruby/bignum' => '/ruby~2.3/bignum',
  325. '/ruby/fixnum' => '/ruby~2.3/fixnum',
  326. }.each do |path, url|
  327. class_eval <<-CODE, __FILE__, __LINE__ + 1
  328. get '#{path}' do
  329. redirect '#{url}', 301
  330. end
  331. CODE
  332. end
  333. get %r{/feed(?:\.atom)?} do
  334. content_type 'application/atom+xml'
  335. settings.news_feed
  336. end
  337. DOC_REDIRECTS = {
  338. 'iojs' => 'node',
  339. 'node_lts' => 'node~6_lts',
  340. 'node~4.2_lts' => 'node~4_lts',
  341. 'yii1' => 'yii~1.1',
  342. 'python2' => 'python~2.7',
  343. 'xpath' => 'xslt_xpath',
  344. 'angular~4_typescript' => 'angular',
  345. 'angular~2_typescript' => 'angular~2',
  346. 'angular~2.0_typescript' => 'angular~2',
  347. 'angular~1.5' => 'angularjs~1.5',
  348. 'angular~1.4' => 'angularjs~1.4',
  349. 'angular~1.3' => 'angularjs~1.3',
  350. 'angular~1.2' => 'angularjs~1.2',
  351. 'codeigniter~3.0' => 'codeigniter~3',
  352. 'webpack~2' => 'webpack'
  353. }
  354. get %r{/([\w~\.%]+)(\-[\w\-]+)?(/.*)?} do |doc, type, rest|
  355. doc.sub! '%7E', '~'
  356. if DOC_REDIRECTS.key?(doc)
  357. return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}", 301
  358. end
  359. if rest && doc == 'angular' && rest.start_with?('/ng')
  360. return redirect "/angularjs/api#{rest}", 301
  361. end
  362. if rest && doc == 'dom'
  363. if rest.start_with?('/windowtimers')
  364. return redirect "/dom#{rest.sub('windowtimers', 'windoworworkerglobalscope')}", 301
  365. end
  366. if rest.start_with?('/window/url.')
  367. return redirect "/dom#{rest.sub('window/url.', 'url/')}", 301
  368. end
  369. if rest.start_with?('/window.')
  370. return redirect "/dom#{rest.sub('window.', 'window/')}", 301
  371. end
  372. if rest.start_with?('/element.')
  373. return redirect "/dom#{rest.sub('element.', 'element/')}", 301
  374. end
  375. if rest.start_with?('/event.')
  376. return redirect "/dom#{rest.sub('event.', 'event/')}", 301
  377. end
  378. if rest.start_with?('/document.')
  379. return redirect "/dom#{rest.sub('document.', 'document/')}", 301
  380. end
  381. end
  382. return 404 unless @doc = find_doc(doc)
  383. if rest.nil?
  384. redirect "/#{doc}#{type}/#{query_string_for_redirection}"
  385. elsif rest.length > 1 && rest.end_with?('/')
  386. redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
  387. elsif user_has_docs?(doc) && supports_js_redirection?
  388. redirect_via_js(request.path)
  389. else
  390. response.headers['Content-Security-Policy'] = settings.csp if settings.csp
  391. erb :other
  392. end
  393. end
  394. not_found do
  395. send_file File.join(settings.public_folder, '404.html'), status: status
  396. end
  397. error do
  398. send_file File.join(settings.public_folder, '500.html'), status: status
  399. end
  400. configure do
  401. require 'rss'
  402. feed = RSS::Maker.make('atom') do |maker|
  403. maker.channel.id = 'tag:devdocs.io,2014:/feed'
  404. maker.channel.title = 'DevDocs'
  405. maker.channel.author = 'DevDocs'
  406. maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
  407. maker.channel.links.new_link do |link|
  408. link.rel = 'self'
  409. link.href = 'https://devdocs.io/feed.atom'
  410. link.type = 'application/atom+xml'
  411. end
  412. maker.channel.links.new_link do |link|
  413. link.rel = 'alternate'
  414. link.href = 'https://devdocs.io/'
  415. link.type = 'text/html'
  416. end
  417. news.each_with_index do |news, i|
  418. maker.items.new_item do |item|
  419. item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
  420. item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
  421. item.description do |desc|
  422. desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="https://devdocs.io/')
  423. desc.type = 'html'
  424. end
  425. item.updated = "#{news.first}T14:00:00Z"
  426. item.published = "#{news.first}T14:00:00Z"
  427. item.links.new_link do |link|
  428. link.rel = 'alternate'
  429. link.href = 'https://devdocs.io/'
  430. link.type = 'text/html'
  431. end
  432. end
  433. end
  434. end
  435. set :news_feed, feed.to_s
  436. end
  437. end