use askama::Template; use axum::{ async_trait, extract::{self, FromRef, FromRequestParts}, http::{request::Parts, StatusCode}, response::{Html, IntoResponse, Response}, routing::{get, post}, Form, Json, Router, }; use hyper::{header, HeaderMap}; use serde::Deserialize; use sqlx::{query, SqlitePool}; use std::{env, net::SocketAddr}; use tower_http::services::ServeDir; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; const DB_URL: &str = "sqlite://sqlite.db"; #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| "example_templates=debug".into()), ) .with(tracing_subscriber::fmt::layer()) .init(); let db_pool = SqlitePool::connect(DB_URL) .await .expect("Failed to connect to the database."); let static_path = env::var("STATIC_PATH").unwrap_or("static".to_string()); // build our application with some routes let app = Router::new() .nest_service("/static", ServeDir::new(static_path)) .route("/", get(register_form)) .route("/register/", post(register)) .route("/favicon.ico", get(favicon)) .with_state(db_pool); // run it let addr = SocketAddr::from(([127, 0, 0, 1], 8035)); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn register_form() -> impl IntoResponse { let template = HelloTemplate {}; HtmlTemplate(template) } #[derive(Template)] #[template(path = "hello.html")] struct HelloTemplate {} struct HtmlTemplate(T); impl IntoResponse for HtmlTemplate where T: Template, { fn into_response(self) -> Response { match self.0.render() { Ok(html) => Html(html).into_response(), Err(err) => ( StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to render template. Error: {}", err), ) .into_response(), } } } #[derive(Deserialize)] struct RegistrationForm { name: String, email: String, klasse: String, } struct DatabaseConnection(sqlx::pool::PoolConnection); #[async_trait] impl FromRequestParts for DatabaseConnection where SqlitePool: FromRef, S: Send + Sync, { type Rejection = (StatusCode, String); async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result { let pool = SqlitePool::from_ref(state); let conn = pool.acquire().await.map_err(internal_error)?; Ok(Self(conn)) } } // register a user async fn register( DatabaseConnection(conn): DatabaseConnection, Form(form): Form, ) -> impl IntoResponse { let mut conn = conn; // Insert the form data into the database let result = query!( "INSERT INTO anmeldungen (name, email, klasse) VALUES (?, ?, ?)", form.name, form.email, form.klasse ) .execute(&mut conn) .await; if let Err(error) = result { eprintln!("Failed to insert data into database: {:?}", error); return StatusCode::INTERNAL_SERVER_ERROR; } // Return a simple response StatusCode::OK } /// Utility function for mapping any error into a `500 Internal Server Error` /// response. fn internal_error(err: E) -> (StatusCode, String) where E: std::error::Error, { (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) } async fn favicon() -> impl IntoResponse { let mut headers = HeaderMap::new(); headers.insert(header::CONTENT_TYPE, "image/x-icon".parse().unwrap()); let bytes = include_bytes!("../static/favicon.ico"); (headers, bytes) }