-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathapp.py
More file actions
80 lines (65 loc) · 2.19 KB
/
app.py
File metadata and controls
80 lines (65 loc) · 2.19 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
from flask import Flask, jsonify
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
from os import environ
from dotenv import load_dotenv
from datetime import timedelta
from config import Config
# Load environment variables
load_dotenv()
# Initialize SQLAlchemy
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
# Configure CORS
CORS(app,
resources={r"/*": {
"origins": Config.CORS_ORIGINS,
"methods": Config.CORS_METHODS,
"allow_headers": Config.CORS_HEADERS,
"supports_credentials": Config.CORS_SUPPORTS_CREDENTIALS
}})
# JWT Configuration
app.config['JWT_SECRET_KEY'] = environ.get('JWT_SECRET_KEY', 'your-secret-key')
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=1)
jwt = JWTManager(app)
@jwt.expired_token_loader
def expired_token_callback(jwt_header, jwt_data):
return jsonify({
'error': 'Token has expired',
'code': 'token_expired'
}), 401
@jwt.invalid_token_loader
def invalid_token_callback(error):
return jsonify({
'error': 'Invalid token',
'code': 'invalid_token'
}), 401
@jwt.unauthorized_loader
def missing_token_callback(error):
return jsonify({
'error': 'Authorization token is missing',
'code': 'authorization_required'
}), 401
# Database configuration
app.config['SQLALCHEMY_DATABASE_URI'] = environ.get('DATABASE_URL', 'sqlite:///planventure.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize extensions
db.init_app(app)
# Register blueprints
from routes.auth import auth_bp
from routes.trips import trips_bp
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(trips_bp, url_prefix='/api')
# Register routes
@app.route('/')
def home():
return jsonify({"message": "Welcome to PlanVenture API"})
@app.route('/health')
def health_check():
return jsonify({"status": "healthy"})
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)