77 lines
2.0 KiB
Python
77 lines
2.0 KiB
Python
import time
|
|
import pymysql
|
|
from flask import Flask, jsonify
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
import redis
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://myuser:mypassword@mysql:3306/mydb'
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
MYSQL_HOST = "mysql"
|
|
MYSQL_PORT = 3306
|
|
MYSQL_USER = "myuser"
|
|
MYSQL_PASSWORD = "mypassword"
|
|
MYSQL_DB = "mydb"
|
|
|
|
# Connexion Redis
|
|
redis_client = redis.Redis(host='redis', port=6379, decode_responses=True)
|
|
|
|
while True:
|
|
try:
|
|
conn = pymysql.connect(
|
|
host=MYSQL_HOST,
|
|
port=MYSQL_PORT,
|
|
user=MYSQL_USER,
|
|
password=MYSQL_PASSWORD,
|
|
database=MYSQL_DB
|
|
)
|
|
conn.close()
|
|
print("MySQL is up - continuing.")
|
|
break
|
|
except pymysql.err.OperationalError as e:
|
|
print("Waiting for MySQL...", e)
|
|
time.sleep(2)
|
|
|
|
class Artiste(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
nom = db.Column(db.String(100), nullable=False)
|
|
|
|
class Galerie(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
nom = db.Column(db.String(100), nullable=False)
|
|
|
|
class Oeuvre(db.Model):
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
titre = db.Column(db.String(200), nullable=False)
|
|
exposee = db.Column(db.Boolean, default=False)
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def index():
|
|
return "Public API", 200
|
|
|
|
@app.route("/artistes", methods=["GET"])
|
|
def get_artistes():
|
|
artistes = Artiste.query.all()
|
|
return jsonify([{"id": a.id, "nom": a.nom} for a in artistes]), 200
|
|
|
|
@app.route("/galeries", methods=["GET"])
|
|
def get_galeries():
|
|
galeries = Galerie.query.all()
|
|
return jsonify([{"id": g.id, "nom": g.nom} for g in galeries]), 200
|
|
|
|
@app.route("/oeuvres", methods=["GET"])
|
|
def get_oeuvres():
|
|
oeuvres = Oeuvre.query.filter_by(exposee=True).all()
|
|
return jsonify([{"id": o.id, "titre": o.titre} for o in oeuvres]), 200
|
|
|
|
|
|
if __name__ == "__main__":
|
|
with app.app_context():
|
|
db.create_all()
|
|
app.run(host='0.0.0.0',port=5001, debug=True)
|
|
|