2013-10-24 20:25:52 +02:00
|
|
|
require 'bundler/setup'
|
|
|
|
Bundler.require :app
|
|
|
|
|
|
|
|
class App < Sinatra::Application
|
|
|
|
Bundler.require environment
|
|
|
|
require 'sinatra/cookies'
|
|
|
|
|
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
|
|
|
|
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 application.js application.css)
|
|
|
|
|
|
|
|
require 'yajl/json_gem'
|
|
|
|
set :docs_prefix, 'docs'
|
|
|
|
set :docs_host, -> { File.join('', docs_prefix) }
|
|
|
|
set :docs_path, -> { File.join(public_folder, docs_prefix) }
|
|
|
|
set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
|
|
|
|
set :docs, -> { Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc| [doc['slug'], doc] }] }
|
2015-02-01 23:19:28 +01:00
|
|
|
set :default_docs, %w(css dom dom_events html http javascript)
|
2013-10-24 20:25:52 +02:00
|
|
|
|
2014-11-30 19:56:02 +01:00
|
|
|
set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
|
|
|
|
set :news, -> { JSON.parse(File.read(news_path)) }
|
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-03 16:38:22 +01:00
|
|
|
configure :test, :development do
|
2014-04-13 22:31:09 +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')
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
configure :production do
|
|
|
|
set :static, false
|
|
|
|
set :docs_host, 'http://docs.devdocs.io'
|
|
|
|
|
|
|
|
use Rack::ConditionalGet
|
|
|
|
use Rack::ETag
|
|
|
|
use Rack::Deflater
|
|
|
|
use Rack::Static,
|
|
|
|
root: 'public',
|
2014-01-16 04:44:39 +01:00
|
|
|
urls: %w(/assets /docs /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp),
|
2013-10-24 20:25:52 +02:00
|
|
|
header_rules: [
|
2014-12-14 23:42:57 +01:00
|
|
|
[:all, {'Cache-Control' => 'no-cache, max-age=0'}],
|
2013-10-24 20:25:52 +02:00
|
|
|
['/assets', {'Cache-Control' => 'public, max-age=604800'}],
|
|
|
|
['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
|
|
|
|
['/images', {'Cache-Control' => 'public, max-age=86400'}] ]
|
|
|
|
|
|
|
|
sprockets.js_compressor = Uglifier.new output: { beautify: true, indent_level: 0 }
|
|
|
|
sprockets.css_compressor = :sass
|
|
|
|
|
|
|
|
Sprockets::Helpers.configure do |config|
|
|
|
|
config.digest = true
|
|
|
|
config.asset_host = 'maxcdn.devdocs.io'
|
|
|
|
config.manifest = Sprockets::Manifest.new(sprockets, assets_manifest_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
include Sinatra::Cookies
|
|
|
|
include Sprockets::Helpers
|
|
|
|
|
|
|
|
def browser
|
|
|
|
@browser ||= Browser.new ua: request.user_agent
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsupported_browser?
|
2014-04-06 00:41:20 +02:00
|
|
|
browser.ie? && %w(6 7 8 9).include?(browser.version)
|
2013-10-24 20:25:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def doc_index_urls
|
|
|
|
cookie = cookies[:docs]
|
|
|
|
|
2015-02-01 23:19:28 +01:00
|
|
|
docs = if cookie.nil? || cookie.empty?
|
|
|
|
settings.default_docs
|
|
|
|
else
|
|
|
|
cookie.split('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
docs.inject [] do |result, slug|
|
2013-10-24 20:25:52 +02:00
|
|
|
if doc = settings.docs[slug]
|
2014-12-14 23:42:57 +01:00
|
|
|
result << File.join('', settings.docs_prefix, doc['index_path']) + "?#{doc['mtime']}"
|
2013-10-24 20:25:52 +02:00
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
2014-04-20 03:11:17 +02:00
|
|
|
|
|
|
|
def doc_index_page?
|
|
|
|
@doc && request.path == "/#{@doc['slug']}/"
|
|
|
|
end
|
2015-01-03 15:24:07 +01:00
|
|
|
|
|
|
|
def query_string_for_redirection
|
|
|
|
request.query_string.empty? ? nil : "?#{request.query_string}"
|
|
|
|
end
|
2013-10-24 20:25:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
halt erb :unsupported if unsupported_browser?
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/manifest.appcache' do
|
|
|
|
content_type 'text/cache-manifest'
|
2014-12-14 23:42:57 +01:00
|
|
|
expires 0, :'no-cache'
|
2013-10-24 20:25:52 +02:00
|
|
|
erb :manifest
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/' do
|
|
|
|
return redirect '/' unless request.query_string.empty?
|
|
|
|
erb :index
|
|
|
|
end
|
|
|
|
|
2014-12-31 21:53:35 +01:00
|
|
|
%w(offline about news help).each do |page|
|
2013-10-24 20:25:52 +02:00
|
|
|
get "/#{page}" do
|
|
|
|
redirect "/#/#{page}", 302
|
|
|
|
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
|
|
|
|
|
2014-12-14 18:29:34 +01:00
|
|
|
get '/s/maxcdn' do
|
|
|
|
redirect 'https://www.maxcdn.com/?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/s/shopify' do
|
|
|
|
redirect 'http://www.shopify.com/careers?utm_source=devdocs&utm_medium=banner&utm_campaign=devdocs'
|
|
|
|
end
|
|
|
|
|
2015-01-03 14:39:25 +01:00
|
|
|
get %r{\A/feed(?:\.atom)?\z} do
|
2014-11-30 21:55:26 +01:00
|
|
|
content_type 'application/atom+xml'
|
|
|
|
settings.news_feed
|
|
|
|
end
|
|
|
|
|
2014-12-07 17:54:20 +01:00
|
|
|
get '/s/tw' do
|
|
|
|
redirect 'https://twitter.com/intent/tweet?url=http%3A%2F%2Fdevdocs.io&via=DevDocs&text=All-in-one%2C%20quickly%20searchable%20API%20docs%3A'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/s/fb' do
|
|
|
|
redirect 'https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fdevdocs.io'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/s/re' do
|
|
|
|
redirect 'http://www.reddit.com/submit?url=http%3A%2F%2Fdevdocs.io&title=All-in-one%2C%20quickly%20searchable%20API%20docs&resubmit=true'
|
|
|
|
end
|
|
|
|
|
2015-01-03 15:15:46 +01:00
|
|
|
get %r{\A/(\w+)(\-[\w\-]+)?(/.*)?\z} do |doc, type, rest|
|
2013-11-09 11:12:54 +01:00
|
|
|
return 404 unless @doc = settings.docs[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}"
|
2013-11-09 11:12:54 +01:00
|
|
|
else
|
|
|
|
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'
|
|
|
|
link.href = 'http://devdocs.io/feed.atom'
|
|
|
|
link.type = 'application/atom+xml'
|
|
|
|
end
|
|
|
|
|
|
|
|
maker.channel.links.new_link do |link|
|
|
|
|
link.rel = 'alternate'
|
|
|
|
link.href = 'http://devdocs.io/'
|
|
|
|
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|
|
|
|
|
desc.content = news[1..-1].join.gsub("\n", '<br>').gsub('href="/', 'href="http://devdocs.io/')
|
|
|
|
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'
|
|
|
|
link.href = 'http://devdocs.io/'
|
|
|
|
link.type = 'text/html'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
set :news_feed, feed.to_s
|
|
|
|
end
|
2013-10-24 20:25:52 +02:00
|
|
|
end
|