diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..55885f0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.20) +project(am_i_up) + +find_package(ament_cmake REQUIRED) +find_package(ament_cmake_python REQUIRED) + +ament_python_install_package(${PROJECT_NAME}) + +install(DIRECTORY + front_end + DESTINATION share/${PROJECT_NAME}/ +) + +install(DIRECTORY + scripts/ + DESTINATION lib/${PROJECT_NAME}/lib + USE_SOURCE_PERMISSIONS +) + +ament_package() diff --git a/am_i_up/server.py b/am_i_up/server.py index 78f39f9..438a39b 100644 --- a/am_i_up/server.py +++ b/am_i_up/server.py @@ -15,10 +15,13 @@ import yaml import ipaddress import subprocess import os +from jinja2 import Environment, FileSystemLoader, select_autoescape + def main(): facts = Facts() - routes = Routes(facts) + view = View() + routes = Routes(facts, view) app = web.Application() app.add_routes([ @@ -30,6 +33,24 @@ def main(): ]) web.run_app(app) +class View: + def __init__(self): + # TODO: Make global for the script directory. + self._env = Environment( + loader=FileSystemLoader(self._find_template_dirs()), + autoescape=select_autoescape() + ) + + def render_root(self): + return self._env.get_template('base.html').render() + + def _find_template_dirs(self): + package_dir = get_package_share_directory('am_i_up') + template_dir = "{}/front_end/templates".format(package_dir) + if not os.path.exists(template_dir): + raise RuntimeError("Could not find template_dir: {}".format(template_dir)) + return [template_dir] + class Facts: def __init__(self): self._start_time = time.monotonic() @@ -84,12 +105,13 @@ class Facts: return project_state_content class Routes: - def __init__(self, facts): + def __init__(self, facts, view): self._facts = facts + self._view = view async def root(self, request): - text = 'hello!' - return web.Response(text=text) + text = self._view.render_root() + return web.Response(text=text, content_type='text/html') async def ping(self, request): request_dict = await request.json() diff --git a/front_end/templates/base.html b/front_end/templates/base.html new file mode 100644 index 0000000..373f465 --- /dev/null +++ b/front_end/templates/base.html @@ -0,0 +1,32 @@ + + +
+ + + + + + + +