Convert to a django-list structure
This commit is contained in:
1
magic_app/__init__.py
Normal file
1
magic_app/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""The main application package."""
|
61
magic_app/app.py
Normal file
61
magic_app/app.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""The main application factory."""
|
||||
from logging import DEBUG, ERROR, getLogger
|
||||
|
||||
from celery import Celery
|
||||
from flask import Flask
|
||||
from redis import Redis
|
||||
|
||||
from magic_app.config import Base, Production
|
||||
|
||||
celery = Celery(__name__, broker=Base.CELERY_BROKER_URL)
|
||||
|
||||
|
||||
def create_app(config=Production):
|
||||
"""Main application factory."""
|
||||
app = Flask(__name__.split(".")[0])
|
||||
app.config.from_object(config)
|
||||
|
||||
celery.conf.update(app.config)
|
||||
|
||||
configure_redis(app)
|
||||
configure_views(app)
|
||||
configure_logging(app)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def configure_redis(app):
|
||||
"""Configure Redis connection."""
|
||||
from magic_app.session import RedisSessionDB
|
||||
|
||||
host = (Base.REDIS_HOST,)
|
||||
port = (Base.REDIS_PORT,)
|
||||
db_num = Base.REDIS_SESSION_DB
|
||||
|
||||
if app.debug:
|
||||
from fakeredis import FakeRedis
|
||||
|
||||
connection = FakeRedis()
|
||||
else:
|
||||
connection = Redis(host=host, port=port, db=db_num)
|
||||
|
||||
app.config["SESSION"] = RedisSessionDB(connection)
|
||||
|
||||
|
||||
def configure_views(app):
|
||||
"""Configure API resource views."""
|
||||
from magic_app.views import home
|
||||
|
||||
app.register_blueprint(home)
|
||||
|
||||
|
||||
def configure_logging(app):
|
||||
"""Configure application logging."""
|
||||
app_logger = getLogger(__name__)
|
||||
|
||||
if not app.debug:
|
||||
app_logger.setLevel(ERROR)
|
||||
else:
|
||||
app_logger.setLevel(DEBUG)
|
||||
|
||||
app.logging = app_logger
|
48
magic_app/config.py
Normal file
48
magic_app/config.py
Normal file
@ -0,0 +1,48 @@
|
||||
"""The Application settings."""
|
||||
from os import environ, pardir
|
||||
from os.path import abspath, dirname, join
|
||||
|
||||
|
||||
class Base:
|
||||
"""The base configuration."""
|
||||
|
||||
DEBUG = False
|
||||
JSON_AS_ASCII = False
|
||||
|
||||
APP_DIR = abspath(dirname(__file__))
|
||||
PROJECT_ROOT = abspath(join(APP_DIR, pardir))
|
||||
SWAGGER_DIR = abspath(join(PROJECT_ROOT, "swagger_docs"))
|
||||
|
||||
REDIS_HOST = environ["REDIS_HOST"]
|
||||
REDIS_PORT = environ["REDIS_PORT"]
|
||||
REDIS_SESSION_DB = int(environ["REDIS_SESSION_DB"])
|
||||
|
||||
CELERY_BROKER_URL = environ["CELERY_BROKER_URL"]
|
||||
CELERY_RESULT_BACKEND = environ["CELERY_RESULT_BACKEND"]
|
||||
|
||||
|
||||
class Development(Base):
|
||||
"""The Development configuration."""
|
||||
|
||||
ENV = "development"
|
||||
DEBUG = True
|
||||
CELERY_ALWAYS_EAGER = True
|
||||
|
||||
|
||||
class Testing(Base):
|
||||
"""The testing configuration."""
|
||||
|
||||
ENV = "testing"
|
||||
|
||||
|
||||
class Production(Base):
|
||||
"""The production configuration."""
|
||||
|
||||
ENV = "production"
|
||||
|
||||
|
||||
CONFIG = {
|
||||
"development": Development,
|
||||
"testing": Testing,
|
||||
"production": Production,
|
||||
}
|
32
magic_app/session.py
Normal file
32
magic_app/session.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""Session management interface module."""
|
||||
from json import dumps, loads
|
||||
|
||||
|
||||
class RedisSessionDB(dict):
|
||||
"""Dict interface for Redis session management."""
|
||||
|
||||
def __init__(self, connection):
|
||||
self.connection = connection
|
||||
|
||||
def keys(self):
|
||||
return self.connection.keys()
|
||||
|
||||
def flushall(self):
|
||||
self.connection.flushall()
|
||||
|
||||
def __getitem__(self, key):
|
||||
value = self.connection.get(key)
|
||||
if value is None:
|
||||
raise KeyError(key)
|
||||
utf8ified = str(value, "utf-8")
|
||||
return loads(utf8ified)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
value = dumps(value)
|
||||
self.connection.set(key, value)
|
||||
|
||||
def __contains__(self, item):
|
||||
return self.connection.exists(item)
|
||||
|
||||
def __delitem__(self, key):
|
||||
self.connection.delete(key)
|
7
magic_app/tasks.py
Normal file
7
magic_app/tasks.py
Normal file
@ -0,0 +1,7 @@
|
||||
"""Celery tasks module."""
|
||||
from magic_app.app import celery
|
||||
|
||||
|
||||
@celery.task
|
||||
def hello_world():
|
||||
print("Hello, World")
|
7
magic_app/templates/base.html
Normal file
7
magic_app/templates/base.html
Normal file
@ -0,0 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html>
|
||||
<head></head>
|
||||
<body></body>
|
||||
</html>
|
||||
</html>
|
9
magic_app/views.py
Normal file
9
magic_app/views.py
Normal file
@ -0,0 +1,9 @@
|
||||
"""View routing."""
|
||||
from flask import Blueprint
|
||||
|
||||
home = Blueprint("home", __name__)
|
||||
|
||||
|
||||
@home.route("/")
|
||||
def hello_world():
|
||||
return "Hello, World"
|
Reference in New Issue
Block a user