通过 Sinatra 应用程序将选项传递给rackup

发布于 2024-10-06 08:11:53 字数 1405 浏览 9 评论 0原文

我是 ruby​​ 新手,正在学习 Sinatra。虽然通过要求 'sinatra' 创建 Sinatra 站点并直接在其下设置路由非常简单并且有很好的文档记录,但通过要求 'sinatra/base' 创建应用程序并编写一个继承自 'Sinatra::Base' 的类虽然仍然相对容易,但文档却很少(可能是因为它是 Sinatra 的最新功能)。

这正是我正在做的事情。我在 Sinatra 部分没有遇到太多麻烦,但是在机架/瘦/服务器部分遇到了一些麻烦。显然有两种方法可以部署应用程序:使用 Sinatra 本身(使用 run! 方法)和使用rackup 文件(通常为 config.ru)。

使用 Sinatra 的 run! 方法非常直观,并且工作起来就像一个魅力,但显然如果我想在 heroku 上部署我的应用程序,它就不起作用。事实上,我在 GitHub 上遇到的几乎所有 Sinatra 应用程序都使用 config.ru 文件。

使用rackup文件可能同样直观,但我无法理解如何将选项从Sinatra应用程序传递到服务器(ir:端口)。我尝试将选项合并到rackup的默认选项数组中:

MyApp::App.default_options.merge!(
  :run  => false,
  :env  => :production,
  :port => 4567
)

run MyApp::App

通过直接向应用程序添加选项:

MyApp::App.set :port, 4567
MyApp::App.set :run, false
MyApp::App.set :env, :production

run MyApp::App

通过在应用程序类中设置选项:

module MyApp
  class App < Sinatra::Base
    set :port, 4567
    set :run, false
    set :env, :production

    # ...

  # config.ru
  require 'app'

  run MyApp::App

上面的所有方法都失败了,要么显示错误消息,要么不考虑任何选项。那么,在使用rackup文件时,有什么方法可以通过Sinatra应用程序将选项传递给rackup/thin/服务器吗?或者问题中的选项应该通过命令行选项直接传递到rackup/thin/服务器?

作为问题的参考,这里是我正在构建的小 Sinatra 应用程序: https://github.com/AzizLight /维基/

I'm new to ruby, learning Sinatra. While creating a Sinatra site by requiring 'sinatra' and setting up the routes directly under is pretty easy and rather well documented, creating an application by requiring 'sinatra/base' and writing a class that inherits from 'Sinatra::Base', while still relatively easy, is very poorly documented (maybe because it's a pretty recent feature of Sinatra).

And that's exactly what I am doing. I am not having too much trouble on the Sinatra part, however I am having a bit of trouble on the rackup/thin/server part. Apparently there are two ways to deploy the application: using Sinatra itself (using the run! method) and using a rackup file (typically config.ru).

Using Sinatra's run! method is extremely intuitive and works like a charm, but apparently it doesn't work if I want to deploy my app on heroku. As a matter of fact, almost all the Sinatra apps that I have encountered on GitHub use a config.ru file.

Using a rackup file might be equally intuitive, but I can't manage to understand how to pass options from the Sinatra app to the server (ir: the port). I tried to merge options to rackup's default options array:

MyApp::App.default_options.merge!(
  :run  => false,
  :env  => :production,
  :port => 4567
)

run MyApp::App

by adding options directly to the app:

MyApp::App.set :port, 4567
MyApp::App.set :run, false
MyApp::App.set :env, :production

run MyApp::App

by setting options from within the application class:

module MyApp
  class App < Sinatra::Base
    set :port, 4567
    set :run, false
    set :env, :production

    # ...

  # config.ru
  require 'app'

  run MyApp::App

All the methods above failed, either by showing error messages or by just not taking any of the options into consideration. So is there any way to pass options to rackup/thin/the sever via a Sinatra app when using a rackup file? Or the options in questions should be passed directly to rackup/thin/the sever via command-line options?

As a reference to the problem, here is the little Sinatra application I am building: https://github.com/AzizLight/Wiki/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉宸 2024-10-13 08:11:53

实际上,您将直接在命令行上或通过配置文件将选项传递给thin。查看所有选项:

$ thin -h

对于生产,请使用配置文件:

$ thin -C thin-production.yml -R config.ru start

这是一个瘦生产.yml 文件示例:

---
address: localhost
port: 3020
servers: 4
max_conns: 1024
max_persistent_conns: 512
timeout: 30
environment: production
pid: tmp/pids/thin-production.pid
log: log/thin-production.log
daemonize: true 

You're actully going to pass options to thin on the command line directly or via a configuration file. See all options:

$ thin -h

For production, use a configuration file:

$ thin -C thin-production.yml -R config.ru start

Here is an example thin-production.yml file:

---
address: localhost
port: 3020
servers: 4
max_conns: 1024
max_persistent_conns: 512
timeout: 30
environment: production
pid: tmp/pids/thin-production.pid
log: log/thin-production.log
daemonize: true 
人海汹涌 2024-10-13 08:11:53

我知道我在这里复活了一个古老的问题,但我遇到了另一个尚未提及的有用解决方案。如此rack wiki教程:

#\ 开头的第一行被视为选项,允许在配置文件中指定rackup 参数。

因此,如果您想将主机设置为 0.0.0.0 并将端口设置为 5656,您可以将以下行添加到 config.ru 的开头代码> 文件:

#\ -o 0.0.0.0 -p 5656

I know I'm resurrecting an ancient question here, but I came across another useful solution not yet mentioned. As stated in this rack wiki tutorial:

the first line starting with #\ is treated as if it was options, allowing rackup arguments to be specified in the config file.

So if you wanted to set your host to 0.0.0.0 and port to 5656, you would add the following line to the beginning of your config.ru file:

#\ -o 0.0.0.0 -p 5656
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文