#Flask deployment crashed

44 messages · Page 1 of 1 (latest)

dreamy lily
#

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.

hardy gustBOT
#

Project ID: c6894929-c040-4842-8c83-4516d57a14fe

bitter spadeBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

dreamy lily
#

c6894929-c040-4842-8c83-4516d57a14fe

thorny mantle
#

if name == "main"

dreamy lily
#

when i copied to discord, the underscores are gone, it's: name == "main"

thorny mantle
#

ah

#

You shoud declare app = create_app() outside if statement maybe

#

try that.

dreamy lily
#

you mean outside the: if name == "main"?

thorny mantle
#

yep

#

show me create_app's definition

dreamy lily
#

"""
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 app
thorny mantle
#

yeah declare it outside if statement

dreamy lily
#

I am getting the config from config.py that contains multiple configurations for multiple enivronments (development, tes, production...)

thorny mantle
dreamy lily
#

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?

thorny mantle
#

Insert variable in a service

#

from settings tab

dreamy lily
#

how?

#

found it

#

i added: FLASK_ENV=production, like this??

thorny mantle
#

Share screen shot

dreamy lily
#

with: FLASK_ENV=production

thorny mantle
#

Yep

#

Should be running now.

dreamy lily
#

yes its running

thorny mantle
#

🙂

dreamy lily
#

could you also provide how to connect it to postgresql??

thorny mantle
#

Is the service running on railway?

dreamy lily
#

yes,

#

but not table

thorny mantle
#

postgres is not on railway?

#

you should have host username and password.

dreamy lily
#

its like this:

thorny mantle
dreamy lily
#

the pguser, pgpassword are from railway, I haven't added any config myself

thorny mantle
#

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.

dreamy lily
#

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

thorny mantle
#

I think that is beyond the scope of this thread but checkout Flask-CORS and it's usage.