Add index page to display routes

This commit is contained in:
Christopher Williams 2024-10-07 18:44:08 -04:00
parent e72a47cc77
commit 6075d7be00
3 changed files with 26 additions and 11 deletions

View File

@ -82,11 +82,8 @@ impl UserData for HtmlParser {
Ok(())
},
);
methods.add_method(
"render",
|_, this, _: ()| -> Result<String, mlua::Error> {
Ok(this.get_html())
},
);
methods.add_method("render", |_, this, _: ()| -> Result<String, mlua::Error> {
Ok(this.get_html())
});
}
}

View File

@ -60,10 +60,9 @@ async fn main() {
let registered_routes = engine.get_routes().unwrap();
routes_ref.write().unwrap().extend(registered_routes);
routes_ref
std::mem::drop(routes_ref
.write()
.unwrap()
.insert("main".to_string(), "/".to_string());
.unwrap());
let mut r = is_ready_ref.lock().unwrap();
*r = true;

View File

@ -62,13 +62,32 @@ impl Router {
}
}
let r = self.routes.clone();
let r2 = self.routes.clone();
let index = warp::path::end().map(move || {
let mut html_str = "".to_string();
// Create table for the routes with borders
html_str.push_str("<style>table {border-collapse: collapse;} table, th, td {border: 1px solid black;}</style>");
html_str.push_str("<table>");
html_str.push_str("<tr><th>Service</th><th>Endpoint</th></tr>");
for (route, response) in r.iter() {
html_str.push_str("<tr>");
html_str.push_str(&format!("<td>{}</td>", route));
html_str.push_str(&format!("<td>{}</td>", response));
html_str.push_str("</tr>");
}
html_str.push_str("</table>");
warp::reply::html(html_str)
});
let routes = warp::path::param().and_then({
move |word: String| {
let s = r.clone();
let s = r2.clone();
async move { dyn_reply(word, &s).await }
}
});
})
.or(index);
warp::serve(routes).run(([0, 0, 0, 0], self.port)).await;
}
}