Sha256: 6fc437202be7a3dd4bb4aa64dded464d112d6df4d1531f3718867a322dc6d8cf
Contents?: true
Size: 1.44 KB
Versions: 2
Compression:
Stored size: 1.44 KB
Contents
require 'ritm' require 'json' require 'cert' module Doraemon class ProxyServer def initialize(port=8000, root) @port = port @root = root end def start(api_list) Cert.generate_cert _session = Ritm::Session.new _port = @port _session.configure do ssl_reverse_proxy.ca[:pem] = Cert.cert_path ssl_reverse_proxy.ca[:key] = Cert.key_path proxy[:bind_address] = '0.0.0.0' proxy[:bind_port] = _port end _session.on_response do |_req, _resp| if api_list.include?(_req.path) puts "- - - > #{_req.path}" _params = begin JSON.parse(_req.body) rescue _req.body end _result = begin JSON.parse(_resp.body) rescue {} end begin _result = eval(File.read(File.join(@root, api_list[_req.path]))) rescue _result = { "code": -1, "msg": "#{_req.path} 处理错误" } end _resp.status = 200 _resp.body = _result.to_json _resp.header['content-length'] = _resp.body.bytesize _resp.header['content-type'] = 'application/json;charset=UTF-8' puts "#{JSON.pretty_generate(_result)}\n\n" end end _session.start # trap 'INT' do # _session.shutdown # return # end # loop { gets } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
doraemon-1.0.3 | lib/doraemon/proxy_server.rb |
doraemon-1.0.2 | lib/doraemon/proxy_server.rb |