1
0
mirror of https://github.com/enpaul/kodak.git synced 2024-09-21 08:13:54 +00:00

Add initial server infrastructure

This commit is contained in:
Ethan Paul 2020-09-21 23:23:47 -04:00
parent 548a5c8b1d
commit 7daecb8e26
No known key found for this signature in database
GPG Key ID: C5F5542B54A4D9C6
3 changed files with 77 additions and 0 deletions

53
imagemonk/_server.py Normal file
View File

@ -0,0 +1,53 @@
import flask
from imagemonk import __about__
from imagemonk import configuration
from imagemonk import constants
from imagemonk import database
from imagemonk import exceptions
from imagemonk.resources import ResponseHeaders
def make_the_tea() -> None:
"""Just for fun
https://en.wikipedia.org/wiki/Hyper_Text_Coffee_Pot_Control_Protocol
"""
if flask.request.content_type == "message/coffeepot":
raise exceptions.IAmATeapotError(
f"Coffee brewing request for '{flask.request.path}' cannot be completed by teapot application"
)
def initialize_database() -> None:
"""Initialize the database connection"""
database.initialize(flask.current_app.appconfig)
class ImageMonkRequest(flask.Request):
"""Extend the default Flask request object to add custom application state settings"""
def make_response_headers(self) -> ResponseHeaders:
"""Create the headers dictionary of the standard response headers
This function should be used when determining response headers so that the header names,
their contents, and formatting are universal.
:returns: Dictionary of headers
"""
return {
constants.HTTP_HEADER_RESPONSE_VERSION: __about__.__version__,
}
class ImageMonkFlask(flask.Flask):
"""Extend the default Flask object to add the custom application config
There's probably an easier/more kosher way to do this, but ¯\\_(ツ)_/¯
"""
request_class = ImageMonkRequest
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.appconfig: configuration.ImageMonkConfig = configuration.load()

22
imagemonk/application.py Normal file
View File

@ -0,0 +1,22 @@
import flask_restful
from imagemonk import resources
from imagemonk._server import ImageMonkFlask
from imagemonk._server import initialize_database
from imagemonk._server import make_the_tea
APPLICATION = ImageMonkFlask(__name__)
API = flask_restful.Api(APPLICATION, catch_all_404s=True)
def _set_upload_limit() -> None:
APPLICATION.config["MAX_CONTENT_LENGTH"] = APPLICATION.appconfig.upload.size_limit
APPLICATION.before_request(make_the_tea)
APPLICATION.before_first_request(initialize_database)
APPLICATION.before_first_request(_set_upload_limit)
for resource in resources.RESOURCES:
API.add_resource(resource, *resource.routes)

View File

@ -1,6 +1,8 @@
from typing import Tuple
from imagemonk.resources._shared import ImageMonkResource
from imagemonk.resources._shared import ResponseBody
from imagemonk.resources._shared import ResponseHeaders
from imagemonk.resources.image import Image
from imagemonk.resources.image import ImageUpload
from imagemonk.resources.openapi import OpenAPI