简体   繁体   English

如何配置Sinatra Rack应用程序以使用Figaro

[英]How to configure a sinatra rack app to use figaro

I saw this post and think something is missing or a change in version. 我看到了这篇文章,并认为某些内容丢失或版本更改。 I have a very simple rack app using figaro -- hosted with Heroku. 我有一个使用Figaro的非常简单的机架应用程序-由Heroku托管。 Currently, Im on local machine. 目前,我在本地计算机上。

I now need to use ENV . 我现在需要使用ENV

My app structure: 我的应用程序结构:

APP
 |_ config/
 |_ public/
 |_ views/
 |_ config.ru
 |_ app.rb
 |_ other-files

Inside config/application.yml 内部config / application.yml

SOME_KEY: some-value

Inside config.ru 内部config.ru

require './app.rb'
run MyApp.run!

This the part this gives the error. 这部分给出了错误。 Inside app.rb: 内部app.rb:

require  File.dirname(__FILE__) + '/config/application.yml'

[...]

This is the same line as in the link above. 此行与上面的链接相同。 I get 我懂了

cannot load such file /config/application.yml 无法加载此类文件/config/application.yml

In app.rb , I need to be able to do ENV['SOME_KEY'] app.rb ,我需要能够执行ENV['SOME_KEY']

You can't just require a yaml file as it is not ruby. 您不能仅require Yaml文件,因为它不是ruby。 Yaml is a file structure that is not ruby specific. Yaml是不是特定于ruby的文件结构。 Figaro was also written to be used with Ruby on Rails, not Sinatra. Figaro也被编写为与Ruby on Rails一起使用,而不是Sinatra。 You can probably figure out a way to make it work, but it's not as easy as just loading a yaml config file. 您可能会想出一种使其工作的方法,但它并不像加载yaml配置文件那样简单。

See this post for some ideas of how you might accomplish what you're trying to do. 有关如何完成您要完成的工作的一些想法,请参阅这篇文章。 Here is a forked version of the gem which might work for you. 这是宝石的分叉版本,可能对您有用。

https://github.com/laserlemon/figaro/pull/229 https://github.com/laserlemon/figaro/pull/229

I've done this before in a standalone app as follows: 我之前在一个独立的应用程序中已完成此操作,如下所示:

Figaro.application = Figaro::Application.new(
  environment: 'production',
  path: File.expand_path("config/application.yml")
)
Figaro.load

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM