add error handling for service not found
parent
082ed27dfa
commit
2154f22f1f
38
server.py
38
server.py
|
|
@ -91,23 +91,27 @@ async def register_service(name: str, ip: str):
|
||||||
async def read_service(name: str):
|
async def read_service(name: str):
|
||||||
"""Reads the hosts file, produces a service catalog entry."""
|
"""Reads the hosts file, produces a service catalog entry."""
|
||||||
catalog = {}
|
catalog = {}
|
||||||
|
try:
|
||||||
with open(HOSTS_FILE, "r") as fd:
|
with open(HOSTS_FILE, "r") as fd:
|
||||||
hosts_file = fd.read().split("\n")
|
hosts_file = fd.read().split("\n")
|
||||||
entries = [
|
entries = [
|
||||||
entry for entry in hosts_file if not entry.startswith("#") and entry != "\n"
|
entry
|
||||||
]
|
for entry in hosts_file
|
||||||
for entry in entries:
|
if not entry.startswith("#") and entry != "\n"
|
||||||
match = re.match(r"\s*(\d+\.\d+\.\d+\.\d+)\s+(.+)", entry)
|
]
|
||||||
if match:
|
for entry in entries:
|
||||||
ip_address = match.group(1)
|
match = re.match(r"\s*(\d+\.\d+\.\d+\.\d+)\s+(.+)", entry)
|
||||||
hostnames = match.group(2).split()
|
if match:
|
||||||
for hostname in hostnames:
|
ip_address = match.group(1)
|
||||||
if hostname not in catalog:
|
hostnames = match.group(2).split()
|
||||||
catalog[hostname] = [ip_address]
|
for hostname in hostnames:
|
||||||
else:
|
if hostname not in catalog:
|
||||||
catalog[hostname].append(ip_address)
|
catalog[hostname] = [ip_address]
|
||||||
return catalog[name]
|
else:
|
||||||
|
catalog[hostname].append(ip_address)
|
||||||
|
return catalog[name]
|
||||||
|
except KeyError:
|
||||||
|
raise HTTPException(status_code=404, detail=f"Service not found: {name}")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/services")
|
@app.get("/services")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue