NXPR-1 Database storage skeleton
This commit is contained in:
parent
77d7c52f27
commit
b02ecf706f
@ -1,24 +1,49 @@
|
||||
from http.client import NON_AUTHORITATIVE_INFORMATION
|
||||
from flask import Flask
|
||||
from utils.config import ServerConfig
|
||||
import sqlite3
|
||||
from utils.database.database import Database
|
||||
from utils.exceptions import DatabaseException
|
||||
|
||||
|
||||
class FlaskAppWrapper(object):
|
||||
|
||||
def __init__(self, app, **configs):
|
||||
self.app = app
|
||||
self.configs(**configs)
|
||||
|
||||
def configs(self, **configs):
|
||||
for config, value in configs:
|
||||
self.app.config[config.upper()] = value
|
||||
|
||||
def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None, methods=['GET'], *args, **kwargs):
|
||||
self.app.add_url_rule(endpoint, endpoint_name,
|
||||
handler, methods=methods, *args, **kwargs)
|
||||
|
||||
def run(self, **kwargs):
|
||||
self.app.run(**kwargs)
|
||||
|
||||
|
||||
class Server():
|
||||
|
||||
def __init__(self, host, port, name, access_password, version, database_file):
|
||||
self.app = Flask(self.name)
|
||||
def __init__(self, host: str, port: int, name: str, access_password: str, version: str, database_file_path: str, logging_level: str):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.name = name
|
||||
self.access_password = access_password
|
||||
self.version = version
|
||||
self.client_database = sqlite3.connect(database_file)
|
||||
self.database = Database(
|
||||
database_file=database_file_path, logging_level=logging_level)
|
||||
self.flask_app = Flask(name)
|
||||
self.app = FlaskAppWrapper(self.flask_app)
|
||||
|
||||
@app.route("/")
|
||||
def basic_server_data(self):
|
||||
return {"server_name": self.name, "server_version": self.version, "host": self.host}
|
||||
|
||||
@app.route("/client/register")
|
||||
def register_new_client_to_database(self):
|
||||
# TODO: implement
|
||||
pass
|
||||
self.database
|
||||
|
||||
def run(self):
|
||||
self.app.add_endpoint(endpoint="/", endpoint_name="server_data",
|
||||
handler=self.basic_server_data, methods=["GET"])
|
||||
# TODO: add rest of endpoints
|
||||
self.app.run()
|
||||
|
13
requirements.txt
Normal file
13
requirements.txt
Normal file
@ -0,0 +1,13 @@
|
||||
autopep8==2.0.0
|
||||
click==8.1.3
|
||||
Flask==2.2.2
|
||||
greenlet==2.0.1
|
||||
itsdangerous==2.1.2
|
||||
Jinja2==3.1.2
|
||||
MarkupSafe==2.1.1
|
||||
pyaml==21.10.1
|
||||
pycodestyle==2.9.1
|
||||
PyYAML==6.0
|
||||
SQLAlchemy==1.4.43
|
||||
tomli==2.0.1
|
||||
Werkzeug==2.2.2
|
@ -130,3 +130,36 @@ class Database:
|
||||
except Exception as ex:
|
||||
self.logger.error(
|
||||
f"Error getting list of images from database: {ex}")
|
||||
|
||||
def get_image_by_hash(self, image_hash: str) -> list[VMImage]:
|
||||
try:
|
||||
with self.session.begin():
|
||||
response = self.session.query(VMImage, image_hash=image_hash)
|
||||
return response
|
||||
except Exception as ex:
|
||||
self.logger.error(
|
||||
f"Error getting list of images with specified hash: {ex}")
|
||||
|
||||
def add_image(self, image: VMImage):
|
||||
try:
|
||||
with self.session.begin():
|
||||
self.session.add(image)
|
||||
self.session.flush()
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
self.logger.error(f"Couldn't save client data do database: {ex}")
|
||||
raise DatabaseException(f"Couldn't add image to database: {ex}")
|
||||
|
||||
def modify_image(self, new_image_object: VMImage) -> VMImage:
|
||||
try:
|
||||
old_object = self.get_image_by_id(new_image_object.image_id)
|
||||
with self.session.begin():
|
||||
old_object = new_image_object
|
||||
self.session.merge(old_object)
|
||||
self.session.flush()
|
||||
self.session.commit()
|
||||
return old_object
|
||||
except Exception as ex:
|
||||
self.logger.error(f"Couldn't modify object in database: {ex}")
|
||||
raise DatabaseException(
|
||||
f"Couldn't modify object in database: {ex}")
|
||||
|
Loading…
Reference in New Issue
Block a user