-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
225 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,29 @@ | ||
mod routes; | ||
|
||
use anyhow::Context; | ||
use axum::Router; | ||
use tracing::info; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
#[tokio::main] | ||
async fn main() { | ||
// initialize tracing | ||
tracing_subscriber::fmt::init(); | ||
async fn main() -> anyhow::Result<()> { | ||
// Debug | ||
std::env::set_var("RUST_LOG", "info"); | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "with_axum_htmx_askama=debug".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
info!("Initialized router!"); | ||
let port = 3000_u16; | ||
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], port)); | ||
|
||
// build our application with a route | ||
let app = Router::new() | ||
.nest("/", routes::web_routes::get_routes()) | ||
.nest("/api", routes::user_routes::get_routes()); | ||
// run our app with hyper, listening globally on port 3000 | ||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); | ||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); | ||
info!("router initialized, now listening on port {}", port); | ||
axum::serve(listener, app).await.unwrap(); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod user_routes; | ||
pub mod web_routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use askama::Template; | ||
use axum::{ | ||
http::StatusCode, | ||
response::{Html, IntoResponse}, | ||
routing::get, | ||
Response, Router, | ||
}; | ||
|
||
pub fn get_routes() -> Router { | ||
Router::new().route("/", get(get_index)) | ||
} | ||
async fn get_index() -> impl IntoResponse { | ||
let template = HelloTemplate {}; | ||
HtmlTemplate(template) | ||
} | ||
#[derive(Template)] | ||
#[template(path = "index.html")] | ||
struct HelloTemplate; | ||
struct HtmlTemplate<T>(T); | ||
impl<T> IntoResponse for HtmlTemplate<T> | ||
where | ||
T: Template, | ||
{ | ||
fn into_response(self) -> Response { | ||
match self.0.render() { | ||
Ok(html) => Html(html).into_response(), | ||
Err(e) => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("Template error: {}", e), | ||
) | ||
.into_response(), | ||
} | ||
} | ||
} |