gpt4 book ai didi

ruby - 如何在 Ruby 中创建服务器(如 rails server 或 gem server)?

转载 作者:数据小太阳 更新时间:2023-10-29 08:34:42 26 4
gpt4 key购买 nike

许多 ruby​​ 框架实现了一个服务器来提供动态 html 文档。我希望创建我自己的服务器,以便我可以提供我自己的本地 rdoc 文档。如何在 ruby​​ 中创建服务器,如 rails servergem server?有人可以给我一个开始或指出一些有关如何执行此操作的文档吗?谢谢

最佳答案

Sinatra是一个很好的方法,尤其是当您动态生成 HTML 时。这是一个作为命令行工具的简单示例:

example.rb:

options = {}

parser = OptionParser.new do |opts|
opts.banner = "Usage: example [command] [options]"

opts.on("-a", "--address HOST") { |arg| options[:bind] = arg }
opts.on("-p", "--port PORT") { |arg| options[:port] = arg }
end

parser.parse(ARGV)

case command = ARGV.first.to_sym
when :serve
require "server"
Example::Server.run! options
else
raise "Unknown command: #{command}"
end

服务器.rb:

require "rubygems"
require "sinatra/base"

module Example
class Server < Sinatra::Base
get "/" do
"Generate some HTML here."
end
end
end

关于ruby - 如何在 Ruby 中创建服务器(如 rails server 或 gem server)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3748189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com