Initial
This commit is contained in:
+146
@@ -0,0 +1,146 @@
|
||||
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 serde::Deserialize;
|
||||
use sqlx::{query, SqlitePool};
|
||||
use std::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.");
|
||||
|
||||
sqlx::query!(
|
||||
"CREATE TABLE IF NOT EXISTS anmeldungen (
|
||||
id INTEGER PRIMARY KEY NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
klasse TEXT NOT NULL
|
||||
)"
|
||||
)
|
||||
.execute(&db_pool)
|
||||
.await
|
||||
.expect("Failed to create the table.");
|
||||
|
||||
// build our application with some routes
|
||||
let app = Router::new()
|
||||
.nest_service("/static", ServeDir::new("static"))
|
||||
.route("/greet/:name", get(greet))
|
||||
.route("/register/", post(register))
|
||||
.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 greet(extract::Path(name): extract::Path<String>) -> impl IntoResponse {
|
||||
let template = HelloTemplate { name };
|
||||
HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "hello.html")]
|
||||
struct HelloTemplate {
|
||||
name: String,
|
||||
}
|
||||
|
||||
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(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<sqlx::Sqlite>);
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for DatabaseConnection
|
||||
where
|
||||
SqlitePool: FromRef<S>,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = (StatusCode, String);
|
||||
|
||||
async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let pool = SqlitePool::from_ref(state);
|
||||
|
||||
let conn = pool.acquire().await.map_err(internal_error)?;
|
||||
|
||||
Ok(Self(conn))
|
||||
}
|
||||
}
|
||||
|
||||
async fn register(
|
||||
DatabaseConnection(conn): DatabaseConnection,
|
||||
Form(form): Form<RegistrationForm>,
|
||||
) -> 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<E>(err: E) -> (StatusCode, String)
|
||||
where
|
||||
E: std::error::Error,
|
||||
{
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
|
||||
}
|
||||
Reference in New Issue
Block a user