Question

So i am trying to serve my static assets from Amazon s3 locally and for Heroku, I was loading a yml file but that doesn't work as Heroku doesn't accept symlinks.

So i was given the suggestion to use ENV variables as Heroku also uses these. I have a rake task to precompile the assets to AWS. two problems at the moment

1) My ENV variables are not being read.(Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank

2) When running a rake task i also get the error 'already initialized constant VALID_CHARACTER'

So the activesupport constant is being loaded twice?

My setup

env.rb

ENV['aws_bucket'] = 'bucketname'
ENV['aws_access_key'] = 'myaccesskey'
ENV['aws_secret_key'] = 'mysecretkey'

Rakefile

require 'bundler/setup'
 Bundler.require(:default)
 require './env' if File.exists?('env.rb')


AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")   
end

namespace :assets do
 desc "Precompile assets"
task :precompile do
AssetSync.sync
end

Gemfile

source :rubygems
gem 'sinatra'
gem 'pony'
gem 'sinatra-flash'
gem 'heroku'
gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'

UPDATE

AssetSync has activesupport in it's gemspec so that's getting included any way. It seems to be conflicting with the constant defined in the mail gem from the pony gemspec.

So with the Pony gem removed i can precompile assets locally, but when i try and compile for heroku nothing happens, it starts the rake task but then just goes back to the terminal ready for a new command.

The other thing is i need Pony for my mailer, how can i get around this?

Was it helpful?

Solution

To get rid of the clash between Pony and when running Rake locally, put the gems into different groups, e.g.

# Gemfile
group :assets do
  gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'
end

group :mail do
  gem "pony"
end

# more…

in the Rakefile

Bundler.require(:assets,:database,:whatever_else_you_need)

in the rackup/app file

Bundler.require(default,:assets,:database,:mail,:whatever_else_you_need)

As to your other problem, you should set the env vars for production via heroku config (see https://devcenter.heroku.com/articles/config-vars) and load them locally using the Rakefile as I said in the other question you asked about this. The env vars will live for the extent of the Ruby process, so if you load them in via Rake and also start the local server within the same Rake process you'll get Sinatra picking up all the env vars.


Edit: env vars will last as long as the process that added them, so if you put them in a dependent task the following task will have access to them:

namespace :assets do

  task :environment do
    AssetSync.configure do |con|
      con.fog_provider = 'AWS'
      con.fog_region = 'eu-west-1'
      con.fog_directory = ENV['aws_bucket']
      con.aws_access_key_id = ENV['aws_access_key']
      con.aws_secret_access_key = ENV['aws_secret_key']
      con.prefix = "assets"
      con.public_path = Pathname("./public")   
    end
  end

 desc "Precompile assets"
 task :precompile => :"assets:environment" do
   AssetSync.sync
 end

OTHER TIPS

You might want to split this into different questions. This makes it easier to help you. As for your first question: I assume you didn't put the env.rb under version control?

Why are your env vars not being picked up by Sinatra? Because you configure Fog in the Rakefile, and Simatra never sees that file. It's used by rake only.

I'd suggest you put the Fog configuration into a third file and require that in both Rakefile and Sinatra app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top