lib/doraemon/proxy_server.rb in doraemon-1.0.12 vs lib/doraemon/proxy_server.rb in doraemon-1.0.13

- old
+ new

@@ -1,121 +1,91 @@ +#!/usr/bin/env ruby +# -*- coding: utf-8 -*- + require 'ritm' require 'json' require 'cert' -$db = Hash.new() +require_relative 'database' +require_relative 'context' module Doraemon class ProxyServer - def initialize(port=8000, root) + attr_accessor :scene_id + + def initialize(port=7000, scene_id) @port = port - @root = root + @scene_id = scene_id + @mutex = Mutex.new end def start() Cert.generate_cert + puts "Proxy server start, port: #{@port}, scene_id: #{@scene_id}" + _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 + ssl_reverse_proxy[:bind_port] = _port + 10000; proxy[:bind_address] = '0.0.0.0' proxy[:bind_port] = _port end _session.on_response do |_req, _resp| - puts "-----------------------------------------------------------------" - puts " - URI: #{_req.request_uri}" + # NOTE: 这里每次回调会在单独线程进行 + next unless _req.request_uri.host.end_with?('idongjia.cn') - name = _req.path - if name.start_with?('/') - name = name[1, name.size - 1] - end - name = name.gsub('_', '__').gsub('/', '_') - - _api_path = nil - for ext in [".rb", ".json", ".api"] do - path = File.join(@root, "#{name}#{ext}") - if File.exist?(path) - _api_path = path - break - end - end - _params = begin JSON.parse(_req.body) rescue _req.body end _result = begin JSON.parse(_resp.body) rescue {} end - if _api_path + if @scene_id > 0 + _path = _req.path + DB.query_api_contents(@scene_id, _path) do |api| + if !api.nil? + _resp_body = nil + begin + _result = eval(api['contents']) + _resp_body = _result.to_json + rescue Exception => e + puts "Exception rescue: #{e.full_message(order: :top)}" + _result = { + "code": -1, + "msg": "#{_path} 处理错误: #{e.to_s}" + } + _resp_body = _result.to_json + end - begin - _ctx_path = File.join(@root, ".context.rb") - eval(File.read(_ctx_path)) if File.exist?(_ctx_path) - _result = eval(File.read(_api_path)) if File.exist?(_api_path) - rescue Exception => e - _result = { - "code": -1, - "msg": "#{_req.path} 处理错误: #{e.to_s}" - } - end + _resp.status = 200 + _resp.body = _resp_body + _resp.header['content-length'] = _resp.body.bytesize + _resp.header['content-type'] = 'application/json; charset=utf-8' - _resp.status = 200 - _resp.body = _result.to_json - _resp.header['content-length'] = _resp.body.bytesize - _resp.header['content-type'] = 'application/json; charset=utf-8' - - end - - print_req_body = false - print_resp_body = false - req_content_type = _req.header['content-type'] - ["text", "json"].each do | t | - if req_content_type.class == Array - req_content_type.each do | tt | - if tt.downcase.index(t) != nil - print_req_body = true - end + puts "-----------------------------------------------------------------" + puts " - PATH: #{_req.request_uri}" + puts " - Request: #{_params.to_json}" + puts " - Response: #{_resp_body}" end end - if _resp.header['content-type'].downcase.index(t) != nil - print_resp_body = true - end end - if print_req_body - puts " - Request: #{_req.body}\n" - else - if _req.body.class != NilClass - puts " - Response: <#{_req.body.length}>" - end - end - - if print_resp_body - puts " - Response: #{_resp.body}\n" if print_resp_body - else - if _resp.body.class != NilClass - puts " - Response: <#{_resp.header['content-type']}>, size #{_resp.body.length}" - end - end - - puts "\n" - end _session.start - - # trap 'INT' do - # _session.shutdown - # return - # end - # loop { gets } + trap 'INT' do + _session.shutdown + return + end + loop { gets } end end end \ No newline at end of file