-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (127 loc) · 4.56 KB
/
app.py
File metadata and controls
149 lines (127 loc) · 4.56 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sqlalchemy
from flask import Flask, redirect
from flask_jwt_extended import JWTManager
from flask_cors import CORS
from dotenv import load_dotenv
from src.models import db
from src.routes import init_app
from flasgger import Swagger
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime, UTC
from src.models.listing_model import Listing
load_dotenv()
def create_app():
app = Flask(__name__)
required_env_vars = {
"DB_SERVER": os.getenv("DB_SERVER"), # test
"DB_NAME": os.getenv("DB_NAME"),
"DB_USERNAME": os.getenv("DB_USERNAME"),
"DB_PASSWORD": os.getenv("DB_PASSWORD"),
"DB_DRIVER": os.getenv("DB_DRIVER"),
"JWT_SECRET_KEY": os.getenv("JWT_SECRET_KEY"),
}
missing_vars = [var for var, value in required_env_vars.items() if not value]
if missing_vars:
raise SystemExit(f"Error: Missing environment variables: {', '.join(missing_vars)}")
app.config['SQLALCHEMY_DATABASE_URI'] = (
f"mssql+pyodbc://{required_env_vars['DB_USERNAME']}:"
f"{required_env_vars['DB_PASSWORD']}@"
f"{required_env_vars['DB_SERVER']}/"
f"{required_env_vars['DB_NAME']}?driver={required_env_vars['DB_DRIVER']}"
)
# app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456789@127.0.0.1:3306/freshdeallocal'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['UPLOAD_FOLDER'] = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'uploads')
app.config['JWT_SECRET_KEY'] = required_env_vars['JWT_SECRET_KEY']
JWTManager(app)
db.init_app(app)
with app.app_context():
db.create_all()
try:
from src.services.achievement_service import AchievementService
AchievementService.initialize_achievements()
print("Achievements initialized successfully")
except Exception as e:
print(f"Error initializing achievements: {e}")
try:
engine = sqlalchemy.create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
connection = engine.connect()
connection.close()
print("Database connection successful.")
except Exception as e:
print(f"Error connecting to the database: {e}")
CORS(app, resources={r"/*": {"origins": "*"}})
swagger_config = {
"openapi": "3.0.0",
"info": {
"title": "Freshdeal API",
"description": "API for Freshdeal application",
"version": "1.0.0",
"contact": {
"name": "Freshdeal",
"url": "https://github.com/FreshDealApp",
"email": "",
}
},
"servers": [
{"url": "https://freshdealbackend.azurewebsites.net/",
"description": "Production server"},
{"url": "http://localhost:8000", "description": "Local development server"},
],
"components": {
"securitySchemes": {
"BearerAuth": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
},
"security": [
{
"BearerAuth": []
}
],
"specs": [
{
"endpoint": 'apispec_1',
"route": '/apispec_1.json',
"rule_filter": lambda rule: True,
"model_filter": lambda tag: True,
}
],
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/swagger/",
"headers": [],
}
Swagger(app, config=swagger_config)
def update_all_listings():
with app.app_context():
try:
listings = Listing.query.filter(Listing.expires_at > datetime.now(UTC)).all()
for listing in listings:
listing.update_expiry()
db.session.commit()
except Exception as e:
db.session.rollback()
print(f"Error updating listings: {str(e)}")
scheduler = BackgroundScheduler()
scheduler.add_job(
func=update_all_listings,
trigger='interval',
hours=2,
id='update_listings_job',
name='Update listings fresh score and consume within time',
replace_existing=True
)
scheduler.start()
init_app(app)
@app.route('/')
def redirect_to_swagger():
return redirect('/swagger')
return app
app = create_app()
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=False)