mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-18 19:57:38 +00:00
[AB-70] moving image generation functionality to separate image generation module
removing doge image generation from default base split rest-api into separate modules (base and extensions)
This commit is contained in:
9
python/components/rest-api-base/docker/Dockerfile
Normal file
9
python/components/rest-api-base/docker/Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
||||
FROM python:3.10.13-alpine3.18
|
||||
RUN apk --no-cache add msttcorefonts-installer fontconfig && \
|
||||
update-ms-fonts && \
|
||||
fc-cache -f
|
||||
ADD wrapper /
|
||||
ADD python/requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
ADD python /python
|
||||
CMD ["/run.sh"]
|
||||
0
python/components/rest-api-base/python/__init__.py
Normal file
0
python/components/rest-api-base/python/__init__.py
Normal file
59
python/components/rest-api-base/python/main.py
Normal file
59
python/components/rest-api-base/python/main.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
import importlib
|
||||
|
||||
FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
logging.basicConfig(encoding='utf-8', level=logging.INFO, format=FORMAT)
|
||||
template_dir = os.path.abspath('resources/templates')
|
||||
app = Flask(__name__, template_folder=template_dir)
|
||||
|
||||
import sys
|
||||
sys.path.append("..")
|
||||
|
||||
# This code was only done, because "from custom import *" did not work, it seems it did not execute the code in it
|
||||
# while the code was valid
|
||||
# https://stackoverflow.com/questions/57878744/how-do-i-dynamically-import-all-py-files-from-a-given-directory-and-all-sub-di
|
||||
def get_py_files(src):
|
||||
cwd = os.getcwd()
|
||||
py_files = []
|
||||
for root, dirs, files in os.walk(src):
|
||||
for file in files:
|
||||
if file.endswith(".py"):
|
||||
py_files.append(os.path.join(cwd, root, file))
|
||||
return py_files
|
||||
|
||||
|
||||
def dynamic_import(module_name, py_path):
|
||||
module_spec = importlib.util.spec_from_file_location(module_name, py_path)
|
||||
module = importlib.util.module_from_spec(module_spec)
|
||||
module_spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def dynamic_import_from_src(src, star_import=False):
|
||||
my_py_files = get_py_files(src)
|
||||
for py_file in my_py_files:
|
||||
module_name = os.path.split(py_file)[-1].strip(".py")
|
||||
imported_module = dynamic_import(module_name, py_file)
|
||||
if star_import:
|
||||
for obj in dir(imported_module):
|
||||
globals()[obj] = imported_module.__dict__[obj]
|
||||
else:
|
||||
globals()[module_name] = imported_module
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
dynamic_import_from_src("endpoints", star_import=False)
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
return 'Hello, World?'
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from waitress import serve
|
||||
|
||||
serve(app, host="0.0.0.0", port=8080)
|
||||
18
python/components/rest-api-base/python/requirements.txt
Normal file
18
python/components/rest-api-base/python/requirements.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
blinker==1.7.0
|
||||
certifi==2023.7.22
|
||||
charset-normalizer==3.3.2
|
||||
click==8.1.7
|
||||
Flask==3.0.0
|
||||
idna==3.4
|
||||
importlib-metadata==6.8.0
|
||||
itsdangerous==2.1.2
|
||||
Jinja2==3.1.2
|
||||
MarkupSafe==2.1.3
|
||||
Pillow==10.1.0
|
||||
requests==2.31.0
|
||||
urllib3==2.0.7
|
||||
waitress==2.1.2
|
||||
Werkzeug==3.0.1
|
||||
zipp==3.17.0
|
||||
pytz==2023.3.post1
|
||||
validators==0.22.0
|
||||
24
python/components/rest-api-base/python/utils/flask_utils.py
Normal file
24
python/components/rest-api-base/python/utils/flask_utils.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from io import BytesIO
|
||||
|
||||
from flask import send_file
|
||||
|
||||
|
||||
def serve_pil_image(pil_img):
|
||||
img_io = BytesIO()
|
||||
pil_img.save(img_io, 'PNG')
|
||||
img_io.seek(0)
|
||||
return send_file(img_io, mimetype='image/png')
|
||||
|
||||
|
||||
def serve_pil_gif_image(frames, duration=25):
|
||||
animated_gif = BytesIO()
|
||||
frames[0].save(animated_gif, format='GIF', save_all=True, append_images=frames[1:], duration=duration, loop=0, disposal=2)
|
||||
animated_gif.seek(0)
|
||||
return send_file(animated_gif, mimetype='image/gif')
|
||||
|
||||
|
||||
class ValidationException(Exception):
|
||||
def __init__(self, provided_value, message):
|
||||
self.provided_value = provided_value
|
||||
self.message = message
|
||||
super().__init__(f'{self.message}: provided value: {provided_value}')
|
||||
5
python/components/rest-api-base/wrapper/run.sh
Executable file
5
python/components/rest-api-base/wrapper/run.sh
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
echo "Starting python server..."
|
||||
cd python
|
||||
python3 -u main.py
|
||||
Reference in New Issue
Block a user