Commit b0e07009 authored by Michele Del Giudice's avatar Michele Del Giudice
Browse files

Fixed simple dockerfile

parent 1a6781bc
FROM python:3.11-slim
WORKDIR /app
# Install build deps needed for some packages (cryptography, etc.) then remove them
RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential libssl-dev libffi-dev curl ca-certificates && \
rm -rf /var/lib/apt/lists/*
COPY requirements.txt ./
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Copy application
COPY . /app
#ENV FLASK_APP=app:create_app
EXPOSE 5000
# Healthcheck: call the Flask /health endpoint
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD curl -fS http://127.0.0.1:5000/health || exit 1
CMD ["flask", "--app" , "app", "run", "--host=0.0.0.0"]
...@@ -30,10 +30,19 @@ pytest -q ...@@ -30,10 +30,19 @@ pytest -q
Run the app: Run the app:
```bash ```bash
export MYSQL_USER=root flask --app app run
export MYSQL_PASSWORD=yourpass ```
export MYSQL_DB=car_rental
python app.py Run the app (on all interfaces):
```bash
flask --app app run --host=0.0.0.0
```
Run the app (on different port):
```bash
flask --app app run --port=12345
``` ```
Database migrations (Flask-Migrate / Alembic) Database migrations (Flask-Migrate / Alembic)
......
...@@ -7,37 +7,36 @@ from models import db, Car, Reservation, CarType ...@@ -7,37 +7,36 @@ from models import db, Car, Reservation, CarType
from flask_migrate import Migrate from flask_migrate import Migrate
def create_app(): app = Flask(__name__)
app = Flask(__name__) app.config.from_object(Config)
app.config.from_object(Config) db.init_app(app)
db.init_app(app) # Migrations
# Migrations Migrate(app, db)
Migrate(app, db)
@app.errorhandler(404)
@app.errorhandler(404) def not_found(e):
def not_found(e):
return jsonify({'error': 'Not found'}), 404 return jsonify({'error': 'Not found'}), 404
@app.errorhandler(400) @app.errorhandler(400)
def bad_request(e): def bad_request(e):
return jsonify({'error': 'Bad request', 'message': str(e)}), 400 return jsonify({'error': 'Bad request', 'message': str(e)}), 400
@app.route('/health', methods=['GET']) @app.route('/health', methods=['GET'])
def health(): def health():
return jsonify({'status': 'ok'}) return jsonify({'status': 'ok'})
@app.route('/cars', methods=['GET']) @app.route('/cars', methods=['GET'])
def list_cars(): def list_cars():
cars = Car.query.all() cars = Car.query.all()
return jsonify([c.to_dict() for c in cars]) return jsonify([c.to_dict() for c in cars])
@app.route('/cars/<int:car_id>', methods=['GET']) @app.route('/cars/<int:car_id>', methods=['GET'])
def get_car(car_id): def get_car(car_id):
car = Car.query.get_or_404(car_id) car = Car.query.get_or_404(car_id)
return jsonify(car.to_dict()) return jsonify(car.to_dict())
@app.route('/cars', methods=['POST']) @app.route('/cars', methods=['POST'])
def create_car(): def create_car():
data = request.get_json() or {} data = request.get_json() or {}
required = ['name', 'seats', 'ports', 'type', 'brand', 'license_plate'] required = ['name', 'seats', 'ports', 'type', 'brand', 'license_plate']
for f in required: for f in required:
...@@ -72,8 +71,8 @@ def create_app(): ...@@ -72,8 +71,8 @@ def create_app():
return jsonify(car.to_dict()), 201 return jsonify(car.to_dict()), 201
@app.route('/cars/<int:car_id>', methods=['PUT']) @app.route('/cars/<int:car_id>', methods=['PUT'])
def update_car(car_id): def update_car(car_id):
car = Car.query.get_or_404(car_id) car = Car.query.get_or_404(car_id)
data = request.get_json() or {} data = request.get_json() or {}
if 'name' in data: if 'name' in data:
...@@ -102,26 +101,26 @@ def create_app(): ...@@ -102,26 +101,26 @@ def create_app():
return jsonify(car.to_dict()) return jsonify(car.to_dict())
@app.route('/cars/<int:car_id>', methods=['DELETE']) @app.route('/cars/<int:car_id>', methods=['DELETE'])
def delete_car(car_id): def delete_car(car_id):
car = Car.query.get_or_404(car_id) car = Car.query.get_or_404(car_id)
db.session.delete(car) db.session.delete(car)
db.session.commit() db.session.commit()
return jsonify({'deleted': car_id}) return jsonify({'deleted': car_id})
# Reservations # Reservations
@app.route('/reservations', methods=['GET']) @app.route('/reservations', methods=['GET'])
def list_reservations(): def list_reservations():
res = Reservation.query.all() res = Reservation.query.all()
return jsonify([r.to_dict() for r in res]) return jsonify([r.to_dict() for r in res])
@app.route('/reservations/<int:res_id>', methods=['GET']) @app.route('/reservations/<int:res_id>', methods=['GET'])
def get_reservation(res_id): def get_reservation(res_id):
r = Reservation.query.get_or_404(res_id) r = Reservation.query.get_or_404(res_id)
return jsonify(r.to_dict()) return jsonify(r.to_dict())
@app.route('/reservations', methods=['POST']) @app.route('/reservations', methods=['POST'])
def create_reservation(): def create_reservation():
data = request.get_json() or {} data = request.get_json() or {}
required = ['pickup_date', 'return_date', 'car_id', 'customer_fullname', 'drive_license'] required = ['pickup_date', 'return_date', 'car_id', 'customer_fullname', 'drive_license']
for f in required: for f in required:
...@@ -157,8 +156,8 @@ def create_app(): ...@@ -157,8 +156,8 @@ def create_app():
return jsonify(reservation.to_dict()), 201 return jsonify(reservation.to_dict()), 201
@app.route('/reservations/<int:res_id>', methods=['PUT']) @app.route('/reservations/<int:res_id>', methods=['PUT'])
def update_reservation(res_id): def update_reservation(res_id):
r = Reservation.query.get_or_404(res_id) r = Reservation.query.get_or_404(res_id)
data = request.get_json() or {} data = request.get_json() or {}
if 'pickup_date' in data: if 'pickup_date' in data:
...@@ -192,16 +191,9 @@ def create_app(): ...@@ -192,16 +191,9 @@ def create_app():
return jsonify(r.to_dict()) return jsonify(r.to_dict())
@app.route('/reservations/<int:res_id>', methods=['DELETE']) @app.route('/reservations/<int:res_id>', methods=['DELETE'])
def delete_reservation(res_id): def delete_reservation(res_id):
r = Reservation.query.get_or_404(res_id) r = Reservation.query.get_or_404(res_id)
db.session.delete(r) db.session.delete(r)
db.session.commit() db.session.commit()
return jsonify({'deleted': res_id}) return jsonify({'deleted': res_id})
return app
if __name__ == '__main__':
app = create_app()
app.run(host=os.getenv('FLASK_RUN_HOST', '0.0.0.0'), port=int(os.getenv('FLASK_RUN_PORT', 5000)), debug=True)
...@@ -5,4 +5,3 @@ cryptography==46.0.3 ...@@ -5,4 +5,3 @@ cryptography==46.0.3
Flask-Migrate>=4.0 Flask-Migrate>=4.0
pytest>=7.0 pytest>=7.0
pytest-flask>=1.2 pytest-flask>=1.2
gunicorn>=20.0
\ 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