diff --git a/bunnypy/bunnypy.py b/bunnypy/bunnypy.py index 2f48b88..fc866ad 100644 --- a/bunnypy/bunnypy.py +++ b/bunnypy/bunnypy.py @@ -8,7 +8,7 @@ from urllib.parse import parse_qs from wsgiref.simple_server import make_server -__version__ = "0.3.1" +__version__ = "0.3.2" __default_html__ = '''Welcome to BunnyPy @@ -32,9 +32,14 @@ ''' -__asset_dir__ = os.path.dirname(os.path.abspath(__file__)) + '/asset/' -__def_allowed_methods__ = ', '.join(['GET', 'POST', 'PUT', 'DELETE']) -__def_allowed_headers__ = ', '.join(['Content-Type', 'Authorization']) +__ASSET_DIR__ = os.path.dirname(os.path.abspath(__file__)) + '/asset/' +__ALLOWED_METHODS__ = ', '.join(['GET', 'POST', 'PUT', 'DELETE']) +__ALLOWED_HEADERS__ = ', '.join(['Content-Type', 'Authorization']) +__MIME_HEADERS__ = { + 'html': ('Content-Type', 'text/html;charset=utf-8'), + 'json': ('Content-Type', 'application/json;charset=utf-8'), + 'sse': ('Content-Type', 'text/event-stream;charset=utf-8'), +} class Bunny: @@ -48,12 +53,12 @@ def handler(self, environ, start_response): cors_conf = self.__cors__(environ) else: cors_conf = {} - allowed_origin = cors_conf['origins'] if 'origins' in cors_conf else '' + allowed_origin = ('Access-Control-Allow-Origin', cors_conf['origins'] if 'origins' in cors_conf else '') if environ['REQUEST_METHOD'] == 'OPTIONS' and 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in environ: start_response('200 OK', [ - ('Access-Control-Allow-Origin', allowed_origin), - ('Access-Control-Allow-Methods', __def_allowed_methods__), - ('Access-Control-Allow-Headers', __def_allowed_headers__), + allowed_origin, + ('Access-Control-Allow-Methods', __ALLOWED_METHODS__), + ('Access-Control-Allow-Headers', __ALLOWED_HEADERS__), ('Access-Control-Max-Age', '86400') ]) return [] @@ -64,9 +69,9 @@ def handler(self, environ, start_response): data = static_file.read() except FileNotFoundError: data = self.__render_error('404 Not Found') - start_response('200 OK', [('Access-Control-Allow-Origin', allowed_origin), - ('Content-Type', 'text/html;charset=utf-8')]) - return [data] + start_response('200 OK', [allowed_origin, __MIME_HEADERS__['html']]) + yield data + return [] url_array = environ['PATH_INFO'].split('/') if url_array[1] == '': url_array[1] = 'index' @@ -78,22 +83,18 @@ def handler(self, environ, start_response): req = self.Request(environ) response = self.__call_action__(req, url_array[1], action) if isinstance(response, dict) or isinstance(response, list): - start_response('200 OK', [('Access-Control-Allow-Origin', allowed_origin), - ('Content-Type', 'application/json;charset=utf-8')]) - return [json.dumps(response).encode('utf-8')] + start_response('200 OK', [allowed_origin, __MIME_HEADERS__['json']]) + yield json.dumps(response).encode('utf-8') elif isinstance(response, types.GeneratorType): - start_response('200 OK', [('Access-Control-Allow-Origin', allowed_origin), - ('Content-Type', 'text/event-stream;charset=utf-8')]) + start_response('200 OK', [allowed_origin, __MIME_HEADERS__['sse']]) for chunk in response: yield f'data: {chunk}\n\n'.encode('utf-8') else: - start_response('200 OK', [('Access-Control-Allow-Origin', allowed_origin), - ('Content-Type', 'text/html;charset=utf-8')]) - return [response.encode('utf-8')] + start_response('200 OK', [allowed_origin, __MIME_HEADERS__['html']]) + yield response.encode('utf-8') else: - start_response('200 OK', [('Access-Control-Allow-Origin', allowed_origin), - ('Content-Type', 'text/html;charset=utf-8')]) - return [__default_html__.encode('utf-8')] + start_response('200 OK', [allowed_origin, __MIME_HEADERS__['html']]) + yield __default_html__.encode('utf-8') def __init__(self, host='127.0.0.1', port=8000, database=None, cors=None): self.__host__ = host @@ -177,7 +178,7 @@ def render(self, view, context=None): def __render_error(self, msg, err_trace=None): return self.TemplateRender('error.html', { 'bunny_error': msg, 'bunny_error_trace': err_trace - }, __asset_dir__).render() + }, __ASSET_DIR__).render() class Request: def __init__(self, environ):