added homepage
This commit is contained in:
commit
1936d7cab9
13 changed files with 1262 additions and 0 deletions
59
src/main.rs
Normal file
59
src/main.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use askama::Template;
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use tracing::info;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "portfolio=info".into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
info!("hello, web server!");
|
||||
// build our application with a single route
|
||||
let app = Router::new().route("/", get(root));
|
||||
|
||||
// run our app with hyper, listening globally on port 3000
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
async fn root() -> impl IntoResponse {
|
||||
let template = HelloTemplate {};
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct HelloTemplate;
|
||||
|
||||
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
||||
/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
// Attempt to render the template with askama
|
||||
match self.0.render() {
|
||||
// If we're able to successfully parse and aggregate the template, serve it
|
||||
Ok(html) => Html(html).into_response(),
|
||||
// If we're not, return an error or some bit of fallback HTML
|
||||
Err(err) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to render template. Error: {}", err),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue