From 019ad29a7ff3d461dd4abc3eecd2d8acaaaa4bfb Mon Sep 17 00:00:00 2001 From: Jonathan Rudenberg Date: Sat, 27 Oct 2012 19:55:03 -0400 Subject: [PATCH] Set up assets for S3/CloudFront CDN --- Gemfile | 2 +- Gemfile.lock | 37 +++++++++++++++++++++++++++++++++++-- Rakefile | 12 ++++++++++++ Rules | 1 + config.yaml | 1 + config/asset_sync.rb | 13 +++++++++++++ content/about/_faq.md | 4 ++-- content/docs/index.md | 2 +- content/index.slim | 2 +- layouts/_head.slim | 6 +++--- lib/asset_url.rb | 22 ++++++++++++++++++++++ lib/default.rb | 1 + 12 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 Rakefile create mode 100644 config/asset_sync.rb create mode 100644 lib/asset_url.rb diff --git a/Gemfile b/Gemfile index f1cd632..a604cc1 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gem 'slim' gem 'rainpress' gem 'nokogiri' gem 'adsf' -gem 'builder' gem 'tent-schemas', :git => 'git://github.com/tent/tent-schemas.git', :branch => 'master' gem 'pygments.rb' gem 'fssm', :groups => :development +gem 'asset_sync', :git => 'git://github.com/titanous/asset_sync.git', :branch => 'fix-mime' diff --git a/Gemfile.lock b/Gemfile.lock index cda0c65..774b96c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,24 +6,56 @@ GIT tent-schemas (0.0.1) json-schema +GIT + remote: git://github.com/titanous/asset_sync.git + revision: 64f86044921b7029cf3be673a1920aa7fdfe8604 + branch: fix-mime + specs: + asset_sync (0.5.0) + activemodel + fog + GEM remote: http://rubygems.org/ specs: + activemodel (3.2.8) + activesupport (= 3.2.8) + builder (~> 3.0.0) + activesupport (3.2.8) + i18n (~> 0.6) + multi_json (~> 1.0) adsf (1.1.1) rack (>= 1.0.0) - builder (3.1.4) + builder (3.0.4) clogger (1.1.0) rack (> 0.9) colored (1.2) cri (2.3.0) colored (>= 1.2) + excon (0.16.7) + fog (1.6.0) + builder + excon (~> 0.14) + formatador (~> 0.2.0) + mime-types + multi_json (~> 1.0) + net-scp (~> 1.0.4) + net-ssh (>= 2.1.3) + nokogiri (~> 1.5.0) + ruby-hmac + formatador (0.2.4) fssm (0.2.9) + i18n (0.6.1) json-schema (1.0.10) mime-types (1.19) + multi_json (1.3.6) nanoc (3.4.1) cri (~> 2.2) nanoc-cachebuster (0.3.1) nanoc (>= 3.3.0) + net-scp (1.0.4) + net-ssh (>= 1.99.1) + net-ssh (2.6.1) nokogiri (1.5.5) posix-spawn (0.3.6) puma (1.6.3) @@ -35,6 +67,7 @@ GEM rack-rewrite (1.3.0) rainpress (1.0) redcarpet (2.2.2) + ruby-hmac (0.4.0) slim (1.3.3) temple (~> 0.5.5) tilt (~> 1.3.3) @@ -47,7 +80,7 @@ PLATFORMS DEPENDENCIES adsf - builder + asset_sync! clogger fssm mime-types diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..559057c --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +require 'bundler/setup' + +task :gzip_assets do + Dir['output/assets/**/*.*'].reject { |f| f =~ /\.gz\z/ }.each do |f| + sh "gzip -c #{f} > #{f}.gz" unless File.exist?("#{f}.gz") + end +end + +task :deploy_assets => :gzip_assets do + require './config/asset_sync' + AssetSync.sync +end diff --git a/Rules b/Rules index 5e6fffe..7048273 100644 --- a/Rules +++ b/Rules @@ -40,6 +40,7 @@ compile '*' do when 'md' filter :schema_table filter :api_example + filter :erb filter :redcarpet, renderer: MarkdownHTML, options: { fenced_code_blocks: true, no_intra_emphasis: true, diff --git a/config.yaml b/config.yaml index fbccc76..4778609 100644 --- a/config.yaml +++ b/config.yaml @@ -85,6 +85,7 @@ base_url: http://tent.io title: Tent author_name: Tent author_uri: http://tent.io +cdn_url: https://d31dxsia6hg3x2.cloudfront.net # Configure the robots.txt file for this site. # Setting 'default' to true-ish will use sensible defaults. If you diff --git a/config/asset_sync.rb b/config/asset_sync.rb new file mode 100644 index 0000000..5fa1637 --- /dev/null +++ b/config/asset_sync.rb @@ -0,0 +1,13 @@ +require 'asset_sync' +require 'mime/types' + +AssetSync.configure do |config| + config.fog_provider = 'AWS' + config.fog_directory = ENV['S3_BUCKET'] + config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID'] + config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] + config.prefix = "assets" + config.public_path = Pathname("./output") + config.gzip_compression = true + config.always_upload = %w( manifest.json ) +end diff --git a/content/about/_faq.md b/content/about/_faq.md index ed5f026..15d2f92 100644 --- a/content/about/_faq.md +++ b/content/about/_faq.md @@ -29,10 +29,10 @@ Tent servers maintain relationships between users and manage apps. Users create With Tent, your relationships and data are all in one place that you control. on centralized services, your data and relationships are fragmented across multiple apps and networks. They can be difficult to export, and information about you is often sold. Like email, Tent lets you take your data and relationships with you when you change providers #### How Tent Works -![diagram](/assets/images/tent_diagram.png) +![diagram](<%= image_url('tent_diagram.png') %>) #### How Other Services Work -![diagram](/assets/images/other_services.png) +![diagram](<%= image_url('other_services.png') %>) ### How can I use Tent? diff --git a/content/docs/index.md b/content/docs/index.md index f259d5b..3319929 100644 --- a/content/docs/index.md +++ b/content/docs/index.md @@ -88,4 +88,4 @@ https://cloudmir.com/ (Lindsay) and https://titanous.com (Jonathan) are both fol 6. Lindsay, Jonathan, and Jesse can each read, reply to, repost, or ignore the post as they wish. -![diagram](/assets/images/diagram.png) \ No newline at end of file +![diagram](<%= image_url('diagram.png') %>) diff --git a/content/index.slim b/content/index.slim index 5122cd8..239358c 100644 --- a/content/index.slim +++ b/content/index.slim @@ -2,7 +2,7 @@ h1.intro strong Tent | is a protocol for distributed social networking and personal data storage. -img.responsive#arrows src="/assets/images/arrows_top.png" +img.responsive#arrows src=image_url('arrows_top.png') .feature h2 Communicate diff --git a/layouts/_head.slim b/layouts/_head.slim index 9db9816..b10f1f9 100644 --- a/layouts/_head.slim +++ b/layouts/_head.slim @@ -1,8 +1,8 @@ meta name="viewport" content='width=device-width, initial-scale=1.0' title = "#{@item[:title] + ' ยท ' if @item[:title]}Tent - the decentralized social web" -link href='/assets/css/bootstrap.css' rel='stylesheet' type='text/css' media='screen' -link href='/assets/css/bootstrap-responsive.css' rel='stylesheet' type='text/css' media='screen' -link href='/assets/css/style.css' rel='stylesheet' type='text/css' media='screen' +link href=asset_url('bootstrap.css') rel='stylesheet' type='text/css' media='screen' +link href=asset_url('bootstrap-responsive.css') rel='stylesheet' type='text/css' media='screen' +link href=asset_url('style.css') rel='stylesheet' type='text/css' media='screen' link rel='alternate' type='application/atom+xml' title='Atom feed' href="/blog.xml" javascript: var _gaq = _gaq || []; diff --git a/lib/asset_url.rb b/lib/asset_url.rb new file mode 100644 index 0000000..e7a25f3 --- /dev/null +++ b/lib/asset_url.rb @@ -0,0 +1,22 @@ +module Nanoc3::Helpers::AssetURL + def asset_url(path, options = {}) + name, extension = path.match(/([^.]+).(.+)$/).to_a[1..-1] + extensions = extension.split('.') + + asset_dir = options[:asset_dir] || extensions.first + + filename = "content/assets/#{asset_dir}/#{name}.#{extension}" + fp = fingerprint(filename) + + path = "/assets/#{asset_dir}/#{name}#{fp}.#{extensions[0..extensions.size-2].join('.')}" + if ENV['USE_CDN'] + @site.config[:cdn_url] + path + else + path + end + end + + def image_url(path) + asset_url(path, :asset_dir => 'images') + end +end diff --git a/lib/default.rb b/lib/default.rb index d8f32e1..cd28bc2 100644 --- a/lib/default.rb +++ b/lib/default.rb @@ -8,5 +8,6 @@ include Nanoc3::Helpers::Blogging include Nanoc3::Helpers::Filtering include Nanoc3::Helpers::LinkTo include Nanoc3::Helpers::CacheBusting +include Nanoc3::Helpers::AssetURL Slim::Engine.set_default_options pretty: true