http_server_async: bugfixes
This commit is contained in:
parent
5b455035b2
commit
fd6e8856bd
|
@ -26,8 +26,8 @@ class Application:
|
||||||
self._server = None
|
self._server = None
|
||||||
|
|
||||||
self.cfg = Config(**kwargs)
|
self.cfg = Config(**kwargs)
|
||||||
#self.router = Router(trim_last_slash=True)
|
self.router = Router(trim_last_slash=True)
|
||||||
self.router = Router()
|
#self.router = Router()
|
||||||
self.middleware = DotDict({'request': [], 'response': []})
|
self.middleware = DotDict({'request': [], 'response': []})
|
||||||
|
|
||||||
for view in views:
|
for view in views:
|
||||||
|
@ -88,7 +88,7 @@ class Application:
|
||||||
|
|
||||||
def add_static(self, path, src):
|
def add_static(self, path, src):
|
||||||
if Path(src).isdir:
|
if Path(src).isdir:
|
||||||
path = Path(path).join('{path}')
|
path = Path(path).join('{path:.*}')
|
||||||
|
|
||||||
self.add_route(Static(src), path)
|
self.add_route(Static(src), path)
|
||||||
|
|
||||||
|
|
|
@ -298,8 +298,10 @@ class HeaderItem(list):
|
||||||
return ','.join(str(v) for v in self)
|
return ','.join(str(v) for v in self)
|
||||||
|
|
||||||
|
|
||||||
def set(self, *value):
|
def set(self, *values):
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
|
for value in values:
|
||||||
self.append(value)
|
self.append(value)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,10 @@ class Response:
|
||||||
|
|
||||||
@content_type.setter
|
@content_type.setter
|
||||||
def content_type(self, data):
|
def content_type(self, data):
|
||||||
|
try:
|
||||||
|
self.headers['Content-Type'].set(data)
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
self.headers['Content-Type'] = data
|
self.headers['Content-Type'] = data
|
||||||
|
|
||||||
|
|
||||||
|
@ -175,10 +179,13 @@ class Response:
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
data = data.encode('utf-8')
|
data = data.encode('utf-8')
|
||||||
|
|
||||||
|
elif isinstance(data, bytearray):
|
||||||
|
data = bytes(data)
|
||||||
|
|
||||||
elif any(map(isinstance, [data], [dict, list, tuple])):
|
elif any(map(isinstance, [data], [dict, list, tuple])):
|
||||||
data = json.dumps(data).encode('utf-8')
|
data = json.dumps(data).encode('utf-8')
|
||||||
|
|
||||||
else:
|
elif not isinstance(data, bytes):
|
||||||
data = str(data).encode('utf-8')
|
data = str(data).encode('utf-8')
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -1,14 +1,28 @@
|
||||||
import magic
|
import magic, mimetypes
|
||||||
|
|
||||||
from . import http_methods, error
|
from . import http_methods, error
|
||||||
|
|
||||||
|
from ..dotdict import DotDict
|
||||||
from ..path import Path
|
from ..path import Path
|
||||||
|
from ..template import Color
|
||||||
from ..exceptions import (
|
from ..exceptions import (
|
||||||
InvalidMethodException,
|
InvalidMethodException,
|
||||||
MethodNotHandledException
|
MethodNotHandledException
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
default_theme = DotDict(
|
||||||
|
primary = Color('#e7a'),
|
||||||
|
secondary = Color('#a7e'),
|
||||||
|
background = Color('#191919'),
|
||||||
|
positive = Color('#aea'),
|
||||||
|
negative = Color('#e99'),
|
||||||
|
white = Color('#eee'),
|
||||||
|
black = Color('#111'),
|
||||||
|
speed = 250
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class View:
|
class View:
|
||||||
__path__ = ''
|
__path__ = ''
|
||||||
|
|
||||||
|
@ -42,16 +56,16 @@ def Static(src):
|
||||||
|
|
||||||
with open(src_path, 'rb') as fd:
|
with open(src_path, 'rb') as fd:
|
||||||
data = fd.read()
|
data = fd.read()
|
||||||
mime = magic.from_buffer(data[:2048], mime=True)
|
mime = magic.from_buffer(data[:2048], mime=True) or mimetypes.guess_type(path)
|
||||||
|
|
||||||
except (FileNotFoundError, IsADirectoryError) as e:
|
except (FileNotFoundError, IsADirectoryError) as e:
|
||||||
raise error.NotFound('Static file not found')
|
raise error.NotFound('Static file not found')
|
||||||
|
|
||||||
headers = {}
|
|
||||||
|
|
||||||
response.body = data
|
response.body = data
|
||||||
response.content_type = mime
|
response.content_type = mime
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
return StaticHandler
|
return StaticHandler
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,5 +114,5 @@ class Style(View):
|
||||||
__path__ = '/framework/style.css'
|
__path__ = '/framework/style.css'
|
||||||
|
|
||||||
async def get(self, request, response):
|
async def get(self, request, response):
|
||||||
response.body = self.app.render('base.css')
|
response.body = self.app.render('base.css', default_theme)
|
||||||
response.content_type = 'text/css'
|
response.content_type = 'text/css'
|
||||||
|
|
Loading…
Reference in a new issue