use axum::{routing::get, Router};
use axum_session::{Session, SessionConfig, SessionLayer, SessionNullPool, SessionStore};
use std::net::SocketAddr;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let session_config = SessionConfig::default().with_table_name("sessions_table");
let session_store = SessionStore::<SessionNullPool>::new(None, session_config)
.await
.unwrap();
let app = Router::new()
.route("/greet", get(greet))
.layer(SessionLayer::new(session_store));
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
.unwrap();
}
async fn greet(session: Session<SessionNullPool>) -> String {
let mut count: usize = session.get("count").unwrap_or(0);
count += 1;
session.set("count", count);
count.to_string()
}