在与Sinatra合作几周后,我开始在临时环境中部署我的应用程序。在查看sinatra configuration settings并环顾四周之后,我没有找到一种方法来为每个环境配置一个配置文件,而不是这样:
require 'sinatra/base'
require 'sinatra/custom_logger'
class MyApp < Sinatra::Base
helpers Sinatra::CustomLogger
configure :development do
logger = MyCustomLogger::Logger.new(param1,
param2,
param3,
paramX)
set :logger, logger
...
end
configure :production do
logger = MyAnotherCustomerLogger.new(param1, param2)
set :logger, logger
end
configure :whatever do
end
# endpoints definition
end
我想得到更清洁的东西:
require 'sinatra/base'
require 'environment_config'
class MyApp < Sinatra::Base
register EnvironmentConfig # Not sure how..
...
end
class EnvironmentConfig
configuration :development do
# 10 lines of configuration
end
configuration: production do
# 20 lines of configuration
end
end
在该类/模块中,我们可以为每个环境提供一个文件,也可以为一个具有所有不同配置的文件。
我的问题是,有没有办法以一种配置与端点定义不在同一位置的方式构建Sinatra应用程序? 先谢谢你。
答案 0 :(得分:1)
尝试sinatra / config,您可以将所有配置设置存储在一个文件中,并提供特定的环境配置。
require "sinatra"
require "sinatra/contrib"
config_file '../config/config.yml'
get "/"
settings.bar
end
#config.yml
development:
foo: development
bar: bar
test:
foo: test
bar: bar
production:
foo: production
bar: bar
答案 1 :(得分:0)
我终于想出了一个使用self.included类方法的解决方案:
# config.rb
require 'sinatra/custom_logger'
module Config
def self.included(base_klass)
base_klass.extend(ClassMethods)
base_klass.helpers(Sinatra::CustomLogger)
base_klass.class_eval do
configure :development do
logger = MyCustomLogger.new(
param1,
param2,
param3,
paramx
)
set :logger, logger
end
configure :production do
# other stuff
end
end
end
module ClassMethods; end
end
然后
require_relative 'config'
class MyApp < Sinatra::Base
include Config