add `/<uuid> route + backend handling
This commit is contained in:
27
src/main.rs
27
src/main.rs
@@ -1,3 +1,4 @@
|
||||
use crate::model::client::Client;
|
||||
use axum::{routing::get, Router};
|
||||
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
||||
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
|
||||
@@ -14,19 +15,21 @@ pub(crate) enum Backend {
|
||||
Sqlite(SqlitePool),
|
||||
}
|
||||
|
||||
fn client_id(cookies: CookieJar) -> (CookieJar, String) {
|
||||
let mut cookies = cookies;
|
||||
if cookies.get("client_id").is_none() {
|
||||
let id = Uuid::new_v4().to_string();
|
||||
cookies = cookies.add(Cookie::new("client_id", id))
|
||||
impl Backend {
|
||||
async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) {
|
||||
let existing_uuid = cookies
|
||||
.get("client_id")
|
||||
.and_then(|cookie| Uuid::parse_str(cookie.value()).ok());
|
||||
|
||||
match existing_uuid {
|
||||
Some(uuid) => (cookies, self.get_client(&uuid).await),
|
||||
None => {
|
||||
let new_id = Uuid::new_v4();
|
||||
let updated_cookies = cookies.add(Cookie::new("client_id", new_id.to_string()));
|
||||
(updated_cookies, self.get_client(&new_id).await)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let id = cookies
|
||||
.get("client_id")
|
||||
.expect("can't happen, as we checked above")
|
||||
.to_string();
|
||||
|
||||
(cookies, id)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
Reference in New Issue
Block a user