create app factory pattern

This commit is contained in:
Sorrel Bri 2019-10-05 17:49:49 -07:00
parent c8d56b5e1e
commit 7c14f451bb
10 changed files with 62 additions and 68 deletions

View file

@ -3,7 +3,6 @@ from .users.user_endpoint import UserEndpoint
api = Blueprint('api', __name__, url_prefix='/api')
@api.route('/home', methods=['GET'])
def api_home():
response = {"message": "home page"}

58
app.py
View file

@ -1,58 +0,0 @@
import os
from flask import Flask
# ! SQLAlchemy > Marshmallow - these must be imported in this order
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_migrate import Migrate
from flask_bcrypt import Bcrypt
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# config database
app_settings = os.getenv(
'APP_SETTINGS',
'config.DevelopmentConfig'
)
app.config.from_object(app_settings)
# init bcrypt
bcrypt = Bcrypt(app)
# init database
db = SQLAlchemy(app)
# init marshmallow
ma = Marshmallow(app)
# init all db models
import models
migrate = Migrate(app, db)
# dev server
DEBUG = True
PORT = 8000
# Routes
@app.route('/')
def hello_world():
return 'Hello World'
# Blue prints
from api.api import api
from auth.auth import auth
app.register_blueprint(api)
app.register_blueprint(auth)
if __name__ == '__main__':
app.run(debug=DEBUG, port=PORT)

View file

@ -1,6 +1,6 @@
from flask import Blueprint, request, jsonify, session
import models
from models import User
print(User)
auth = Blueprint('auth', __name__, url_prefix='/auth')
@auth.route('/signup', methods=['POST'])

View file

@ -14,6 +14,7 @@ class DevelopmentConfig(BaseConfig):
DEBUG = True
BCRYPT_LOG_ROUNDS = 4
SQLALCHEMY_DATABASE_URI = DATABASE
PORT = 8000
class TestingConfig(BaseConfig):

7
config/models_mount.py Normal file
View file

@ -0,0 +1,7 @@
if __name__ == '__main__':
from ..models.User import User
from ..models.GameRoom import GameRoom
from ..models.TimeSettings import TimeSettings
from ..models.Game import Game
from ..models.Move import Move
from ..models.Message import Message

15
database.py Normal file
View file

@ -0,0 +1,15 @@
# ! SQLAlchemy > Marshmallow - these must be imported in this order
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
# init bcrypt
def bcrypt(app):
Bcrypt(app)
# init database
db = SQLAlchemy()
# init marshmallow
ma = Marshmallow()

View file

@ -1,6 +0,0 @@
from models.User import User
from models.GameRoom import GameRoom
from models.TimeSettings import TimeSettings
from models.Game import Game
from models.Move import Move
from models.Message import Message

View file

@ -1,4 +1,4 @@
from app import db, ma, bcrypt
from database import db, ma, bcrypt
import datetime
import enum
import jwt

36
server.py Normal file
View file

@ -0,0 +1,36 @@
import os
from database import db, ma, bcrypt
from flask import Flask
from flask_migrate import Migrate
from flask_cors import CORS
# Blueprints
from api.api import api
from auth.auth import auth
import config.models_mount
def create_app():
app = Flask(__name__)
bcrypt(app)
CORS(app)
app_settings = os.getenv(
'APP_SETTINGS',
'config.config.DevelopmentConfig'
)
app.config.from_object(app_settings)
db.init_app(app)
ma.init_app(app)
app.register_blueprint(api)
app.register_blueprint(auth)
migrate = Migrate(app, db)
return app
if __name__ == '__main__':
app = create_app()
app.run(port=8000, debug=True)