Initial
This commit is contained in:
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
25
app/app.py
Normal file
25
app/app.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from flask import Flask
|
||||
from app.config.config import get_config_by_name
|
||||
from app.initialize_functions import initialize_route,initialize_db
|
||||
|
||||
def create_app(config=None) -> Flask:
|
||||
"""
|
||||
Create a Flask application.
|
||||
|
||||
Args:
|
||||
config: The configuration object to use.
|
||||
|
||||
Returns:
|
||||
A Flask application instance.
|
||||
"""
|
||||
app = Flask(__name__)
|
||||
if config:
|
||||
app.config.from_object(get_config_by_name(config))
|
||||
|
||||
# Initialize extensions
|
||||
initialize_db(app)
|
||||
|
||||
# Register blueprints
|
||||
initialize_route(app)
|
||||
|
||||
return app
|
||||
0
app/config/__init__.py
Normal file
0
app/config/__init__.py
Normal file
34
app/config/config.py
Normal file
34
app/config/config.py
Normal file
@@ -0,0 +1,34 @@
|
||||
class BaseConfig:
|
||||
"""Base configuration."""
|
||||
DEBUG = False
|
||||
TESTING = False
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
SECRET_KEY = 'your-secret-key'
|
||||
|
||||
class DevelopmentConfig(BaseConfig):
|
||||
"""Development configuration."""
|
||||
DEBUG = True
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///development.db'
|
||||
|
||||
class TestingConfig(BaseConfig):
|
||||
"""Testing configuration."""
|
||||
DEBUG = True
|
||||
TESTING = True
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///testing.db'
|
||||
|
||||
class ProductionConfig(BaseConfig):
|
||||
"""Production configuration."""
|
||||
DEBUG = False
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///production.db'
|
||||
|
||||
|
||||
def get_config_by_name(config_name):
|
||||
""" Get config by name """
|
||||
if config_name == 'development':
|
||||
return DevelopmentConfig()
|
||||
elif config_name == 'production':
|
||||
return ProductionConfig()
|
||||
elif config_name == 'testing':
|
||||
return TestingConfig()
|
||||
else:
|
||||
return DevelopmentConfig()
|
||||
0
app/db/__init__.py
Normal file
0
app/db/__init__.py
Normal file
9
app/db/db.py
Normal file
9
app/db/db.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
db = SQLAlchemy(model_class=Base)
|
||||
14
app/initialize_functions.py
Normal file
14
app/initialize_functions.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from flask import Flask
|
||||
from app.modules.main.route import main_bp
|
||||
from app.db.db import db
|
||||
|
||||
|
||||
def initialize_route(app: Flask):
|
||||
with app.app_context():
|
||||
app.register_blueprint(main_bp)
|
||||
|
||||
|
||||
def initialize_db(app: Flask):
|
||||
with app.app_context():
|
||||
db.init_app(app)
|
||||
db.create_all()
|
||||
0
app/modules/__init__.py
Normal file
0
app/modules/__init__.py
Normal file
0
app/modules/main/__init__.py
Normal file
0
app/modules/main/__init__.py
Normal file
3
app/modules/main/controller.py
Normal file
3
app/modules/main/controller.py
Normal file
@@ -0,0 +1,3 @@
|
||||
class MainController:
|
||||
def index(self):
|
||||
return {'message':'Hello, World!'}
|
||||
10
app/modules/main/main_tests.py
Normal file
10
app/modules/main/main_tests.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import unittest
|
||||
import json
|
||||
|
||||
from app.modules.main.controller import MainController
|
||||
|
||||
|
||||
def test_index():
|
||||
main_controller = MainController()
|
||||
result = main_controller.index()
|
||||
assert result == {'message': 'Hello, World!'}
|
||||
11
app/modules/main/route.py
Normal file
11
app/modules/main/route.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from flask import Blueprint, make_response, jsonify
|
||||
from .controller import MainController
|
||||
|
||||
main_bp = Blueprint('main', __name__)
|
||||
|
||||
main_controller = MainController()
|
||||
|
||||
@main_bp.route('/')
|
||||
def index():
|
||||
result=main_controller.index()
|
||||
return make_response(jsonify(data=result))
|
||||
0
app/tests/__init__.py
Normal file
0
app/tests/__init__.py
Normal file
15
app/tests/conftest.py
Normal file
15
app/tests/conftest.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import pytest
|
||||
|
||||
from app.app import create_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
app = create_app('testing')
|
||||
app.config.update({"TESTING": True})
|
||||
yield app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
Reference in New Issue
Block a user