A
A
Askhat Bikmetov2014-12-12 00:36:46
Ruby on Rails
Askhat Bikmetov, 2014-12-12 00:36:46

Where to store rack application initialization parameters?

Any rack application starts with config.ru, and since there is no single right way in Ruby tm , there are many approaches to organizing this "start point" of the entire project. In my current project, initialization is divided into two files, actually config.ru:

$:.unshift File.expand_path '..', __FILE__
require 'init'
run Awesomeapp

and init.rb:
require 'bundler
Bundler.require :default

Mongoid.load! 'db.yml', :development

Dragonfly.app.configure do
  datastore :mongoid
end

require 'app/models'
require 'app/lication'

I see no reason why the contents of init.rb should not be in config.ru, which actually prompted me to ask this question: how do you initialize the application? What is wrong with my method and what is good with yours?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2014-12-12
@AMar4enko

config.ru

require_relative 'config/boot'

run Grape::RackBuilder.application

config/boot.rb
# Defines our constants
RACK_ENV = ENV['RACK_ENV'] || 'development'  unless defined?(RACK_ENV)
APP_ROOT = File.expand_path('../..', __FILE__) unless defined?(APP_ROOT)

# Load our dependencies
require 'rubygems' unless defined?(Gem)
require 'bundler/setup'
# Load initializers
Bundler.require(:default, RACK_ENV)

require_relative 'settings'

ApplicationSettings.source File.expand_path('config/application.yml', APP_ROOT)
ApplicationSettings.root = APP_ROOT

require_relative 'database'

Dir[File.join(File.dirname(__FILE__),'initializers/**/*.rb')].each {|file_name| require file_name}

Rabl.configure do |config|
  config.include_json_root = false
end

Grape::RackBuilder.setup do
  %w{api models lib uploaders models abilities jobs search_indexes}.each do |path|
    add_source_path ApplicationSettings.root("#{path}/**/*.rb")
    add_source_path ApplicationSettings.root("#{path}/*.rb")
  end

  use Warden::Manager do |config|
    config.strategies.add(:public_access_token, PublicTokenStrategy)
    config.default_strategies :public_access_token
    config.failure_app = API::Application
    config.intercept_401 = false
  end

  use Rack::Config do |env|
    env['api.tilt.root'] = ApplicationSettings.root('rabl')
  end

  mount 'API::Application', to: '/api'

  logger ApplicationSettings.logger(:rackbuilder)
end.boot!

In addition to config.ru, application initialization is also needed in spec_helper for testing. So breaking it down is a must.

R
Renat Ibragimov, 2014-12-12
@MpaK999

Yes, in general, there is nothing wrong, it's just not clear why split into 2 files when there are 10 lines of code :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question