mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-01-01 23:35:29 +00:00
[AB-xxx] adding rest-api base to abstracto with capability to load custom code if necessary
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
<module>scheduling</module>
|
||||
<module>documentation</module>
|
||||
<module>bundle</module>
|
||||
<module>rest-api</module>
|
||||
</modules>
|
||||
|
||||
<distributionManagement>
|
||||
|
||||
38
abstracto-application/rest-api/pom.xml
Normal file
38
abstracto-application/rest-api/pom.xml
Normal file
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>dev.sheldan.abstracto</groupId>
|
||||
<artifactId>abstracto-application</artifactId>
|
||||
<version>1.5.15-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>dev.sheldan.sissi.api</groupId>
|
||||
<artifactId>rest-api</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<finalName>rest-api-${project.version}</finalName>
|
||||
<appendAssemblyId>false</appendAssemblyId>
|
||||
<descriptors>
|
||||
<descriptor>src/main/assembly/assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
|
||||
<id>zip</id>
|
||||
<includeBaseDirectory>false</includeBaseDirectory>
|
||||
<formats>
|
||||
<format>zip</format>
|
||||
</formats>
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<outputDirectory>resources</outputDirectory>
|
||||
<directory>${project.basedir}/src/main/resources</directory>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<outputDirectory>.</outputDirectory>
|
||||
<directory>${project.basedir}/src/main/python</directory>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
</assembly>
|
||||
@@ -0,0 +1,18 @@
|
||||
from __main__ import app
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from flask import request
|
||||
|
||||
from utils import flask_utils
|
||||
|
||||
@app.route('/memes/doge/orangeSun/')
|
||||
def image_gen():
|
||||
text = request.args.get('text')
|
||||
with Image.open('resources/img/semf_template.jpg') as im:
|
||||
d1 = ImageDraw.Draw(im)
|
||||
text_box_size = (300, 240)
|
||||
W, H = text_box_size
|
||||
font = ImageFont.truetype(f'Impact.ttf', 60)
|
||||
_, _, w, h = d1.textbbox((0, 0), text, font=font)
|
||||
d1.text(((W-w)/2 + 320, (H-h)/2 + 120), text, font=font, fill=(255, 255, 255))
|
||||
return flask_utils.serve_pil_image(im)
|
||||
61
abstracto-application/rest-api/src/main/python/main.py
Normal file
61
abstracto-application/rest-api/src/main/python/main.py
Normal file
@@ -0,0 +1,61 @@
|
||||
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("..")
|
||||
# loads the api end points
|
||||
from base import image_gen
|
||||
|
||||
# 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("custom", 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)
|
||||
@@ -0,0 +1,17 @@
|
||||
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')
|
||||
|
||||
|
||||
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}')
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user