extract language
This commit is contained in:
37
src/main.rs
37
src/main.rs
@@ -1,5 +1,5 @@
|
||||
use crate::model::client::Client;
|
||||
use axum::{routing::get, Router};
|
||||
use axum::{http::HeaderMap, routing::get, Router};
|
||||
use axum_extra::extract::{cookie::Cookie, CookieJar};
|
||||
use sqlx::{pool::PoolOptions, sqlite::SqliteConnectOptions, SqlitePool};
|
||||
use std::{str::FromStr, sync::Arc};
|
||||
@@ -8,6 +8,7 @@ use uuid::Uuid;
|
||||
|
||||
mod game;
|
||||
mod index;
|
||||
pub(crate) mod language;
|
||||
pub(crate) mod model;
|
||||
mod page;
|
||||
pub(crate) mod random_names;
|
||||
@@ -16,6 +17,27 @@ pub(crate) enum Backend {
|
||||
Sqlite(SqlitePool),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Language {
|
||||
German,
|
||||
English,
|
||||
}
|
||||
|
||||
impl From<String> for Language {
|
||||
fn from(value: String) -> Self {
|
||||
if value.starts_with("de") {
|
||||
Language::German
|
||||
} else {
|
||||
Language::English
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Req {
|
||||
client: Client,
|
||||
lang: Language,
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
async fn client(&self, cookies: CookieJar) -> (CookieJar, Client) {
|
||||
let existing_uuid = cookies
|
||||
@@ -31,6 +53,19 @@ impl Backend {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Combined method for getting both client and language
|
||||
async fn client_full(&self, cookies: CookieJar, headers: &HeaderMap) -> (CookieJar, Req) {
|
||||
let (cookies, client) = self.client(cookies).await;
|
||||
let lang = language::language(&cookies, headers);
|
||||
(
|
||||
cookies,
|
||||
Req {
|
||||
client,
|
||||
lang: lang.into(),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
Reference in New Issue
Block a user