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))
|
print("Calling ping with request: {}", json.dumps(request))
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
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())
|
print(await resp.text())
|
||||||
|
|
||||||
async def call_uptime():
|
async def call_uptime():
|
||||||
print("Calling uptime.")
|
print("Calling uptime.")
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
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())
|
print(await resp.text())
|
||||||
|
|
||||||
async def call_build_info():
|
async def call_build_info():
|
||||||
print("Calling build_info.")
|
print("Calling build_info.")
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
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())
|
print(await resp.text())
|
||||||
|
|
||||||
async def call_env():
|
async def call_env():
|
||||||
print("Calling env.")
|
print("Calling env.")
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
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())
|
print(await resp.text())
|
||||||
|
|
|
||||||
|
|
@ -20,29 +20,36 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
facts = Facts()
|
facts = Facts()
|
||||||
view = View()
|
view = View(facts)
|
||||||
routes = Routes(facts, view)
|
api = Api(facts)
|
||||||
|
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
app.add_routes([
|
app.add_routes([
|
||||||
web.get('/', routes.root),
|
web.get('/', view.root),
|
||||||
web.get('/ping', routes.ping),
|
web.get('/ping', view.ping),
|
||||||
web.get('/uptime', routes.uptime),
|
web.get('/api/ping', api.ping),
|
||||||
web.get('/build_info', routes.build_info),
|
web.get('/api/uptime', api.uptime),
|
||||||
web.get('/env', routes.env)
|
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)
|
web.run_app(app)
|
||||||
|
|
||||||
class View:
|
class View:
|
||||||
def __init__(self):
|
def __init__(self, facts):
|
||||||
# TODO: Make global for the script directory.
|
self._facts = facts
|
||||||
self._env = Environment(
|
self._env = Environment(
|
||||||
loader=FileSystemLoader(self._find_template_dirs()),
|
loader=FileSystemLoader(self._find_template_dirs()),
|
||||||
autoescape=select_autoescape()
|
autoescape=select_autoescape()
|
||||||
)
|
)
|
||||||
|
|
||||||
def render_root(self):
|
async def root(self, request):
|
||||||
return self._env.get_template('base.html').render()
|
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):
|
def _find_template_dirs(self):
|
||||||
package_dir = get_package_share_directory('am_i_up')
|
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))
|
raise RuntimeError("Could not find template_dir: {}".format(template_dir))
|
||||||
return [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:
|
class Facts:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._start_time = time.monotonic()
|
self._start_time = time.monotonic()
|
||||||
|
|
@ -104,14 +118,9 @@ class Facts:
|
||||||
print("Can't find build info.\n{}".format(e))
|
print("Can't find build info.\n{}".format(e))
|
||||||
return project_state_content
|
return project_state_content
|
||||||
|
|
||||||
class Routes:
|
class Api:
|
||||||
def __init__(self, facts, view):
|
def __init__(self, facts):
|
||||||
self._facts = 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):
|
async def ping(self, request):
|
||||||
request_dict = await request.json()
|
request_dict = await request.json()
|
||||||
|
|
@ -135,7 +144,6 @@ class Routes:
|
||||||
env = self._facts.get_env()
|
env = self._facts.get_env()
|
||||||
return web.json_response(env)
|
return web.json_response(env)
|
||||||
|
|
||||||
|
|
||||||
def is_valid_ip(address):
|
def is_valid_ip(address):
|
||||||
try:
|
try:
|
||||||
ipaddress.ip_address(address)
|
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 -->
|
<!-- 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="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>
|
<title>{% block title %}Robot Status UI{% endblock %}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- NavBar -->
|
<!-- 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 -->
|
<!-- Content -->
|
||||||
<div class="container">
|
<div class="container">
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<div>Hello World!</div>
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
</div>
|
</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