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]
axum = "0.7.4"
axum-extra = { version = "0.9.2", features = ["cookie-signed"] }
clap = { version = "4.5.1", features = ["derive"] }
dioxus = "0.4.3"
dioxus-ssr = "0.4.3"
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 dioxus::prelude::*;
const LOCALHOST: &str = "127.0.0.1";
const SESSION_KEY: &str = "sessionid";
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@ -15,18 +20,38 @@ struct Args {
address: String,
}
#[cfg(test)]
mod http_session {
#[tokio::test]
async fn my_test() {
assert!(true);
}
}
#[tokio::main]
async fn main() {
let args = Args::parse();
let url = format!("{}:{}", args.address, args.port);
let app = Router::new().route("/", get(|| app_endpoint()));
let listener = tokio::net::TcpListener::bind(&url).await.unwrap();
axum::serve(listener, app).await.unwrap();
let addr = format!("{}:{}", args.address, args.port);
let app = Router::new()
.route("/", get(handler));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
async fn app_endpoint() -> Html<String> {
// render the rsx! macro to HTML
Html(dioxus_ssr::render_lazy(rsx! {
div { "hello world!" }
}))
async fn handler(jar: CookieJar) -> impl IntoResponse {
let cookies: CookieJar;
let id: String;
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))
}