Skip to content

Commit

Permalink
feat(config, main, utils): add SERVER mode, unify backend and fronten…
Browse files Browse the repository at this point in the history
…d config
  • Loading branch information
Iamhexi committed Nov 1, 2024
1 parent 0df2bae commit a0f8e2c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
6 changes: 3 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ production_mode: false
learning_materials: ./learning_assets
experiment_implementation: ./tests/model
experiment_results: ./tests/model/results
backend_address: 127.0.0.1 # Change also in frontend/src/lib/config.js
backend_port: 8000 # Change also in frontend/src/lib/config.js
protocol: http # Either `http` or `https`
backend_address: 127.0.0.1
backend_port: 8000
frontend_address: 127.0.0.1
frontend_port: 3000
protocol: http # Either `http` or `https`
question_generation_model: t5
natural_language_inference_model: roberta
49 changes: 31 additions & 18 deletions knowledge_verificator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@
from knowledge_verificator.command_line import run_cli_mode
from tests.model.runner import ExperimentRunner


def run_backend() -> None:
"""Run the HTTP backend of the system."""
import uvicorn

uvicorn.run(
'knowledge_verificator.backend:ENDPOINTS',
host=config().backend_address,
port=config().backend_port,
reload=(not config().production_mode),
)


def run_frontend() -> subprocess.Popen:
"""Run the built-in frontend of the system."""
arguments = [
'npm',
'run',
'dev',
'--',
'--port',
str(config().frontend_port),
]
return subprocess.Popen(args=arguments, cwd='frontend')

Check warning on line 35 in knowledge_verificator/main.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

knowledge_verificator/main.py#L35

subprocess call - check for execution of untrusted input.


if __name__ == '__main__':
match config().mode:
case OperatingMode.EXPERIMENT:
Expand All @@ -20,22 +46,9 @@
case OperatingMode.CLI:
run_cli_mode()

case OperatingMode.SERVER:
run_backend()

case OperatingMode.CLIENT_SERVER:
import uvicorn

arguments = [
'npm',
'run',
'dev',
'--',
'--port',
str(config().frontend_port),
]
frontend = subprocess.Popen(args=arguments, cwd='frontend')

uvicorn.run(
'knowledge_verificator.backend:ENDPOINTS',
host=config().backend_address,
port=config().backend_port,
reload=(not config().production_mode),
)
run_frontend()
run_backend()
35 changes: 34 additions & 1 deletion knowledge_verificator/utils/configuration_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ class OperatingMode(Enum):
- CLIENT_SERVER - an HTTP server is started, which serves as the
backend. Graphical user interface (GUI) in a form of webpage
is served as the frontend.
- SERVER - an HTTP server is started as an API. The built-in
frontend is not used. This is useful if one wants to build
a custom frontend for the existing backend.
"""

EXPERIMENT = 'EXPERIMENT'
CLI = 'CLI'
CLIENT_SERVER = 'CLIENT_SERVER'
SERVER = 'SERVER'


@dataclass
Expand Down Expand Up @@ -118,11 +122,40 @@ class ConfigurationParser:
"""Class, which loads and parses a YAML configuration."""

def __init__(self, configuration_file: Path) -> None:
"""Load a YAML configuration file."""
"""
Load a YAML configuration file and, optionally, re-create a JS file
with the most current configuration.
"""
self._config_file: Path | None = None
self._config_data: dict = {}
self.load_configuration(configuration_file)

# Update configuration for the built-in frontend only.
if self._config_data['mode'] == OperatingMode.CLIENT_SERVER:
self.update_javascript_configuration(
Path('frontend/src/lib/config.js')
)

def update_javascript_configuration(self, js_config: Path) -> None:
"""
Re-create a JavaScript configuration file to be compliant
with a YAML configuration.
Create a JavaScript configuration file with a constant API_URL, which
supplies an URL to backend for the frontend.
Args:
js_config (Path): Path to a JavaScript configuration file.
"""

backend_address = self._config_data['backend_address']
backend_port = self._config_data['backend_port']
protocol = self._config_data['protocol']
js_config_content = f"export const API_URL = '{protocol}://{backend_address}:{backend_port}'; // Do not edit manually."

with open(js_config.resolve(), 'wt', encoding='utf-8') as fd:
fd.write(js_config_content)

def parse_configuration(self) -> Configuration:
"""
Parse the previously loaded YAML configuration file to an instance
Expand Down

0 comments on commit a0f8e2c

Please sign in to comment.