require 'rack/rpc' class Server < Rack::RPC::Server def hello_world "Hello, world!" end rpc 'hello_world' => :hello_world end
module RPC
require 'rack/rpc'
#require rpc/*.rb文件
Dir.glob(File.join(File.dirname(__FILE__), 'rpc', "*.rb")) do |file|
require file
end
class Runner < Rack::RPC::Server
#include rpc/*.rb and regsiter rpc call
#eg. rpc/god.rb god.hello
@@rpc_list = []
Dir.glob(File.join(File.dirname(__FILE__), 'rpc', "*.rb")) do |file|
rpc_class = File.basename(file).split('.rb')[0].capitalize
rpc_list = []
#加载module下的方法到Runner这个类下面
eval "include Frigga::RPC::#{rpc_class}"
#获取声明的RPC接口
eval "rpc_list = Frigga::RPC::#{rpc_class}::RPC_LIST"
rpc_list.each do |rpc_name|
#alias一个新的rpc方法,叫old_xxxx_xxxx
eval "alias :old_#{rpc_class.downcase}_#{rpc_name} :#{rpc_name}"
#重新定义rpc方法,添加一行日志打印功能,然后再调用old_xxxx_xxxx rpc方法
define_method "#{rpc_class.downcase}_#{rpc_name}".to_sym do |*arg|
Logger.info "[#{request.ip}] called #{rpc_class.downcase}.#{rpc_name} #{arg.join(', ')}"
eval "old_#{rpc_class.downcase}_#{rpc_name} *arg"
end
#注册RPC调用
rpc "#{rpc_class.downcase}.#{rpc_name}" => "#{rpc_class.downcase}_#{rpc_name}".to_sym
#添加到全局变量,汇总所有的rpc方法
@@rpc_list << "#{rpc_class.downcase}.#{rpc_name}"
end
end
def help
rpc_methods = (['help'] + @@rpc_list.sort).join("\n")
end
rpc "help" => :help
end
end #RPC
module RPC
module Ip
#RPC_LIST used for regsiter rpc_call
RPC_LIST = %w(list add del)
def list
$white_lists
end
def add(ip)
if ip =~ /^((25[0-5]|2[0-4]\d|[0-1]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[0-1]?\d\d?)$/
$white_lists << ip
write_to_file
return "succ"
else
return "fail"
end
end
def del(ip)
if $white_lists.include?(ip)
$white_lists.delete ip
write_to_file
return "succ"
else
return "fail"
end
end
def write_to_file
File.open(IP_yml, "w") do |f|
$white_lists.uniq.each {|i| f << "- #{i}\n"}
end
end
end
end
class ChefDSL
def template(path, &block)
TemplateDSL.new(path, &block)
end
end
class TemplateDSL
def initialize(path, &block)
@path = path
instance_eval &block
end
def source(source); @source = source; end
def owner(owner); @owner = owner; end
def mode(mode); @mode = mode; end
end
require 'colorize'
class Tip
def self.ask(stat = true, &block)
new(&block).ret if stat == true
end
attr_reader :ret
def initialize(&block)
@opt = []
@caller = {}
@banner = ""
@ret = false
self.instance_eval(&block)
print "#{@banner} [#{@opt.join('/')}]: ".light_yellow
loop do
x = gets.chomp.strip.to_sym
if @opt.include?(x)
@ret = ( @caller[x].call if @caller.key?(x) )
if @ret == :retry
print "\n#{@banner} [#{@opt.join('/')}]: ".light_yellow
next
else
return @ret
end
else
print "input error, please enter [#{@opt.join('/')}]: ".light_yellow
end
end
end
def on(opt, &block)
@opt << opt
@caller[opt] = block if block_given?
end
def banner(str)
@banner = str
end
end
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有