问题描述
限时送ChatGPT账号..我有一个 Sinatra::Base 对象,我想将它包含在我所有的网络应用程序中.在那个基类中,我有启动时调用的 configure 方法.
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.
然后,我计划拥有一个监控服务,该服务将监控所有已注册的服务,并偶尔 ping 它们以确保它们仍然正常运行.
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.
在配置方法中,我无法获取端口信息.'self.settings.port' 变量在这种方法中似乎不起作用.
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) 关于如何获取端口的任何想法?我有主持人.
a) any ideas on how to get the port? I have the host.
b) 是否有一个 sinatra 插件已经做了这样的事情,所以我不必自己编写?:-)
b) is there a sinatra plug-in that already does something like this so I don't have to write it myself? :-)
//在我的 Sinatra::Base 代码中.让我们称之为 register_me.rb
//in my Sinatra::Base code. lets call it register_me.rb
注册我<辛纳特拉::基地
RegisterMe < Sinatra::Base
配置做
//保存主机和端口信息到数据库
//save host and port information to database
结束
获取'/check_status'
get '/check_status'
//返回状态
结束
//在我的网络服务代码中
//in my web service code
require register_me//此时sinatra会初始化RegisterMe对象并调用configure
require register_me //at this point, sinatra will initialize the RegisterMe object and call configure
发布 ('/blah')
post ('/blah')
//此特定网络服务的方法示例
//example of a method for this particular web service
结束
推荐答案
Sinatra::Application.port 将返回正确的端口
Sinatra::Application.port will return the correct port
这篇关于来自 Sinatra Base 对象.获取应用程序的端口,包括基础对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论