Initial
This commit is contained in:
commit
6617df1a1d
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
sqlite.db*
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,19 @@
|
|||
[package]
|
||||
name = "make_uhle"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.6", features = ["json"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
sqlx = { version = "0.6", features = ["runtime-tokio-native-tls", "sqlite"] }
|
||||
anyhow = "1.0"
|
||||
askama = "0.12"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
tower = { version = "0.4", features = ["util"] }
|
||||
tower-http = { version = "0.4.0", features = ["fs", "trace"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
hyper = { version = "0.14", features = ["full"] }
|
|
@ -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())
|
||||
}
|
File diff suppressed because one or more lines are too long
Binary file not shown.
After Width: | Height: | Size: 298 KiB |
|
@ -0,0 +1,58 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Hello Bulma!</title>
|
||||
<link rel="stylesheet" href="/static/bulma.min.css">
|
||||
</head>
|
||||
|
||||
|
||||
|
||||
<body>
|
||||
<section class="hero is-success is-fullheight">
|
||||
<div class="hero-body">
|
||||
<div class="container has-text-centered">
|
||||
<div class="column is-4 is-offset-4">
|
||||
<div class="box">
|
||||
<figure class="avatar">
|
||||
<img src="/static/logo.jpeg">
|
||||
</figure>
|
||||
<p>Melden Sie sich an, wenn Sie Interesse an einer IT-Make-AG an der Waldorfschule Uhlandshöhe haben.</p>
|
||||
<form action="/register/" method="POST">
|
||||
<div class="field">
|
||||
<label class="label" for="name">Name</label>
|
||||
<div class="control">
|
||||
<input class="input is-large" type="text" id="name" name="name" placeholder="Dein Name" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="email">E-Mail</label>
|
||||
<div class="control">
|
||||
<input class="input is-large" type="email" id="email" name="email" placeholder="Deine E-Mail-Adresse"
|
||||
required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="grade">Klasse</label>
|
||||
<div class="control">
|
||||
<input class="input is-large" type="text" id="klasse" name="klasse" placeholder="Deine Klasse"
|
||||
required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="control">
|
||||
<button class="button is-primary" type="submit">Registrieren</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<script async type="text/javascript" src="../js/bulma.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue