R
Railway•10mo ago
IssaKass

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.
No description
30 Replies
Percy
Percy•10mo ago
Project ID: c6894929-c040-4842-8c83-4516d57a14fe
IssaKass
IssaKass•10mo ago
c6894929-c040-4842-8c83-4516d57a14fe
King Jahad
King Jahad•10mo ago
if name == "main"
IssaKass
IssaKass•10mo ago
when i copied to discord, the underscores are gone, it's: name == "main"
King Jahad
King Jahad•10mo ago
ah You shoud declare app = create_app() outside if statement maybe try that.
IssaKass
IssaKass•10mo ago
you mean outside the: if name == "main"?
King Jahad
King Jahad•10mo ago
yep show me create_app's definition
IssaKass
IssaKass•10mo ago
""" 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
King Jahad
King Jahad•10mo ago
yeah declare it outside if statement
IssaKass
IssaKass•10mo ago
I am getting the config from config.py that contains multiple configurations for multiple enivronments (development, tes, production...)
King Jahad
King Jahad•10mo ago
Try this.
IssaKass
IssaKass•10mo ago
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?
King Jahad
King Jahad•10mo ago
Insert variable in a service from settings tab
IssaKass
IssaKass•10mo ago
how? found it i added: FLASK_ENV=production, like this??
King Jahad
King Jahad•10mo ago
Share screen shot
IssaKass
IssaKass•10mo ago
with: FLASK_ENV=production
No description
King Jahad
King Jahad•10mo ago
Yep Should be running now.
IssaKass
IssaKass•10mo ago
yes its running
King Jahad
King Jahad•10mo ago
🙂
IssaKass
IssaKass•10mo ago
could you also provide how to connect it to postgresql??
King Jahad
King Jahad•10mo ago
Is the service running on railway?
IssaKass
IssaKass•10mo ago
yes, but not table
King Jahad
King Jahad•10mo ago
postgres is not on railway? you should have host username and password.
IssaKass
IssaKass•10mo ago
its like this:
No description
King Jahad
King Jahad•10mo ago
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 …
IssaKass
IssaKass•10mo ago
the pguser, pgpassword are from railway, I haven't added any config myself
King Jahad
King Jahad•10mo ago
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.
IssaKass
IssaKass•10mo ago
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
King Jahad
King Jahad•10mo ago
I think that is beyond the scope of this thread but checkout Flask-CORS and it's usage.
IssaKass
IssaKass•10mo ago
It sometimes happens and sometimes not
Want results from more Discord servers?
Add your server