Skip to content

Commit

Permalink
chores: add dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ikurotime committed Dec 28, 2023
1 parent 293c1c3 commit 9803eba
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 7 deletions.
167 changes: 166 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.77"
askama = "0.12.1"
axum = "0.7.2"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
tokio = { version = "1.35.1", features = ["full"] }
tower = "0.4.13"
tower-http = { version = "0.5.0", features = ["fs"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
26 changes: 20 additions & 6 deletions src/main.rs
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(())
}
1 change: 1 addition & 0 deletions src/routes.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod user_routes;
pub mod web_routes;
34 changes: 34 additions & 0 deletions src/routes/web_routes.rs
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(),
}
}
}

0 comments on commit 9803eba

Please sign in to comment.