Added session id.

This commit is contained in:
Jeff Baskin 2024-03-11 10:45:20 -04:00
parent a493fbf387
commit f3722e46e4
3 changed files with 216 additions and 687 deletions

853
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,6 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.7.4" axum = "0.7.4"
axum-extra = { version = "0.9.2", features = ["cookie-signed"] }
clap = { version = "4.5.1", features = ["derive"] } clap = { version = "4.5.1", features = ["derive"] }
dioxus = "0.4.3"
dioxus-ssr = "0.4.3"
tokio = { version = "1.36.0", features = ["full"] } tokio = { version = "1.36.0", features = ["full"] }

View File

@ -1,8 +1,13 @@
use axum::{response::Html, routing::get, Router}; use axum::{
response::IntoResponse,
routing::get,
Router,
};
use axum_extra::extract::cookie::{CookieJar, Cookie};
use clap::Parser; use clap::Parser;
use dioxus::prelude::*;
const LOCALHOST: &str = "127.0.0.1"; const LOCALHOST: &str = "127.0.0.1";
const SESSION_KEY: &str = "sessionid";
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -15,18 +20,38 @@ struct Args {
address: String, address: String,
} }
#[cfg(test)]
mod http_session {
#[tokio::test]
async fn my_test() {
assert!(true);
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let args = Args::parse(); let args = Args::parse();
let url = format!("{}:{}", args.address, args.port); let addr = format!("{}:{}", args.address, args.port);
let app = Router::new().route("/", get(|| app_endpoint())); let app = Router::new()
let listener = tokio::net::TcpListener::bind(&url).await.unwrap(); .route("/", get(handler));
axum::serve(listener, app).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
} }
async fn app_endpoint() -> Html<String> { async fn handler(jar: CookieJar) -> impl IntoResponse {
// render the rsx! macro to HTML let cookies: CookieJar;
Html(dioxus_ssr::render_lazy(rsx! { let id: String;
div { "hello world!" } match jar.get(SESSION_KEY) {
})) Some(session) => {
id = session.to_string();
cookies = jar;
},
None => {
id = "Fred".to_string();
cookies = jar.add(Cookie::new(SESSION_KEY, id.clone()));
}
}
(cookies, format!("id is {}", id))
} }