Commit 74dfbf54 authored by Muhammad Usman's avatar Muhammad Usman

first commit

parents
FROM python:3.8-alpine
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN python -m pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
COPY . /app
EXPOSE 8080
CMD [ "python", "./main.py" ]
from flask import Flask
from waitress import serve
from flaskext.mysql import MySQL
import json
import os
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = os.environ.get('DB_USER')
app.config['MYSQL_DATABASE_PASSWORD'] = os.environ.get('DB_PASSWORD')
app.config['MYSQL_DATABASE_DB'] = os.environ.get('DB_NAME')
app.config['MYSQL_DATABASE_HOST'] = os.environ.get('DB_HOST')
mysql.init_app(app)
@app.route('/')
def main_page():
return 'Main Page!'
@app.route('/hello')
def hello_world():
return 'Hello World!'
@app.route('/user/')
def users():
query = "select id,firstname,lastname,email,reg_date from users"
try:
conn = mysql.connect()
cursor =conn.cursor()
cursor.execute(query)
data = cursor.fetchall()
except Exception as e:
return "Execption in database: " + str(e)
response = {}
for row in data:
response[row[0]] = {"first_name":str(row[1]),"last_name":str(row[2]),"email":str(row[3]),"reg_date":str(row[4])}
if not response:
response = {"message":"record not found"}
response = json.dumps(response)
return response
if __name__ == '__main__':
if os.environ.get('APP_ENV') == "production":
serve(app, host="0.0.0.0", port=8080)
else:
app.run(host='0.0.0.0', port=8080)
Flask==2.0.2
waitress==2.0.0
Flask-MySQL==1.5.2
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment