major api refactoring
parent
f0cdf02479
commit
80cc6e4430
@ -0,0 +1,38 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from flask_cors import CORS
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
|
||||
from api.settings import app_settings
|
||||
|
||||
# instantiate the extensions
|
||||
db = SQLAlchemy()
|
||||
cors = CORS()
|
||||
|
||||
|
||||
def create_app(script_info=None):
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# set config
|
||||
config_engine = app_settings[os.getenv("APP_SETTINGS", "dev")]()
|
||||
app.config.from_object(config_engine)
|
||||
|
||||
# set up extensions
|
||||
db.init_app(app)
|
||||
cors.init_app(app)
|
||||
|
||||
# migrate.init_app(app, db)
|
||||
|
||||
# register blueprints
|
||||
from .app import main_blueprint
|
||||
|
||||
app.register_blueprint(main_blueprint)
|
||||
# from project.api.users import users_blueprint
|
||||
# app.register_blueprint(users_blueprint)
|
||||
|
||||
# shell context for flask cli
|
||||
app.shell_context_processor({"app": app, "db": db})
|
||||
return app
|
@ -0,0 +1,21 @@
|
||||
import os
|
||||
from flask import Blueprint
|
||||
|
||||
|
||||
main_blueprint = Blueprint("main", __name__)
|
||||
|
||||
|
||||
__version__ = "1.2.0"
|
||||
|
||||
|
||||
@main_blueprint.route("/ping")
|
||||
@main_blueprint.route("/")
|
||||
def pong():
|
||||
"""A simple ping route."""
|
||||
return {"message": "pong"}
|
||||
|
||||
|
||||
@main_blueprint.route("/env")
|
||||
def env():
|
||||
"""Exposes the environment variables."""
|
||||
return dict(os.environ)
|
@ -0,0 +1,64 @@
|
||||
import os
|
||||
|
||||
|
||||
# TODO Add postgres params option
|
||||
def build_postgres_uri(host, user, dbname, passwd=None, port="5432", sslmode=None):
|
||||
"""Builds a PostgreSQL uri.
|
||||
|
||||
See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING for additional details.
|
||||
"""
|
||||
|
||||
if passwd:
|
||||
uri = f"postgresql://{user}:{passwd}@{host}:{port}/{dbname}"
|
||||
else:
|
||||
uri = f"postgresql://{user}@{host}:{port}/{dbname}"
|
||||
if sslmode:
|
||||
uri = f"{uri}?sslmode={sslmode}"
|
||||
return uri
|
||||
|
||||
|
||||
class BaseConfigEngine:
|
||||
"""Base configuration engine"""
|
||||
|
||||
POSTGRES_HOST = os.getenv("POSTGRES_HOST", default="localhost")
|
||||
POSTGRES_USER = os.getenv("POSTGRES_USER", default="postgres")
|
||||
POSTGRES_APP_DATABASE = os.getenv("POSTGRES_APP_DATABASE", default="postgres")
|
||||
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
|
||||
POSTGRES_PORT = os.getenv("POSTGRES_PORT", default="5432")
|
||||
POSTGRES_SSL_MODE = os.getenv("POSTGRES_SSL_MODE", default="prefer")
|
||||
POSTGRES_PARAMS = os.getenv("POSTGRES_PARAMS")
|
||||
SECRET_KEY = os.getenv("SECRET_KEY", "defaultsecretkey")
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
|
||||
@property
|
||||
def SQLALCHEMY_DATABASE_URI(self): # Note: all caps
|
||||
return build_postgres_uri(
|
||||
self.POSTGRES_HOST,
|
||||
self.POSTGRES_USER,
|
||||
self.POSTGRES_APP_DATABASE,
|
||||
self.POSTGRES_PASSWORD,
|
||||
self.POSTGRES_PORT,
|
||||
self.POSTGRES_SSL_MODE,
|
||||
)
|
||||
|
||||
|
||||
class DevConfigEngine(BaseConfigEngine):
|
||||
DEBUG = True
|
||||
|
||||
|
||||
class TestingConfigEngine(BaseConfigEngine):
|
||||
TESTING = True
|
||||
|
||||
|
||||
class ProductionConfigEngine(BaseConfigEngine):
|
||||
DEBUG = False
|
||||
|
||||
|
||||
app_settings = {
|
||||
"dev": DevConfigEngine,
|
||||
"development": DevConfigEngine,
|
||||
"test": TestingConfigEngine,
|
||||
"testing": TestingConfigEngine,
|
||||
"prod": ProductionConfigEngine,
|
||||
"production": ProductionConfigEngine,
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
|
||||
from backend.settings import build_postgres_uri
|
||||
from api.settings import build_postgres_uri
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
@ -0,0 +1,6 @@
|
||||
from api import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
@ -1,21 +0,0 @@
|
||||
import os
|
||||
from flask import Flask
|
||||
from flask_cors import CORS
|
||||
|
||||
app = Flask(__name__)
|
||||
cors = CORS(app)
|
||||
|
||||
__version__ = "1.0.0"
|
||||
|
||||
|
||||
@app.route("/ping")
|
||||
@app.route("/")
|
||||
def pong():
|
||||
"""A simple ping route."""
|
||||
return {"message": "pong"}
|
||||
|
||||
|
||||
@app.route("/env")
|
||||
def env():
|
||||
"""Exposes the environment variables."""
|
||||
return dict(os.environ)
|
@ -1,27 +0,0 @@
|
||||
import os
|
||||
|
||||
|
||||
def build_postgres_uri(
|
||||
host, user, dbname, port, sslmode=None, passwd=None, params=None
|
||||
):
|
||||
"""Builds a PostgreSQL uri.
|
||||
|
||||
See https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING for additional details.
|
||||
"""
|
||||
|
||||
if passwd:
|
||||
uri = f"postgresql://{user}:{passwd}@{host}:{port}/{dbname}"
|
||||
else:
|
||||
uri = f"postgresql://{user}@{host}:{port}/{dbname}"
|
||||
if sslmode:
|
||||
uri = f"{uri}?sslmode={sslmode}"
|
||||
return uri
|
||||
|
||||
|
||||
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
|
||||
POSTGRES_USER = os.getenv("POSTGRES_USER")
|
||||
POSTGRES_APP_DATABASE = os.getenv("POSTGRES_APP_DATABASE")
|
||||
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
|
||||
POSTGRES_PORT = os.getenv("POSTGRES_PORT", default="5432")
|
||||
POSTGRES_SSL_MODE = os.getenv("POSTGRES_SSL_MODE", default="prefer")
|
||||
POSTGRES_PARAMS = os.getenv("POSTGRES_PARAMS")
|
Loading…
Reference in New Issue