From f0574de8f45abc57d1eb190c7c5c762c01164a38 Mon Sep 17 00:00:00 2001 From: James Pace Date: Thu, 1 Jan 2026 10:29:05 -0500 Subject: [PATCH] mutliple changes left from the last week. --- am_i_up/client.py | 8 +++--- am_i_up/server.py | 46 ++++++++++++++++++++--------------- front_end/static/css/base.css | 6 +++++ front_end/templates/base.html | 13 +++++++--- front_end/templates/home.html | 5 ++++ front_end/templates/ping.html | 5 ++++ 6 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 front_end/static/css/base.css create mode 100644 front_end/templates/home.html create mode 100644 front_end/templates/ping.html diff --git a/am_i_up/client.py b/am_i_up/client.py index 8a1179d..5fda2d4 100644 --- a/am_i_up/client.py +++ b/am_i_up/client.py @@ -42,26 +42,26 @@ async def call_ping(options): print("Calling ping with request: {}", json.dumps(request)) async with aiohttp.ClientSession() as session: - async with session.get('http://localhost:8080/ping', json=request) as resp: + async with session.get('http://localhost:8080/api/ping', json=request) as resp: print(await resp.text()) async def call_uptime(): print("Calling uptime.") async with aiohttp.ClientSession() as session: - async with session.get('http://localhost:8080/uptime') as resp: + async with session.get('http://localhost:8080/api/uptime') as resp: print(await resp.text()) async def call_build_info(): print("Calling build_info.") async with aiohttp.ClientSession() as session: - async with session.get('http://localhost:8080/build_info') as resp: + async with session.get('http://localhost:8080/api/build_info') as resp: print(await resp.text()) async def call_env(): print("Calling env.") async with aiohttp.ClientSession() as session: - async with session.get('http://localhost:8080/env') as resp: + async with session.get('http://localhost:8080/api/env') as resp: print(await resp.text()) diff --git a/am_i_up/server.py b/am_i_up/server.py index 438a39b..f21822e 100644 --- a/am_i_up/server.py +++ b/am_i_up/server.py @@ -20,29 +20,36 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape def main(): facts = Facts() - view = View() - routes = Routes(facts, view) + view = View(facts) + api = Api(facts) app = web.Application() app.add_routes([ - web.get('/', routes.root), - web.get('/ping', routes.ping), - web.get('/uptime', routes.uptime), - web.get('/build_info', routes.build_info), - web.get('/env', routes.env) + web.get('/', view.root), + web.get('/ping', view.ping), + web.get('/api/ping', api.ping), + web.get('/api/uptime', api.uptime), + web.get('/api/build_info', api.build_info), + web.get('/api/env', api.env), + web.static('/static', view.get_static_dir()) ]) web.run_app(app) class View: - def __init__(self): - # TODO: Make global for the script directory. + def __init__(self, facts): + self._facts = facts self._env = Environment( loader=FileSystemLoader(self._find_template_dirs()), autoescape=select_autoescape() ) - def render_root(self): - return self._env.get_template('base.html').render() + async def root(self, request): + text = self._env.get_template('home.html').render() + return web.Response(text=text, content_type='text/html') + + async def ping(self, request): + text = self._env.get_template('ping.html').render() + return web.Response(text=text, content_type='text/html') def _find_template_dirs(self): package_dir = get_package_share_directory('am_i_up') @@ -51,6 +58,13 @@ class View: raise RuntimeError("Could not find template_dir: {}".format(template_dir)) return [template_dir] + def get_static_dir(self): + package_dir = get_package_share_directory('am_i_up') + static_dir = "{}/front_end/static".format(package_dir) + if not os.path.exists(static_dir): + raise RuntimeError("Could not find static_dir: {}".format(static_dir)) + return static_dir + class Facts: def __init__(self): self._start_time = time.monotonic() @@ -104,14 +118,9 @@ class Facts: print("Can't find build info.\n{}".format(e)) return project_state_content -class Routes: - def __init__(self, facts, view): +class Api: + def __init__(self, facts): self._facts = facts - self._view = view - - async def root(self, request): - text = self._view.render_root() - return web.Response(text=text, content_type='text/html') async def ping(self, request): request_dict = await request.json() @@ -135,7 +144,6 @@ class Routes: env = self._facts.get_env() return web.json_response(env) - def is_valid_ip(address): try: ipaddress.ip_address(address) diff --git a/front_end/static/css/base.css b/front_end/static/css/base.css new file mode 100644 index 0000000..ffc40df --- /dev/null +++ b/front_end/static/css/base.css @@ -0,0 +1,6 @@ +.padded { + padding-left: 10px; + padding-right: 10px; + padding-top: 10px; + padding-bottom: 10px; +} diff --git a/front_end/templates/base.html b/front_end/templates/base.html index 373f465..c882a6a 100644 --- a/front_end/templates/base.html +++ b/front_end/templates/base.html @@ -8,18 +8,25 @@ + + {% block title %}Robot Status UI{% endblock %} +
{% block content %} - -
Hello World!
- {% endblock %}
diff --git a/front_end/templates/home.html b/front_end/templates/home.html new file mode 100644 index 0000000..52cdaba --- /dev/null +++ b/front_end/templates/home.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block content %} +

Home

+{% endblock %} diff --git a/front_end/templates/ping.html b/front_end/templates/ping.html new file mode 100644 index 0000000..2e9abe1 --- /dev/null +++ b/front_end/templates/ping.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block content %} +

Ping

+{% endblock %}