Flask deployment crashed
I have the following Flask folder structure. With the main.py containing:
import os
from api import create_app
if name == "main":
app = create_app()
app.run(debug=True, port=os.getenv("PORT", default=5000))
When deployed to railway, I got the following deploy log: Failed to find attribute 'app' in 'main'.
If I deploy without using create_app(), it works, but create_app() handles lots of configuration for the app.
30 Replies
Project ID:
c6894929-c040-4842-8c83-4516d57a14fe
c6894929-c040-4842-8c83-4516d57a14fe
if name == "main"
when i copied to discord, the underscores are gone, it's: name == "main"
ah
You shoud declare app = create_app() outside if statement maybe
try that.
you mean outside the: if name == "main"?
yep
show me create_app's definition
"""
Flask App Factory
This module provides a factory function
create_app
to create and configure a Flask app.
The app is configured based on the specified environment using the get_config
function from
the config
module. The following extensions are initialized:
- Flask-Migrate for database migrations
- Flask-CORS for enabling Cross-Origin Resource Sharing (CORS)
- Flask-JWT for JSON Web Token (JWT) support
The app registers several blueprints for organizing routes:
- auth_bp: Blueprint for authentication routes
- users_bp: Blueprint for user-related routes
- projects_bp: Blueprint for project-related routes
- subscriptions_bp: Blueprint for subscription-related routes
- export_bp: Blueprint for export-related routes
After configuration, the app context is updated with the current app's configuration.
Example Usage:
app = create_app(environment="production")
"""
import os
from flask import Flask, current_app, jsonify
from flask_migrate import Migrate
from flask_cors import CORS
from config import get_config
from api.extensions import db, jwt
from api.routes import auth_bp, users_bp, projects_bp, subscriptions_bp
def create_app():
"""Create and configure the Flask app."""
app = Flask(name)
# Load configuration based on the specified environment
flask_env = os.getenv("FLASK_ENV")
app.config.from_object(get_config(flask_env))
# Initialize the database extension
db.init_app(app)
# Initialize the migration extension
Migrate(app, db)
# Enable CORS for all routes
CORS(app)
# Initialize the JWT extension
jwt.init_app(app)
# Register the blueprints with a specified prefix
app.register_blueprint(auth_bp)
app.register_blueprint(users_bp)
app.register_blueprint(projects_bp)
app.register_blueprint(subscriptions_bp)
# Set the configuration in the current app context
with app.app_context():
current_app.config.update(app.config)
return appyeah declare it outside if statement
I am getting the config from config.py that contains multiple configurations for multiple enivronments (development, tes, production...)
Try this.
I am deploying now
that error is gone, but now: flask_env = os.getenv("FLASK_ENV") retunrs None
how i get the flask env dynamically from railway app?
Insert variable in a service
from settings tab
how?
found it
i added: FLASK_ENV=production, like this??
Share screen shot
with: FLASK_ENV=production
Yep
Should be running now.
yes its running
🙂
could you also provide how to connect it to postgresql??
Is the service running on railway?
yes,
but not table
postgres is not on railway?
you should have host username and password.
its like this:
PostgreSQL Documentation
34.1. Database Connection Control Functions
34.1. Database Connection Control Functions # 34.1.1. Connection Strings 34.1.2. Parameter Key Words The following functions deal with making a connection …
the pguser, pgpassword are from railway, I haven't added any config myself
use the non private url and you should be set.
you can use the private url but it will only work from within the project.
ok I'll do. Do you know why that error comes: Access to XMLHttpRequest at 'http://127.0.0.1:5000/auth/login' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
==> it's in local development
I'm enabling CORS
I think that is beyond the scope of this thread but checkout Flask-CORS and it's usage.
It sometimes happens and sometimes not