diff --git a/server/app/__init__.py b/server/app/__init__.py index e1512dd590b55127e9aa54f2d9925aaa816a09f3..26ddaba0e37079db27ba2379135ad64eee5fec56 100644 --- a/server/app/__init__.py +++ b/server/app/__init__.py @@ -1,3 +1,5 @@ +import json + from flask import Flask, redirect, request from flask_uploads import configure_uploads from flask_uploads.extensions import IMAGES @@ -8,15 +10,49 @@ from app.apis import init_api from app.core import bcrypt, db, jwt, ma -def create_app(config_name="configmodule.DevelopmentConfig"): +def load_secret_config(mode, database): + with open("./secret-webconfig.json") as f: + config = json.load(f) + + mode_config = config[mode] + result = {"JWT_SECRET_KEY": mode_config["JWT_SECRET_KEY"]} + + if database == "postgre": + pg = mode_config["postgre"] + uri = f"postgresql://{pg['USER']}:{pg['PASSWORD']}@{pg['HOST']}:{str(pg['PORT'])}/{pg['DATABASE']}" + result["SQLALCHEMY_DATABASE_URI"] = uri + elif database == "lite": + lite = mode_config["lite"] + result["SQLALCHEMY_DATABASE_URI"] = lite["SQLALCHEMY_DATABASE_URI"] + + return result + + +def create_app(mode, database): """ Creates Flask app, returns it and a SocketIO instance. Call run on the SocketIO instance and pass in the Flask app to start the server. """ + if mode == "dev": + config_name = "configmodule.DevelopmentConfig" + elif mode == "prod": + config_name = "configmodule.ProductionConfig" + elif mode == "test": + config_name = "configmodule.TestingConfig" + else: + raise Exception("Invalid mode\nValid modes are: dev, prod and test") # Init flask app = Flask(__name__, static_url_path="/static", static_folder="static") + + # Init config app.config.from_object(config_name) + + # Init secret config + secret_config = load_secret_config(mode, database) + app.config.update(secret_config) + + print(app.config.get("SERVER_NAME")) app.url_map.strict_slashes = False with app.app_context(): diff --git a/server/configmodule.py b/server/configmodule.py index a8fb75a6ed46fab5966543850c8d08422473db46..bb24906d8e191e77401818d92f35bcdcd1ff8eb6 100644 --- a/server/configmodule.py +++ b/server/configmodule.py @@ -2,32 +2,6 @@ import os from datetime import timedelta -class DevDbConfig: - HOST = "localhost" - PORT = 5432 - USER = "postgres" - PASSWORD = "password" - DATABASE = "teknik8" - SQLALCHEMY_DATABASE_URI = "postgresql://" + USER + ":" + PASSWORD + "@" + HOST + ":" + str(PORT) + "/" + DATABASE - - -class TestDbConfig: - HOST = "localhost" - PORT = 5432 - USER = "postgres" - PASSWORD = "password" - DATABASE = "teknik8-test" - SQLALCHEMY_DATABASE_URI = "postgresql://" + USER + ":" + PASSWORD + "@" + HOST + ":" + str(PORT) + "/" + DATABASE - - -class LiteDevDbConfig: - SQLALCHEMY_DATABASE_URI = "sqlite:///database.db" - - -class LiteTestDbConfig: - SQLALCHEMY_DATABASE_URI = "sqlite:///test.db" - - class Config: DEBUG = False TESTING = False @@ -55,16 +29,19 @@ class Config: OPENAPI_SWAGGER_UI_URL = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/" -class DevelopmentConfig(Config, LiteDevDbConfig): +class DevelopmentConfig(Config): DEBUG = True + SERVER_NAME = "localhost:5000" -class TestingConfig(Config, LiteTestDbConfig): +class TestingConfig(Config): TESTING = True + SERVER_NAME = "localhost:5000" USER_LOGIN_LOCKED_ATTEMPTS = 4 USER_LOGIN_LOCKED_EXPIRES = timedelta(seconds=4) -class ProductionConfig(Config, LiteDevDbConfig): +class ProductionConfig(Config): DEBUG = False TESTING = False + SERVER_NAME = "130.237.227.40:5000" # teknikattan.sys.kth.se diff --git a/server/main.py b/server/main.py index bf4a239311a451132d5c1dfb2b262ce29f4ccb32..589a28452076f6793200c528b80b7739a64ab423 100644 --- a/server/main.py +++ b/server/main.py @@ -1,5 +1,33 @@ +import sys + from app import create_app +""" +Action-arg1: server(default), populate +Mode-arg2: dev(default), prod, test +Database-arg3: lite(default), postgre +""" + if __name__ == "__main__": - app, sio = create_app("configmodule.DevelopmentConfig") - sio.run(app, port=5000) + argv = sys.argv + + # action = argv[1] if len(argv) > 1 else "server" + mode = argv[1] if len(argv) > 1 else "dev" + # database = argv[3] if len(argv) > 3 else "lite" + + # if mode == "prod": + # database = "postgre" + + if mode == "dev" or mode == "test": + database = argv[2] if len(argv) > 2 else "lite" + elif mode == "prod": + database = "postgre" + else: + print("Invalid args") + print("Dev args: no args, 'dev lite' or 'dev postgre'") + print("Prod args: 'prod'\n") + sys.exit(-1) + + print(f"Starting server in {mode} mode with database {database}...") + app, sio = create_app(mode, database) + sio.run(app) diff --git a/server/populate.py b/server/populate.py index 71aba44a517e05cfcc7bacbaeb0b3ab445cd976f..714d8ae3900abd5345a702617132e14a1ffb75f5 100644 --- a/server/populate.py +++ b/server/populate.py @@ -3,6 +3,7 @@ This file will reset and polulate the database with some data. """ import random +import sys import app.database.controller as dbc from app import create_app, db @@ -102,7 +103,7 @@ def create_default_items(): ) for k in range(3): - dbc.add.question_alternative(f"Alternative {k}", f"Correct {k}", item_question.id) + dbc.add.question_alternative(item_question.id, alternative=f"Alternative {k}", correct=f"Correct {k}") # Add text components # TODO: Add images as components @@ -144,10 +145,27 @@ def create_default_items(): if __name__ == "__main__": - app, _ = create_app("configmodule.DevelopmentConfig") + argv = sys.argv + + mode = argv[1] if len(argv) > 1 else "dev" + if mode == "dev" or mode == "test": + database = argv[2] if len(argv) > 2 else "lite" + elif mode == "prod": + database = "postgre" + else: + print("Invalid args") + print("Dev args: no args, 'dev lite' or 'dev postgre'") + print("Prod args: 'prod'\n") + sys.exit(-1) + + print(f"Populating server in {mode} mode with database {database}...") + + app, _ = create_app(mode, database) with app.app_context(): db.drop_all() db.create_all() create_default_items() + + print("Task populating done") diff --git a/server/secret-webconfig.json b/server/secret-webconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..bf839d4504c033d39356051ba6360cd882dd0a33 --- /dev/null +++ b/server/secret-webconfig.json @@ -0,0 +1,40 @@ +{ + "prod":{ + "JWT_SECRET_KEY": "super-secret", + "postgre": { + "HOST" : "localhost", + "PORT" : 5432, + "USER" : "postgres", + "PASSWORD" : "password", + "DATABASE" : "teknik8" + } + }, + "dev":{ + "JWT_SECRET_KEY": "super-secret", + "postgre": { + "HOST" : "localhost", + "PORT" : 5432, + "USER" : "postgres", + "PASSWORD" : "password", + "DATABASE" : "teknik8-dev" + }, + "lite":{ + "SQLALCHEMY_DATABASE_URI":"sqlite:///database.db" + } + + + }, + "test":{ + "JWT_SECRET_KEY": "super-secret", + "postgre": { + "HOST" : "localhost", + "PORT" : 5432, + "USER" : "postgres", + "PASSWORD" : "password", + "DATABASE" : "teknik8-test" + }, + "lite":{ + "SQLALCHEMY_DATABASE_URI":"sqlite:///test.db" + } + } +} \ No newline at end of file diff --git a/server/tests/__init__.py b/server/tests/__init__.py index 0c9c485630f372ca2a34a29cb42cb42fb2fe2a89..48da072e873f5031dd163816d8da802c33776a33 100644 --- a/server/tests/__init__.py +++ b/server/tests/__init__.py @@ -6,7 +6,7 @@ DISABLE_TESTS = False @pytest.fixture def app(): - app, _ = create_app("configmodule.TestingConfig") + app, _ = create_app("test","lite") app.app_context().push() db.drop_all() db.create_all()