devdocs/lib/app.rb

468 lines
15 KiB
Ruby
Raw Normal View History

2016-01-10 17:55:30 +01:00
# frozen_string_literal: true
2013-10-24 20:25:52 +02:00
require 'bundler/setup'
Bundler.require :app
class App < Sinatra::Application
Bundler.require environment
require 'sinatra/cookies'
2017-02-05 23:04:57 +01:00
require 'tilt/erubi'
2017-01-22 20:40:33 +01:00
require 'active_support/notifications'
2013-10-24 20:25:52 +02:00
2014-01-16 04:47:33 +01:00
Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'
2013-10-24 20:25:52 +02:00
configure do
2018-11-25 18:29:56 +01:00
use Rack::SslEnforcer, only_environments: ['production', 'test'], hsts: true, force_secure_cookies: false
2018-10-07 16:28:28 +02:00
2013-10-24 20:25:52 +02:00
set :sentry_dsn, ENV['SENTRY_DSN']
set :protection, except: [:frame_options, :xss_header]
set :root, Pathname.new(File.expand_path('../..', __FILE__))
set :sprockets, Sprockets::Environment.new(root)
set :assets_prefix, 'assets'
set :assets_path, File.join(public_folder, assets_prefix)
set :assets_manifest_path, File.join(assets_path, 'manifest.json')
set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)
2013-10-24 20:25:52 +02:00
require 'yajl/json_gem'
set :docs_prefix, 'docs'
set :docs_origin, File.join('', docs_prefix)
set :docs_path, File.join(public_folder, docs_prefix)
set :docs_manifest_path, File.join(docs_path, 'docs.json')
set :default_docs, %w(css dom html http javascript)
set :news_path, File.join(root, assets_prefix, 'javascripts', 'news.json')
2014-11-30 19:56:02 +01:00
2016-05-01 20:05:27 +02:00
set :csp, false
require 'docs'
Docs.generate_manifest
2013-10-24 20:25:52 +02:00
Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
sprockets.append_path(path)
end
Sprockets::Helpers.configure do |config|
config.environment = sprockets
config.prefix = "/#{assets_prefix}"
config.public_path = public_folder
2015-05-04 04:25:50 +02:00
config.protocol = :relative
2013-10-24 20:25:52 +02:00
end
end
2015-01-03 16:38:22 +01:00
configure :test, :development do
2018-09-16 23:15:20 +02:00
require 'thor'
load 'tasks/sprites.thor'
2019-09-03 19:32:08 +02:00
SpritesCLI.new.invoke(:generate, [], :disable_optimization => true)
2018-09-16 23:15:20 +02:00
require 'active_support/per_thread_registry'
2013-10-24 20:25:52 +02:00
require 'active_support/cache'
sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
2015-01-03 16:38:22 +01:00
end
configure :development do
register Sinatra::Reloader
2013-10-24 20:25:52 +02:00
use BetterErrors::Middleware
BetterErrors.application_root = File.expand_path('..', __FILE__)
BetterErrors.editor = :sublime
2016-06-04 16:35:29 +02:00
2017-02-19 17:15:23 +01:00
set :csp, "default-src 'self' *; script-src 'self' 'nonce-devdocs' *; font-src 'none'; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
2013-10-24 20:25:52 +02:00
end
configure :production do
set :static, false
2021-02-16 11:49:32 +01:00
set :docs_origin, '//documents.devdocs.io'
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:;"
2013-10-24 20:25:52 +02:00
use Rack::ConditionalGet
use Rack::ETag
use Rack::Deflater
use Rack::Static,
root: 'public',
2017-09-09 22:57:42 +02:00
urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /mathml.css /manifest.json),
2013-10-24 20:25:52 +02:00
header_rules: [
2017-09-10 16:29:57 +02:00
[:all, { 'Cache-Control' => 'no-cache, max-age=0' }],
['/assets', { 'Cache-Control' => 'public, max-age=604800' }],
['/docs', { 'Cache-Control' => 'public, max-age=86400' }],
['/images', { 'Cache-Control' => 'public, max-age=86400' }],
['/favicon.ico', { 'Cache-Control' => 'public, max-age=86400' }],
['/robots.txt', { 'Cache-Control' => 'public, max-age=86400' }],
['/opensearch.xml', { 'Cache-Control' => 'public, max-age=86400' }],
['/mathml.css', { 'Cache-Control' => 'public, max-age=86400' }],
['/manifest.json', { 'Cache-Control' => 'public, max-age=86400' }]
]
2013-10-24 20:25:52 +02:00
sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
sprockets.css_compressor = :sass
Sprockets::Helpers.configure do |config|
config.digest = true
config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
end
end
configure :test do
set :docs_manifest_path, File.join(root, 'test', 'files', 'docs.json')
end
def self.parse_docs
Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
doc['full_name'] = doc['name'].dup
2017-08-08 00:33:17 +02:00
doc['full_name'] << " #{doc['version']}" if doc['version'] && !doc['version'].empty?
doc['slug_without_version'] = doc['slug'].split('~').first
[doc['slug'], doc]
}]
end
def self.parse_news
JSON.parse(File.read(news_path))
end
configure :development, :test do
set :docs, -> { parse_docs }
set :news, -> { parse_news }
end
configure :production do
set :docs, parse_docs
set :news, parse_news
end
2013-10-24 20:25:52 +02:00
helpers do
include Sinatra::Cookies
include Sprockets::Helpers
2017-05-22 23:41:50 +02:00
def memoized_cookies
@memoized_cookies ||= cookies.to_hash
end
def canonical_origin
2018-03-18 21:43:41 +01:00
"https://#{request.host_with_port}"
end
2013-10-24 20:25:52 +02:00
def browser
2016-03-07 00:10:02 +01:00
@browser ||= Browser.new(request.user_agent)
2013-10-24 20:25:52 +02:00
end
def unsupported_browser?
browser.ie?
2013-10-24 20:25:52 +02:00
end
def docs
@docs ||= begin
2017-05-22 23:41:50 +02:00
cookie = memoized_cookies['docs']
if cookie.nil?
settings.default_docs
else
cookie.split('/')
end
end
end
2016-01-17 19:30:46 +01:00
def find_doc(slug)
settings.docs[slug] || begin
settings.docs.each do |_, doc|
return doc if doc['slug_without_version'] == slug
2016-01-17 19:30:46 +01:00
end
nil
end
end
def user_has_docs?(slug)
docs.include?(slug) || begin
slug = "#{slug}~"
2016-01-17 19:30:46 +01:00
docs.any? { |_slug| _slug.start_with?(slug) }
end
end
def doc_index_urls
2015-10-18 17:26:00 +02:00
docs.each_with_object [] do |slug, result|
2013-10-24 20:25:52 +02:00
if doc = settings.docs[slug]
result << File.join('', settings.docs_prefix, slug, 'index.json') + "?#{doc['mtime']}"
2013-10-24 20:25:52 +02:00
end
end
end
def doc_index_page?
@doc && (request.path == "/#{@doc['slug']}/" || request.path == "/#{@doc['slug_without_version']}/")
end
2015-01-03 15:24:07 +01:00
def query_string_for_redirection
request.query_string.empty? ? nil : "?#{request.query_string}"
end
2015-02-08 23:50:03 +01:00
2019-07-07 14:30:31 +02:00
def service_worker_asset_urls
@@service_worker_asset_urls ||= [
javascript_path('application'),
2017-05-22 23:41:50 +02:00
stylesheet_path('application'),
2019-07-06 21:01:42 +02:00
image_path('sprites/docs.png'),
image_path('sprites/docs@2x.png'),
2019-07-07 14:30:31 +02:00
asset_path('docs.js'),
App.production? ? nil : javascript_path('debug'),
].compact
2017-05-22 23:41:50 +02:00
end
2019-07-10 22:38:33 +02:00
# Returns a cache name for the service worker to use which changes if any of the assets changes
# When a manifest exist, this name is only created once based on the asset manifest because it never changes without a server restart
# 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
def service_worker_cache_name
if File.exist?(App.assets_manifest_path)
2019-07-10 22:38:33 +02:00
if defined?(@@service_worker_cache_name)
return @@service_worker_cache_name
end
2015-08-03 23:06:19 +02:00
2019-07-10 22:38:33 +02:00
digest = Sprockets::Manifest
2019-07-10 22:43:52 +02:00
.new(nil, App.assets_manifest_path)
.files
.values
.map {|file| file["digest"]}
.join
2019-07-10 22:38:33 +02:00
return @@service_worker_cache_name ||= Digest::MD5.hexdigest(digest)
else
2019-07-10 22:38:33 +02:00
paths = App.sprockets
2019-07-10 22:43:52 +02:00
.each_file
.to_a
.reject {|file| file.start_with?(App.docs_path)}
2015-08-03 23:06:19 +02:00
2019-07-10 22:38:33 +02:00
return App.sprockets.pack_hexdigest(App.sprockets.files_digest(paths))
end
end
2019-07-07 00:55:58 +02:00
def redirect_via_js(path)
response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
redirect '/', 302
end
def supports_js_redirection?
2017-05-22 23:41:50 +02:00
browser.modern? && !memoized_cookies.empty?
end
2013-10-24 20:25:52 +02:00
end
before do
halt erb :unsupported if unsupported_browser?
end
OUT_HOST = 'out.devdocs.io'.freeze
before do
if request.host == OUT_HOST && !request.path.start_with?('/s/')
query_string = "?#{request.query_string}" unless request.query_string.empty?
2018-03-18 21:34:14 +01:00
redirect "https://devdocs.io#{request.path}#{query_string}", 302
end
end
2019-07-07 00:55:58 +02:00
get '/service-worker.js' do
content_type 'application/javascript'
2014-12-14 23:42:57 +01:00
expires 0, :'no-cache'
2019-07-07 00:55:58 +02:00
erb :'service-worker.js'
2013-10-24 20:25:52 +02:00
end
get '/' do
return redirect "/#q=#{params[:q]}" if params[:q]
2019-07-07 00:55:58 +02:00
return redirect '/' unless request.query_string.empty?
2016-05-01 20:05:27 +02:00
response.headers['Content-Security-Policy'] = settings.csp if settings.csp
2013-10-24 20:25:52 +02:00
erb :index
end
2017-02-26 15:58:41 +01:00
%w(settings offline about news help).each do |page|
2013-10-24 20:25:52 +02:00
get "/#{page}" do
if supports_js_redirection?
redirect_via_js "/#{page}"
else
redirect "/#/#{page}", 302
end
2013-10-24 20:25:52 +02:00
end
end
2014-02-08 17:32:36 +01:00
get '/search' do
redirect "/#q=#{params[:q]}"
end
2013-10-24 20:25:52 +02:00
get '/ping' do
200
end
%w(docs.json application.js application.css).each do |asset|
class_eval <<-CODE, __FILE__, __LINE__ + 1
get '/#{asset}' do
redirect asset_path('#{asset}', protocol: 'http')
end
CODE
end
2015-07-13 04:53:47 +02:00
{
'/s/maxcdn' => 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
'/s/shopify' => 'https://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs',
'/s/jetbrains' => 'https://www.jetbrains.com/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
'/s/jetbrains/ruby' => 'https://www.jetbrains.com/ruby/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
'/s/jetbrains/python' => 'https://www.jetbrains.com/pycharm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
'/s/jetbrains/c' => 'https://www.jetbrains.com/clion/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
'/s/jetbrains/web' => 'https://www.jetbrains.com/webstorm/?utm_source=devdocs&utm_medium=sponsorship&utm_campaign=devdocs',
2017-09-23 18:04:09 +02:00
'/s/code-school' => 'https://www.codeschool.com/?utm_campaign=devdocs&utm_content=homepage&utm_source=devdocs&utm_medium=sponsorship',
2016-07-31 00:29:35 +02:00
'/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',
2016-01-09 16:44:57 +01:00
'/s/fb' => 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io',
2016-07-31 00:29:35 +02:00
'/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'
2015-07-13 04:53:47 +02:00
}.each do |path, url|
class_eval <<-CODE, __FILE__, __LINE__ + 1
get '#{path}' do
redirect '#{url}'
end
CODE
2014-12-14 18:29:34 +01:00
end
2017-03-04 18:23:29 +01:00
%w(/maxcdn /maxcdn/).each do |path|
class_eval <<-CODE, __FILE__, __LINE__ + 1
get '#{path}' do
410
end
CODE
end
{
2017-06-25 18:13:14 +02:00
'/tips' => '/help',
'/css-data-types/' => '/css-values-units/',
'/css-at-rules/' => '/?q=css%20%40',
'/dom/window/setinterval' => '/dom/windoworworkerglobalscope/setinterval',
'/html/article' => '/html/element/article',
'/html-html5/' => 'html-elements/',
'/html-standard/' => 'html-elements/',
'/http-status-codes/' => '/http-status/',
'/ruby/bignum' => '/ruby~2.3/bignum',
'/ruby/fixnum' => '/ruby~2.3/fixnum',
2017-03-04 18:23:29 +01:00
}.each do |path, url|
class_eval <<-CODE, __FILE__, __LINE__ + 1
get '#{path}' do
redirect '#{url}', 301
end
CODE
end
get %r{/feed(?:\.atom)?} do
2014-11-30 21:55:26 +01:00
content_type 'application/atom+xml'
settings.news_feed
end
2016-03-05 18:49:43 +01:00
DOC_REDIRECTS = {
'iojs' => 'node',
2017-03-04 18:23:29 +01:00
'node_lts' => 'node~6_lts',
'node~4.2_lts' => 'node~4_lts',
2016-03-05 18:49:43 +01:00
'yii1' => 'yii~1.1',
2016-03-27 22:16:51 +02:00
'python2' => 'python~2.7',
2016-06-12 23:14:57 +02:00
'xpath' => 'xslt_xpath',
2017-07-30 19:18:27 +02:00
'angular~4_typescript' => 'angular',
'angular~2_typescript' => 'angular~2',
'angular~2.0_typescript' => 'angular~2',
2016-06-12 23:14:57 +02:00
'angular~1.5' => 'angularjs~1.5',
'angular~1.4' => 'angularjs~1.4',
'angular~1.3' => 'angularjs~1.3',
'angular~1.2' => 'angularjs~1.2',
2017-06-25 21:40:51 +02:00
'codeigniter~3.0' => 'codeigniter~3',
'webpack~2' => 'webpack'
2016-03-05 18:49:43 +01:00
}
get %r{/([\w~\.%]+)(\-[\w\-]+)?(/.*)?} do |doc, type, rest|
2016-06-12 23:58:59 +02:00
doc.sub! '%7E', '~'
if DOC_REDIRECTS.key?(doc)
return redirect "/#{DOC_REDIRECTS[doc]}#{type}#{rest}", 301
end
if rest && doc == 'angular' && rest.start_with?('/ng')
return redirect "/angularjs/api#{rest}", 301
end
2017-03-04 18:23:29 +01:00
if rest && doc == 'dom'
if rest.start_with?('/windowtimers')
return redirect "/dom#{rest.sub('windowtimers', 'windoworworkerglobalscope')}", 301
end
2017-06-25 18:13:14 +02:00
if rest.start_with?('/window/url.')
return redirect "/dom#{rest.sub('window/url.', 'url/')}", 301
end
2017-03-04 18:23:29 +01:00
if rest.start_with?('/window.')
return redirect "/dom#{rest.sub('window.', 'window/')}", 301
end
if rest.start_with?('/element.')
return redirect "/dom#{rest.sub('element.', 'element/')}", 301
end
2017-06-25 18:13:14 +02:00
if rest.start_with?('/event.')
return redirect "/dom#{rest.sub('event.', 'event/')}", 301
end
if rest.start_with?('/document.')
return redirect "/dom#{rest.sub('document.', 'document/')}", 301
end
end
2016-01-17 19:30:46 +01:00
return 404 unless @doc = find_doc(doc)
2015-01-03 15:15:46 +01:00
if rest.nil?
2015-01-03 15:24:07 +01:00
redirect "/#{doc}#{type}/#{query_string_for_redirection}"
2015-01-03 15:15:46 +01:00
elsif rest.length > 1 && rest.end_with?('/')
2015-01-03 15:24:07 +01:00
redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
2016-01-17 19:30:46 +01:00
elsif user_has_docs?(doc) && supports_js_redirection?
redirect_via_js(request.path)
else
2016-05-01 20:05:27 +02:00
response.headers['Content-Security-Policy'] = settings.csp if settings.csp
erb :other
end
end
2013-10-24 20:25:52 +02:00
not_found do
send_file File.join(settings.public_folder, '404.html'), status: status
end
error do
send_file File.join(settings.public_folder, '500.html'), status: status
end
2014-11-30 21:55:26 +01:00
configure do
require 'rss'
feed = RSS::Maker.make('atom') do |maker|
maker.channel.id = 'tag:devdocs.io,2014:/feed'
maker.channel.title = 'DevDocs'
maker.channel.author = 'DevDocs'
maker.channel.updated = "#{settings.news.first.first}T14:00:00Z"
maker.channel.links.new_link do |link|
link.rel = 'self'
2018-03-18 21:35:40 +01:00
link.href = 'https://devdocs.io/feed.atom'
2014-11-30 21:55:26 +01:00
link.type = 'application/atom+xml'
end
maker.channel.links.new_link do |link|
link.rel = 'alternate'
2018-03-18 21:35:40 +01:00
link.href = 'https://devdocs.io/'
2014-11-30 21:55:26 +01:00
link.type = 'text/html'
end
news.each_with_index do |news, i|
maker.items.new_item do |item|
item.id = "tag:devdocs.io,2014:News/#{settings.news.length - i}"
item.title = news[1].split("\n").first.gsub(/<\/?[^>]*>/, '')
item.description do |desc|
2018-03-18 21:35:40 +01:00
desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="https://devdocs.io/')
2014-11-30 21:55:26 +01:00
desc.type = 'html'
end
item.updated = "#{news.first}T14:00:00Z"
item.published = "#{news.first}T14:00:00Z"
item.links.new_link do |link|
link.rel = 'alternate'
2018-03-18 21:35:40 +01:00
link.href = 'https://devdocs.io/'
2014-11-30 21:55:26 +01:00
link.type = 'text/html'
end
end
end
end
set :news_feed, feed.to_s
end
2013-10-24 20:25:52 +02:00
end