diff --git a/.gitignore b/.gitignore index c969d7c..f152031 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ env .env movielist.sqlite posters/* +db/* __pycache__ *~ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a344fbf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# Use an official Python runtime as the base image +FROM python:3.9-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the project files into the container +COPY . . +RUN chmod +x docker-entrypoint.sh + +# Create a volume for the posters +VOLUME /app/posters +VOLUME /app/db + +# Install the project dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Set environment variables +ENV FLASK_APP=kinolist +ENV FLASK_RUN_HOST=0.0.0.0 +ENV FLASK_DATABASE=/app/db/movielist.sqlite +ENV FLASK_IMAGE_WIDTH=200 + +# Initialize the database +RUN flask --app kinolist init-db + +# Expose the port the app runs on +EXPOSE 8000 + +# Run the application +CMD ["/app/docker-entrypoint.sh"] diff --git a/README.md b/README.md index 4aa31d7..1316d02 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,3 @@ If you are not in the virtual environment (the `(env)` keyword does not appear i python -m flask --app kinolist run --port 8000 --debug ``` You can access the site at the adress `localhost:8000` on your web browser. Do `Ctrl+C` to stop the server. - -# Remarks -For some reasons, I need a symbolic link from `posters/` to `kinolist/posters/`, I guess it is because python keeps changing the base directory, so the fix I have found is to have two `posters` folders which are actually the same. diff --git a/configure.sh b/configure.sh deleted file mode 100644 index aecfa43..0000000 --- a/configure.sh +++ /dev/null @@ -1,10 +0,0 @@ -if [ ! -d env ]; then - python -m venv env - source env/bin/activate - python -m pip install -r requirements.txt -fi - -if [ ! -f movielist.sqlite ]; then - source env/bin/activate - python -m flask --app kinolist init-db -fi diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..453ad97 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.8' + +services: + web: + build: . + ports: + - "8000:8000" + volumes: + - ./posters:/app/posters + - ./db/:/app/db + environment: + - FLASK_SECRET_KEY=${FLASK_SECRET_KEY} + - FLASK_OMDB_KEY=${FLASK_OMDB_KEY} + - FLASK_ADMIN_PASSWORD=${FLASK_ADMIN_PASSWORD} + - FLASK_DATABASE=/app/db/movielist.sqlite + - FLASK_IMAGE_WIDTH=200 + env_file: + - .env + +volumes: + posters: + db: \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000..1063714 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Check if the database file exists +if [ ! -f /app/db/movielist.sqlite ]; then + echo "Initializing database..." + python -m flask --app kinolist init-db +fi + +# Start the Flask application +exec gunicorn --bind 0.0.0.0:8000 "kinolist:create_app()" \ No newline at end of file diff --git a/kinolist/__init__.py b/kinolist/__init__.py index 24279c6..6bff2fd 100644 --- a/kinolist/__init__.py +++ b/kinolist/__init__.py @@ -13,4 +13,10 @@ def create_app(): database.init_app(app) app.register_blueprint(pages.bp) + + base_dir = os.path.abspath(os.path.dirname(__file__)) + + # Set the UPLOAD_FOLDER to the parent directory of your project + app.config['UPLOAD_FOLDER'] = os.path.join(base_dir, '..', 'posters') + return app diff --git a/kinolist/pages.py b/kinolist/pages.py index 13b2e2a..a369450 100644 --- a/kinolist/pages.py +++ b/kinolist/pages.py @@ -76,7 +76,7 @@ def manage(): poster = request.files["poster"] filename = posters.save(poster.filename, poster.read()) update_poster(id, filename) - return filename; + return filename elif "plot" in request.form: update_plot(id, request.form["plot"]) elif "watched" in request.form: diff --git a/kinolist/posters b/kinolist/posters deleted file mode 120000 index bdc3dde..0000000 --- a/kinolist/posters +++ /dev/null @@ -1 +0,0 @@ -../posters \ No newline at end of file diff --git a/kinolist/posters.py b/kinolist/posters.py index 67c8a05..6dabef1 100644 --- a/kinolist/posters.py +++ b/kinolist/posters.py @@ -1,15 +1,17 @@ from flask import (current_app, send_from_directory) from hashlib import sha1 from pathlib import Path -from os import path +import os def save(name, content): h = sha1(content).hexdigest() filename = h + Path(name).suffix - file = open(path.join(current_app.config['UPLOAD_FOLDER'], filename), "wb") - file.write(content) - file.close() + file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], filename) + + with open(file_path, "wb") as file: + file.write(content) + return filename def get(name): - return send_from_directory(current_app.config["UPLOAD_FOLDER"], name) + return send_from_directory(current_app.config['UPLOAD_FOLDER'], name) diff --git a/requirements.txt b/requirements.txt index c61d1f2..1d820f2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ Flask Flask-Login python-dotenv requests - +gunicorn