mutliple changes left from the last week.
This commit is contained in:
parent
415b2007ce
commit
f0574de8f4
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
.padded {
|
||||
padding-left: 10px;
|
||||
padding-right: 10px;
|
||||
padding-top: 10px;
|
||||
padding-bottom: 10px;
|
||||
}
|
||||
|
|
@ -8,18 +8,25 @@
|
|||
<!-- Bootstrap CSS -->
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
|
||||
|
||||
<link href="/static/css/base.css" rel="stylesheet">
|
||||
|
||||
<title>{% block title %}Robot Status UI{% endblock %}</title>
|
||||
</head>
|
||||
<body>
|
||||
<!-- NavBar -->
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light padded">
|
||||
<a class="navbar-brand" href="/">am i up?</a>
|
||||
<div class="navbar-nav">
|
||||
<a class="nav-item nav-link" href="/">Home</a>
|
||||
<a class="nav-item nav-link" href="/ping">Ping</a>
|
||||
<a class="nav-item nav-link" href="/version">Software Version</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<!-- Content -->
|
||||
<div class="container">
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div>Hello World!</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<p>Home</p>
|
||||
{% endblock %}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<p>Ping</p>
|
||||
{% endblock %}
|
||||
Loading…
Reference in New Issue