来自 Sinatra 基础对象。获取包括基础对象的应用程序端口

发布于 2024-09-03 12:51:12 字数 699 浏览 3 评论 0原文

我有一个 Sinatra::Base 对象,我想将其包含在我的所有 Web 应用程序中。在该基类中,我有在启动时调用的配置方法。

我希望配置代码使用集中式数据库“注册”该服务。注册时需要发送的信息是有关如何联系此网络服务的信息……例如主机和端口。

然后,我计划建立一个监控服务,该服务将监控所有已注册的服务,并偶尔对它们进行 ping 操作,以确保它们仍然正常运行。

在配置方法中,我无法获取端口信息。 “self.settings.port”变量似乎在此方法中不起作用。

a) 关于如何获取端口有什么想法吗?我有主人。

b) 是否有一个 sinatra 插件已经可以做类似的事情,所以我不必自己编写它? :-)

//在我的 Sinatra::Base 代码中。让我们称之为register_me.rb

注册我<西纳特拉::基地

配置执行

//保存主机和端口信息到数据库

结束

获取“/check_status”

//返回状态

结束

<小时>

//在我的网络服务代码中

require register_me //此时,sinatra会初始化RegisterMe对象并调用configure

帖子('/blah')

//此特定 Web 服务的方法示例

结束

I have a Sinatra::Base object that I would like to include in all of my web apps. In that base class I have the configure method which is called on start-up.

I would like that configure code to 'register' that service with a centralized database. The information that needs to be sent when registering is the information on how to contact this web-service... things like host and port.

I then plan on having a monitoring service that will spin over all registered services and occasionally ping them to make sure they are still up and running.

In the configure method I am having trouble getting the port information. The 'self.settings.port' variable doesn't seem to work in this method.

a) any ideas on how to get the port? I have the host.

b) is there a sinatra plug-in that already does something like this so I don't have to write it myself? :-)

//in my Sinatra::Base code. lets call it register_me.rb

RegisterMe < Sinatra::Base

configure do

//save host and port information to database

end

get '/check_status'

//return status

end


//in my web service code

require register_me //at this point, sinatra will initialize the RegisterMe object and call configure

post ('/blah')

//example of a method for this particular web service

end

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

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

发布评论

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

评论(1

水晶透心 2024-09-10 12:51:12

Sinatra::Application.port 将返回正确的端口

Sinatra::Application.port will return the correct port

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