1

When I run my flask application on my local machine I am able to execute queries successfully from my sql database in google cloud. However, when I deploy my app onto the google cloud platform, it throws me a "502 Bad Gateway" error once it successfully builds.

Main.py file below:

imports

from flask import Flask, render_template, request
import mysql.connector
from mysql.connector.constants import ClientFlag

configuration/setup

config = {
    'user': 'user',
    'password': 'passhere!',
    'host': 'hosthere',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': 'ssl/file',
    'ssl_cert': 'ssl/file',
    'ssl_key': 'ssl/file',
    'database': 'db'
}

cnxn = mysql.connector.connect(**config)
app = Flask(__name__, template_folder="templates")

app route

@app.route("/page/", methods=['GET', 'POST'])
def page():
    if request.method == 'GET':
        return render_template('/page.html')
    elif request.method == 'POST':
        pick = request.form["pickzip"]
        pick = pick[:2]
        dest = request.form["destzip"]
        dest = dest[:2]
        ########################### SQL COMMAND GRABS LANE ####################################
        cursor = cnxn.cursor()
        cursor.execute("""SQL COMMAND""", (pick,dest,))
        data = cursor.fetchall()
        if data:
            headings = ("Carrier", "Actual", "Estimated", "Load Count")
            return render_template('/page.html',headings=headings, data=data)
        else:
            data = (["NULL", "NULL", "NULL", "NULL"])
            headings = ("Carrier", "Actual", "Estimated", "Load Count")
            return render_template('/page.html', headings=headings, data=data)
snakshak12
  • 72
  • 6
  • How are you deploying the app onto Google Cloud? A 502 generally indicates some load balancer can't reach your service. – Ray Terrill Oct 24 '22 at 20:52
  • I deployed it with App Engine if that answers your question? I feel load balancing wouldn't be necessary since there is not a whole lot of traffic going to the app currently. @RayTerrill – snakshak12 Oct 24 '22 at 21:04
  • I think you’re misunderstanding the load balancing piece. App Engine in this case is effectively acting as a load balancer and routing traffic to your app. It sounds like you need to adjust your flask app config to listen on port 8080. Have a look at this article: https://cloud.google.com/appengine/docs/standard/python3/building-app. I believe the default port for flask is 5000. – Ray Terrill Oct 25 '22 at 22:19

1 Answers1

0

It sounds like you need to adjust your flask app config to listen on port 8080. Have a look at this article: https://cloud.google.com/appengine/docs/standard/python3/building-app. I believe the default port for flask is 5000.

  • app is running on port 8080 already ---> if __name__ == "__main__": app.run(host="127.0.0.1",port=8080,debug=True) Is there something I need to change in the requirements.txt file? – snakshak12 Oct 26 '22 at 13:09
  • Can you post the full configuration for your app? That's part of what makes this difficult to diagnose - you're only posting parts of your configuration as opposed to the entire thing (the piece with setting your code to run on 8080 wasn't included). – Ray Terrill Oct 27 '22 at 17:52
  • Also have you configured your app as in this doc - specifically this section: https://cloud.google.com/appengine/docs/standard/python3/building-app/writing-web-service#configuring_your_web_service_for_app_engine – Ray Terrill Oct 27 '22 at 17:54