Update
This commit is contained in:
parent
e371b7aabc
commit
be5bfa1fb5
10 changed files with 115 additions and 51 deletions
29
app.py
29
app.py
|
|
@ -1,29 +0,0 @@
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
from flask import Flask
|
|
||||||
from config import Config, DevelopmentConfig, StagingConfig, ProductionConfig
|
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
PORT = int(os.environ.get('PORT', 33507))
|
|
||||||
db1 = SQLAlchemy(app)
|
|
||||||
db2 = SQLAlchemy(app)
|
|
||||||
|
|
||||||
FLASK_ENV = os.environ.get('FLASK_ENV', None)
|
|
||||||
if FLASK_ENV == 'production':
|
|
||||||
DEBUG = ProductionConfig.DEBUG
|
|
||||||
elif FLASK_ENV == 'stage':
|
|
||||||
DEBUG = StagingConfig.DEBUG
|
|
||||||
elif FLASK_ENV == 'development':
|
|
||||||
DEBUG = DevelopmentConfig.DEBUG
|
|
||||||
else:
|
|
||||||
sys.exit("Error FLASK_ENV")
|
|
||||||
|
|
||||||
if Config.SQLALCHEMY_DATABASE_URI_1 is None or Config.SQLALCHEMY_DATABASE_URI_2 is None:
|
|
||||||
sys.exit("Error SQLALCHEMY_DATABASE_URI_1 or/and SQLALCHEMY_DATABASE_URI_2")
|
|
||||||
|
|
||||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = Config.SQLALCHEMY_TRACK_MODIFICATIONS
|
|
||||||
app.config['SECRET_KEY'] = Config.SECRET_KEY
|
|
||||||
app.run(host='0.0.0.0', port=PORT, debug=DEBUG)
|
|
||||||
6
backend/app.py
Normal file
6
backend/app.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from application import create_app
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host='0.0.0.0')
|
||||||
31
backend/application/__init__.py
Normal file
31
backend/application/__init__.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
from flask import Flask
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from ddtrace import patch_all
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
db = SQLAlchemy()
|
||||||
|
patch_all()
|
||||||
|
|
||||||
|
|
||||||
|
def create_app():
|
||||||
|
app = Flask(__name__)
|
||||||
|
FLASK_ENV = os.environ.get('FLASK_ENV', None)
|
||||||
|
if FLASK_ENV == 'production':
|
||||||
|
app.config.from_object("config.ProductionConfig")
|
||||||
|
elif FLASK_ENV == 'staging':
|
||||||
|
app.config.from_object("config.StagingConfig")
|
||||||
|
elif FLASK_ENV == 'development':
|
||||||
|
app.config.from_object("config.DevelopmentConfig")
|
||||||
|
else:
|
||||||
|
app.config.from_object("config.Config")
|
||||||
|
|
||||||
|
if app.config['SQLALCHEMY_DATABASE_URI_1'] is None or app.config['SQLALCHEMY_DATABASE_URI_2'] is None:
|
||||||
|
print('No ENV Variable for DATABASE_URL_1 or DATABASE_URL_2')
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
db.init_app(app)
|
||||||
|
with app.app_context():
|
||||||
|
from . import routes
|
||||||
|
db.create_all()
|
||||||
|
return app
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
from app import db2
|
from . import db
|
||||||
|
|
||||||
|
|
||||||
class Logs(db2.Model):
|
class Logs(db.Model):
|
||||||
__tablename__ = 'logs'
|
__bind_key__ = 'logs'
|
||||||
|
|
||||||
id = db2.Column(db2.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
date = db2.Column(db2.Date())
|
date = db.Column(db.Date())
|
||||||
user = db2.Column(db2.String())
|
user = db.Column(db.String())
|
||||||
ip = db2.Column(db2.String())
|
ip = db.Column(db.String())
|
||||||
table = db2.Column(db2.String())
|
table = db.Column(db.String())
|
||||||
action = db2.Column(db2.String())
|
action = db.Column(db.String())
|
||||||
status = db2.Column(db2.String())
|
status = db.Column(db.String())
|
||||||
|
|
||||||
def __init__(self, date, user, ip, table, action, status):
|
def __init__(self, date, user, ip, table, action, status):
|
||||||
self.date = date
|
self.date = date
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
from app import app
|
from flask import current_app as app
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
from app import app
|
from flask import current_app as app
|
||||||
from responses import send_message, send_error
|
from flask import request
|
||||||
|
from .logs_model import Logs, db
|
||||||
|
from .users_model import Users, db
|
||||||
|
from .responses import send_message, send_error
|
||||||
|
|
||||||
|
|
||||||
# Login
|
# Login
|
||||||
|
|
@ -11,7 +14,29 @@ def login():
|
||||||
# Register
|
# Register
|
||||||
@app.route('/api/register', methods=['POST'])
|
@app.route('/api/register', methods=['POST'])
|
||||||
def register():
|
def register():
|
||||||
return send_message('Register not implemented', None)
|
post_email = str(request.form['email'])
|
||||||
|
post_login = str(request.form['login'])
|
||||||
|
post_hashPass = str(request.form['hashPass'])
|
||||||
|
post_role = str(request.form['role'])
|
||||||
|
|
||||||
|
if post_email and post_login and post_hashPass and post_role:
|
||||||
|
user = Users.query.filter(
|
||||||
|
Users.email == post_email or Users.login == post_login
|
||||||
|
).first()
|
||||||
|
if user:
|
||||||
|
return send_message(f"{post_email} ({post_login}) already exist.", None)
|
||||||
|
user = Users(
|
||||||
|
email=post_email,
|
||||||
|
login=post_login,
|
||||||
|
hashPass=post_hashPass,
|
||||||
|
role=post_role
|
||||||
|
)
|
||||||
|
db.session.add(user)
|
||||||
|
db.session.commit()
|
||||||
|
return send_message('User registered.', user)
|
||||||
|
|
||||||
|
else:
|
||||||
|
return send_error(400, 'POST Request Error : Need email, login, hashPass and role fields.')
|
||||||
|
|
||||||
|
|
||||||
# Logout
|
# Logout
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
from app import db1
|
from . import db
|
||||||
|
|
||||||
|
|
||||||
class Users(db1.Model):
|
class Users(db.Model):
|
||||||
__tablename__ = 'users'
|
__bind_key__ = 'users'
|
||||||
|
|
||||||
id = db1.Column(db1.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
email = db1.Column(db1.String())
|
email = db.Column(db.String())
|
||||||
login = db1.Column(db1.String())
|
login = db.Column(db.String())
|
||||||
hashPass = db1.Column(db1.String())
|
hashPass = db.Column(db.String())
|
||||||
role = db1.Column(db1.String())
|
role = db.Column(db.String())
|
||||||
|
|
||||||
def __init__(self, email, login, hash_pass, role):
|
def __init__(self, email, login, hash_pass, role):
|
||||||
self.email = email
|
self.email = email
|
||||||
|
|
@ -1,14 +1,26 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
TESTING = False
|
TESTING = False
|
||||||
CSRF_ENABLED = True
|
CSRF_ENABLED = True
|
||||||
|
|
||||||
|
PORT = int(os.environ.get('PORT', 33507))
|
||||||
SECRET_KEY = os.environ.get('SECRET_KEY', None)
|
SECRET_KEY = os.environ.get('SECRET_KEY', None)
|
||||||
|
FLASK_APP = os.environ.get('FLASK_APP', None)
|
||||||
|
FLASK_ENV = os.environ.get('FLASK_ENV', None)
|
||||||
|
|
||||||
|
SQLALCHEMY_ECHO = False
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
SQLALCHEMY_DATABASE_URI_1 = os.environ.get('DATABASE_URL_1', None)
|
SQLALCHEMY_DATABASE_URI_1 = os.environ.get('DATABASE_URL_1', None)
|
||||||
SQLALCHEMY_DATABASE_URI_2 = os.environ.get('DATABASE_URL_2', None)
|
SQLALCHEMY_DATABASE_URI_2 = os.environ.get('DATABASE_URL_2', None)
|
||||||
|
SQLALCHEMY_BINDS = {
|
||||||
|
'users': SQLALCHEMY_DATABASE_URI_1,
|
||||||
|
'logs': SQLALCHEMY_DATABASE_URI_2
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ProductionConfig(Config):
|
class ProductionConfig(Config):
|
||||||
14
backend/manage.py
Normal file
14
backend/manage.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
from flask_script import Manager
|
||||||
|
from flask_migrate import Migrate, MigrateCommand
|
||||||
|
|
||||||
|
from flask import current_app as app
|
||||||
|
from . import db
|
||||||
|
|
||||||
|
migrate = Migrate(app, db)
|
||||||
|
manager = Manager(app)
|
||||||
|
|
||||||
|
manager.add_command('db', MigrateCommand)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
manager.run()
|
||||||
5
backend/requirements.txt
Normal file
5
backend/requirements.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
Flask_Migrate==3.1.0
|
||||||
|
ddtrace==0.56.1
|
||||||
|
Flask_SQLAlchemy==2.5.1
|
||||||
|
Flask_Script==2.0.6
|
||||||
|
Flask==2.0.2
|
||||||
Loading…
Add table
Add a link
Reference in a new issue