-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
72 lines (59 loc) · 1.93 KB
/
run.py
File metadata and controls
72 lines (59 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
from dotenv import load_dotenv
from app import create_app, db, migrate
from app.models import User
from flask_migrate import Migrate
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Load environment variables from .env file
load_dotenv()
# Get environment from .env file or default to development
config_name = os.environ.get('FLASK_ENV', 'production')
# Create the Flask application instance
app = create_app(config_name)
# Import models to ensure they're registered with SQLAlchemy before migration
from app.models import User # Import models after app creation
# Create CLI command for initializing the database
@app.cli.command('init-db')
def initialize_db():
"""Initialize the database."""
db.create_all()
print('Database initialized.')
# Create CLI command for creating an admin user
@app.cli.command('create-admin')
def create_admin():
"""Create an admin user."""
admin = User.query.filter_by(email='admin@winaldrugshop.com').first()
if admin:
print('Admin user already exists.')
return
admin = User(
email='admin@winaldrugshop.com',
password='Admin123', # This would be a strong password in production
first_name='Admin',
last_name='User',
is_admin=True
)
db.session.add(admin)
db.session.commit()
print('Admin user created.')
# Create a route to check if the API is running
@app.route('/')
def index():
return {
'message': 'Welcome to Winal Drug Shop API',
'version': '1.0.0',
'status': 'online'
}
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'User': User}
# Run the application
if __name__ == '__main__':
app.run(host=os.environ.get('FLASK_HOST', '0.0.0.0'),
port=int(os.environ.get('FLASK_PORT', 5000)))