Skip to content

Commit

Permalink
v1.4.4 - bugfix (#125)
Browse files Browse the repository at this point in the history
*Fix*
* API /mock support POST method
*New*
* Update command line arg '-v'. Use '-vvv' for more detail.
* Logger time format change to 'hh:mm:ss,ms'
  • Loading branch information
zhaoye authored Mar 13, 2019
1 parent 3bccbd3 commit a87ebfe
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 32 deletions.
5 changes: 5 additions & 0 deletions lyrebird/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from lyrebird.base_server import ThreadServer
from lyrebird import application
from lyrebird.mock import context
from lyrebird.log import get_logger


logger = get_logger()


class Event:
Expand Down Expand Up @@ -96,6 +100,7 @@ def publish(self, channel, message, state=False, *args, **kwargs):
self.event_queue.put(Event(channel, message))
if state:
self.state[channel] = message
logger.debug(f'channel={channel} state={state}\nmessage:\n-----------\n{message}\n-----------\n')

def subscribe(self, channel, callback_fn, *args, **kwargs):
"""
Expand Down
23 changes: 14 additions & 9 deletions lyrebird/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def _init_stream_logger():

def _init_file_logger(custom_log_path=None):
file_formater = logging.Formatter(
fmt='%(asctime)s %(levelname)s [%(module)s] - %(threadName)s [PID] %(process)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
fmt='%(asctime)s %(levelname)s [%(module)s] - %(threadName)s [PID] %(process)s - %(message)s'
)
if custom_log_path:
log_file = custom_log_path
Expand All @@ -70,24 +69,30 @@ def _init_file_logger(custom_log_path=None):

def _setup_3rd_loggers():
socketio = logging.getLogger('socketio')
socketio.setLevel(logging.WARNING)
socketio.setLevel(logging.ERROR)

engineio = logging.getLogger('engineio')
engineio.setLevel(logging.WARNING)
engineio.setLevel(logging.ERROR)

mock = logging.getLogger('mock')
mock.setLevel(logging.WARNING)
mock.setLevel(logging.ERROR)
mock.addHandler(logging.StreamHandler())

werkzeug = logging.getLogger('werkzeug')
werkzeug.setLevel(logging.WARNING)
werkzeug.setLevel(logging.ERROR)


def init(custom_log_path=None):
_init_file_logger(custom_log_path=custom_log_path)
_setup_3rd_loggers()

logger:logging.Logger = logging.getLogger('lyrebird')
if application.config.get('verbose', False):
verbose = application.config.get('verbose', 0)
if verbose == 0:
logger.setLevel(logging.ERROR)
elif verbose == 1:
logger.setLevel(logging.WARNING)
elif verbose == 2:
logger.setLevel(logging.INFO)
elif verbose >= 3:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
6 changes: 2 additions & 4 deletions lyrebird/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def main():
parser = argparse.ArgumentParser(prog='lyrebird')

parser.add_argument('-V', '--version', dest='version', action='store_true', help='show lyrebird version')
parser.add_argument('-v', dest='verbose', action='store_true', help='Show verbose log')
parser.add_argument('-v', dest='verbose', action='count', default=0, help='Show verbose log')
parser.add_argument('--mock', dest='mock', type=int, help='Set mock server port, default port is 4272')
parser.add_argument('--proxy', dest='proxy', type=int, help='Set proxy server port, default port is 9090')
parser.add_argument('--data', dest='data', help='Set data dir, default is "./data/"')
Expand Down Expand Up @@ -83,10 +83,8 @@ def main():
except socket.gaierror as e:
logger.error('Failed to get local IP address, error occurs on %s' % e)

if args.verbose:
application._cm.config['verbose'] = True

# init file logger after config init
application._cm.config['verbose'] = args.verbose
log.init(args.log)

if args.mock:
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/mock/blueprints/api_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
api_mock = Blueprint('mock', __name__, url_prefix='/mock')


@api_mock.route('/')
@api_mock.route('/', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'])
@api_mock.route('/<path:path>', methods=['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS'])
def index(path=''):
# todo add request handlers
Expand Down
14 changes: 13 additions & 1 deletion lyrebird/mock/blueprints/apis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint
from flask import Blueprint, request
from flask_restful import Resource, Api
from .common import Status, WorkMode, Manifest
from .flow import Flow, FlowList
Expand All @@ -7,11 +7,23 @@
from .plugin import Plugin
from .menu import Menu
from .event import Event
from lyrebird.log import get_logger


logger = get_logger()


api = Blueprint('api', __name__, url_prefix='/api')
api_source = Api(api)

@api.after_request
def after_request(response):
"""
输出每条请求概要信息
"""
lyrebird_info = response.headers.get('lyrebird', default='')
logger.info(f'[On API]{response.status_code} {lyrebird_info} {request.method} {request.url[:100]}')
return response

api_source.add_resource(Status, '/status')
api_source.add_resource(Manifest, '/manifest')
Expand Down
10 changes: 6 additions & 4 deletions lyrebird/mock/handlers/handler_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def _parse_request(self):
self.flow['request'] = _request
context.application.cache.add(self.flow)

logger.debug(f'[On client request] {self.flow["request"]["url"]}')

def _read_origin_request_info_from_url(self):
path_index = self.request.url.index(self._raw_path)
url = self.request.url[path_index:]
Expand Down Expand Up @@ -211,10 +213,10 @@ def req2dict(request, output=None):

content_type = request.headers.get('Content-Type')
if not content_type:
output['binary_data'] = 'bin'
else:
content_type = content_type.strip()

output['data'] = RequestDataHelper.data2Str(request.data)
return

content_type = content_type.strip()
if content_type.startswith('application/x-www-form-urlencoded'):
if unziped_data:
output['data'] = urllib.parse.parse_qs(unziped_data.decode('utf-8'))
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/mock/handlers/path_not_found_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ class RequestPathNotFound:

def handle(self, handler_context):
if not handler_context.response:
handler_context.response = Response('Request path not found.', 404)
handler_context.response = Response('Request path not found\n', 404)
4 changes: 4 additions & 0 deletions lyrebird/mock/handlers/proxy_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from requests.packages import urllib3
from .. import context
import urllib
from lyrebird import application


# 关闭ssl警告
Expand All @@ -28,6 +29,9 @@ def handle(self, handler_context):
parsed_url = urllib.parse.urlparse(origin_url)
if not parsed_url.hostname:
return
elif parsed_url.hostname in ['localhost', '127.0.0.1', ] and parsed_url.port == application.config["mock.port"]:
handler_context.response = Response(response='Duplicate request path\n', status=400)
return

method = request.method
data = request.get_data() or request.form or None
Expand Down
16 changes: 6 additions & 10 deletions lyrebird/mock/mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def __init__(self):
self._working_thread = None
self.app = Flask('MOCK')

self.app.env = 'development'

self.app.jinja_env.block_start_string = '[%'
self.app.jinja_env.block_end_string = '%]'
self.app.jinja_env.variable_start_string = '[['
Expand All @@ -68,9 +70,12 @@ def __init__(self):
self.app.jinja_env.globals['version'] = VERSION

# async_mode = threading / eventlet / gevent / gevent_uwsgi
self.socket_io = SocketIO(self.app, async_mode='threading', log_output=False)
self.socket_io = SocketIO(self.app, async_mode='threading', logger=False)

# 存储socket-io
context.application.socket_io = self.socket_io
application._socketio = self.socket_io

# 生成过滤器实例
if self.conf:
self.port = self.conf.get('mock.port')
Expand Down Expand Up @@ -100,15 +105,6 @@ def index():
"""
return redirect(url_for('ui.index')+f'?v={VERSION}')

@self.app.after_request
def after_request(response: Response):
"""
输出每条请求概要信息
"""
lyrebird_info = response.headers.get('lyrebird', default='')
_logger.info(f'{response.status_code} {lyrebird_info} {request.method} {request.url[:100]}')
return response

def run(self):
server_ip = application.config.get('ip')
_logger.warning(f'start on http://{server_ip}:{self.port}')
Expand Down
8 changes: 7 additions & 1 deletion lyrebird/proxy/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from mitmproxy.tools.cmdline import mitmdump
from lyrebird.base_server import ThreadServer
from lyrebird import application
from lyrebird.log import get_logger


"""
HTTP proxy server
Expand All @@ -17,6 +19,9 @@
FLOW_PATH = CURRENT_PATH/'proxy_flow.py'


logger = get_logger()


class LyrebirdProxyServer(ThreadServer):

def __init__(self):
Expand All @@ -43,7 +48,8 @@ def __init__(self):

def run(self):
server_ip = application.config.get('ip')
info_msg(f'start on {server_ip}:{self.proxy_port}', f'{Fore.CYAN} ***请在被测设备上设置代理服务器地址***')
# info_msg(f'start on {server_ip}:{self.proxy_port}', f'{Fore.CYAN} ***请在被测设备上设置代理服务器地址***')
logger.warning(f'start on {server_ip}:{self.proxy_port} {Fore.CYAN} ***请在被测设备上设置代理服务器地址***')
mitm_arguments = [
'-s', str(FLOW_PATH),
'-p', self.proxy_port,
Expand Down
2 changes: 1 addition & 1 deletion lyrebird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
IVERSION = (1, 4, 3)
IVERSION = (1, 4, 4)
VERSION = ".".join(str(i) for i in IVERSION)
LYREBIRD = "Lyrebird " + VERSION

0 comments on commit a87ebfe

Please sign in to comment.