Skip to content

Commit

Permalink
Cache-Control
Browse files Browse the repository at this point in the history
  • Loading branch information
SkywalkerSpace committed Aug 28, 2020
1 parent d54493f commit 5391b2b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
36 changes: 18 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ async def __call__(self, scope, receive, send):
# request
request = HTTPRequest(**scope)
if request.method != 'GET':
error_start, error_body = gen_error_response(
response_start, response_body = gen_error_response(
405, 'Method %s not allowed.' % request.method)
await send(error_start)
await send(error_body)
await send(response_start)
await send(response_body)
return

# router
# ===== ping =====
if request.url in ('ping', 'ping/'):
text_start, text_body = gen_text_response('pong')
await send(text_start)
await send(text_body)
response_start, response_body = gen_text_response('pong')
await send(response_start)
await send(response_body)
return

# ===== thumbnail =====
Expand All @@ -39,35 +39,35 @@ async def __call__(self, scope, receive, send):
thumbnail_info = validator.thumbnail_info
except Exception as e:
logger.error(e)
error_start, error_body = gen_error_response(
response_start, response_body = gen_error_response(
400, 'Request invalid.')
await send(error_start)
await send(error_body)
await send(response_start)
await send(response_body)
return

# get or generate
try:
thumbnail = Thumbnail(**thumbnail_info)
body = thumbnail.body
thumbnail_start, thumbnail_body = gen_thumbnail_response(body)
# send
await send(thumbnail_start)
await send(thumbnail_body)
response_start, response_body = gen_thumbnail_response(body)
await send(response_start)
await send(response_body)
return
except Exception as e:
logger.error(e)
error_start, error_body = gen_error_response(
response_start, response_body = gen_error_response(
500, 'Generate failed.')
await send(error_start)
await send(error_body)
await send(response_start)
await send(response_body)
return

# ===== Not found =====
else:
error_start, error_body = gen_error_response(
response_start, response_body = gen_error_response(
404, 'Not Found.')
await send(error_start)
await send(error_body)
await send(response_start)
await send(response_body)
return


Expand Down
23 changes: 14 additions & 9 deletions seatable_thumbnail/http_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,26 @@ def gen_response_body(body):


def gen_error_response(status, error_msg):
error_start = gen_response_start(status, TEXT_CONTENT_TYPE)
error_body = gen_response_body(error_msg.encode('utf-8'))
response_start = gen_response_start(status, TEXT_CONTENT_TYPE)
response_body = gen_response_body(error_msg.encode('utf-8'))

return error_start, error_body
return response_start, response_body


def gen_text_response(text):
text_start = gen_response_start(200, TEXT_CONTENT_TYPE)
text_body = gen_response_body(text.encode('utf-8'))
response_start = gen_response_start(200, TEXT_CONTENT_TYPE)
response_body = gen_response_body(text.encode('utf-8'))

return text_start, text_body
return response_start, response_body


def gen_thumbnail_response(thumbnail):
thumbnail_start = gen_response_start(200, THUMBNAIL_CONTENT_TYPE)
thumbnail_body = gen_response_body(thumbnail)
response_start = gen_response_start(200, THUMBNAIL_CONTENT_TYPE)
response_body = gen_response_body(thumbnail)

return thumbnail_start, thumbnail_body
# Cache-Control
if thumbnail:
response_start['headers'].append([b'cache-control', b'public'])
response_start['headers'].append([b'cache-control', b'max-age=86400'])

return response_start, response_body

0 comments on commit 5391b2b

Please sign in to comment.