Merge branch 'main' of lain.church:bartholin/flask-server

This commit is contained in:
bartholin 2024-10-13 16:19:56 +02:00
commit 71c05404af
11 changed files with 79 additions and 21 deletions

1
.gitignore vendored
View File

@ -2,5 +2,6 @@ env
.env
movielist.sqlite
posters/*
db/*
__pycache__
*~

31
Dockerfile Normal file
View File

@ -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"]

View File

@ -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.

View File

@ -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

22
docker-compose.yml Normal file
View File

@ -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:

10
docker-entrypoint.sh Normal file
View File

@ -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()"

View File

@ -30,4 +30,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

View File

@ -93,7 +93,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:

View File

@ -1 +0,0 @@
../posters

View File

@ -18,15 +18,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)

View File

@ -3,4 +3,4 @@ Flask
Flask-Login
python-dotenv
requests
gunicorn