morethantext/src/main.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-19 19:54:14 -04:00
use axum::{extract::State, response::IntoResponse, routing::get, Router};
2024-03-17 15:40:00 -04:00
use axum_extra::extract::cookie::{Cookie, CookieJar};
use clap::Parser;
2024-03-19 19:54:14 -04:00
use morethantext::{MoreThanText, Session};
2024-02-29 18:46:01 -05:00
const LOCALHOST: &str = "127.0.0.1";
2024-03-11 10:45:20 -04:00
const SESSION_KEY: &str = "sessionid";
2024-02-29 18:46:01 -05:00
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Post used
#[arg(short, long, default_value_t = 3000)]
2024-02-29 18:46:01 -05:00
port: u16,
/// IP used
#[arg(short, long, default_value_t = LOCALHOST.to_string())]
address: String,
}
2024-03-11 10:45:20 -04:00
#[cfg(test)]
mod http_session {
#[tokio::test]
async fn my_test() {
assert!(true);
}
}
#[tokio::main]
async fn main() {
let args = Args::parse();
2024-03-11 10:45:20 -04:00
let addr = format!("{}:{}", args.address, args.port);
2024-03-19 19:54:14 -04:00
let state = MoreThanText::new();
let app = Router::new().route("/", get(handler)).with_state(state);
2024-03-11 10:45:20 -04:00
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
}
2024-03-19 19:54:14 -04:00
async fn handler(jar: CookieJar, state: State<MoreThanText>) -> impl IntoResponse {
2024-03-11 10:45:20 -04:00
let cookies: CookieJar;
2024-03-19 19:54:14 -04:00
let sid: Option<String>;
2024-03-11 10:45:20 -04:00
match jar.get(SESSION_KEY) {
2024-03-19 19:54:14 -04:00
Some(cookie) => sid = Some(cookie.value().to_string()),
None => sid = None,
}
match state.get_session(sid) {
Session::Ok => cookies = jar,
Session::New(id) => {
2024-03-17 15:40:00 -04:00
let cookie = Cookie::build((SESSION_KEY, id.clone())).domain("example.com");
cookies = jar.add(cookie);
2024-03-11 10:45:20 -04:00
}
}
2024-03-19 19:54:14 -04:00
(cookies, "Something goes here.")
2024-02-26 08:41:24 -05:00
}