URL Shortening Leads to Error 502 Bad Gateway

I use a lot of file sharing and URL shortening, so I thought, ‘Why not create a hobby project for the same?’ Thus, I began tinkering with Cloudflare Tunnels. I purchased a domain name (unhosted), hosted the website locally on my machine, and, using a Cloudflare tunnel, pointed the domain to my localhost. Everything worked fine. Then, I implemented URL shortening. The app shortens the URL as expected, but when I open the shortened URL, I get ‘Error 502 Bad Gateway.’ What could be the reason for the error? I would be grateful if you could provide me with insights on this issue.

I have attached screenshots of the DNS configuration on Cloudflare and the error.

Thank you.

DNS Config:

Error Screenshot:

EDIT 0: I ran the following command systemctl status cloudflared to get cloudflared logs:

RR Request failed error="Unable to reach the origin service. The service may be down or it may not be responding to traffic from cloudflared: dial tcp connect: connection refused" connIndex=2 dest=https://mydomain.com/5TyriP event=0 ip=XXX.XX.XXX.XXX type=http

EDIT 1: I tried disabling the proxy for the CNAME records, but it made my localhost inaccessible over cloudflare tunnel.

EDIT 2: I have used Flask and SQLAlchemy. Below is the script:

from flask import Flask, request, redirect, render_template_string
from flask_sqlalchemy import SQLAlchemy
import random
import string

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///urls.db'
db = SQLAlchemy(app)

class Url(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    original_url = db.Column(db.String(512), nullable=False)
    short_url = db.Column(db.String(6), unique=True, nullable=False)

with app.app_context():

def generate_short_link():
    characters = string.ascii_letters + string.digits
    short_url = ''.join(random.choice(characters) for _ in range(6))
    return short_url

u/app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        original_url = request.form['url']
        short_url = generate_short_link()
        new_url = Url(original_url=original_url, short_url=short_url)
        return f'Short URL is: mydomain.com/{short_url}'
    return '''
    <!doctype html>
            <title>Welcome to mydomain</title>
            <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
            <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet">
            <!-- Include Material CSS, JS here -->
            <h1>Welcome to mydomain</h1>
            <form method="post">
                <div><input name="url" type="text" placeholder="Enter your URL here"></div>
                <button type="submit">Shorten URL</button>

def redirect_to_original(short_url):
    url = Url.query.filter_by(short_url=short_url).first_or_404()
    return redirect(url.original_url)

if __name__ == '__main__':
    app.run(debug=True, port=46381)