summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-04-09 10:50:42 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-04-11 23:50:12 -0500
commit78a81465409fba33648701c8a20660259e16a807 (patch)
tree6a345b391f6067a32a884e6b388c47fb1083bcff
parentd9ed52fc239e3547eb99fe03bd296ab2808d2ebc (diff)
refactor: move jwt_max_age to const
-rw-r--r--src/config.rs48
-rw-r--r--src/routes.rs2
-rw-r--r--src/routes/login.rs3
-rw-r--r--src/routes/register.rs3
-rw-r--r--src/routes/user.rs7
-rw-r--r--src/state.rs9
6 files changed, 4 insertions, 68 deletions
diff --git a/src/config.rs b/src/config.rs
index 62e4323..d36b8fd 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -2,14 +2,12 @@ use std::{net::SocketAddr, sync::Arc};
use axum::Router;
use serde::{Deserialize, Serialize};
-use time::Duration;
use tokio::net::TcpListener;
use unnamed_server::{state::AppState, Error};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
listen_addr: Option<SocketAddr>,
- jwt_max_age: Option<String>,
jwt_secret: Option<String>,
database_url: Option<String>,
}
@@ -34,7 +32,6 @@ impl Config {
listen_addr: std::env::var(format!("{prefix}LISTEN_ADDR"))
.ok()
.and_then(|v| v.parse().ok()),
- jwt_max_age: std::env::var(format!("{prefix}JWT_MAX_AGE")).ok(),
jwt_secret: std::env::var(format!("{prefix}JWT_SECRET")).ok(),
database_url: std::env::var(format!("{prefix}DATABASE_URL")).ok(),
}))
@@ -49,16 +46,11 @@ impl Config {
};
}
- try_extract!(listen_addr, jwt_secret, jwt_max_age, database_url);
+ try_extract!(listen_addr, jwt_secret, database_url);
let listener = TcpListener::bind(listen_addr).await?;
let pool = init_db(&database_url).await?;
- let jwt_max_age = parse_duration(jwt_max_age)?;
- let app_state = Arc::new(AppState {
- pool,
- jwt_secret,
- jwt_max_age,
- });
+ let app_state = Arc::new(AppState { pool, jwt_secret });
let app = unnamed_server::init_router(app_state);
Ok((listener, app))
@@ -68,7 +60,6 @@ impl Config {
fn merge(self, other: Self) -> Self {
Self {
listen_addr: other.listen_addr.or(self.listen_addr),
- jwt_max_age: other.jwt_max_age.or(self.jwt_max_age),
jwt_secret: other.jwt_secret.or(self.jwt_secret),
database_url: other.database_url.or(self.database_url),
}
@@ -79,7 +70,6 @@ impl Default for Config {
fn default() -> Self {
Self {
listen_addr: Some(SocketAddr::from(([127, 0, 0, 1], 30000))),
- jwt_max_age: Some("1h".to_string()),
jwt_secret: None,
database_url: None,
}
@@ -96,37 +86,3 @@ async fn init_db(uri: &str) -> Result<sqlx::Pool<sqlx::Postgres>, Error> {
Ok(pool)
}
-
-fn parse_duration<S: AsRef<str>>(s: S) -> Result<Duration, Error> {
- let chars = &mut s.as_ref().chars();
- let mut nums: i64 = 0;
- let mut unit = String::new();
-
- for c in chars.by_ref() {
- if c.is_ascii_digit() {
- nums = nums * 10
- + c.to_digit(10)
- .ok_or_else(|| Error::Config("Invalid jwt_max_age".to_string()))?
- as i64;
- } else {
- unit.push(c);
- break;
- }
- }
-
- unit.extend(chars);
-
- if "weeks".contains(&unit) {
- Ok(Duration::weeks(nums))
- } else if "days".contains(&unit) {
- Ok(Duration::days(nums))
- } else if "hours".contains(&unit) {
- Ok(Duration::hours(nums))
- } else if "minutes".contains(&unit) {
- Ok(Duration::minutes(nums))
- } else if "seconds".contains(&unit) {
- Ok(Duration::seconds(nums))
- } else {
- Err(Error::Config("Invalid jwt_max_age".to_string()))
- }
-}
diff --git a/src/routes.rs b/src/routes.rs
index 73a6dc4..ad00b1e 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -49,7 +49,6 @@ mod tests {
use tower::ServiceExt;
const JWT_SECRET: &str = "test-jwt-secret-token";
- const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@@ -58,7 +57,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
diff --git a/src/routes/login.rs b/src/routes/login.rs
index 8843bd5..665659e 100644
--- a/src/routes/login.rs
+++ b/src/routes/login.rs
@@ -74,7 +74,6 @@ mod tests {
use crate::init_router;
const JWT_SECRET: &str = "test-jwt-secret-token";
- const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@@ -83,7 +82,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -110,7 +108,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
diff --git a/src/routes/register.rs b/src/routes/register.rs
index 2181808..5e06ae6 100644
--- a/src/routes/register.rs
+++ b/src/routes/register.rs
@@ -76,7 +76,6 @@ mod tests {
use crate::init_router;
const JWT_SECRET: &str = "test-jwt-secret-token";
- const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@@ -85,7 +84,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -119,7 +117,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
diff --git a/src/routes/user.rs b/src/routes/user.rs
index 3663ec6..04ab500 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -61,7 +61,6 @@ mod tests {
use crate::{init_router, model::UserSchema};
const JWT_SECRET: &str = "test-jwt-secret-token";
- const JWT_MAX_AGE: time::Duration = time::Duration::HOUR;
const UUID: uuid::Uuid = uuid::uuid!("4c14f795-86f0-4361-a02f-0edb966fb145");
type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
@@ -71,7 +70,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -107,7 +105,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -134,7 +131,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -166,7 +162,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -189,7 +184,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
@@ -210,7 +204,6 @@ mod tests {
let state = Arc::new(AppState {
pool,
jwt_secret: JWT_SECRET.to_string(),
- jwt_max_age: JWT_MAX_AGE,
});
let router = init_router(state.clone());
diff --git a/src/state.rs b/src/state.rs
index 4531a42..22234f3 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -13,16 +13,11 @@ use crate::Error;
pub struct AppState {
pub pool: Pool<Postgres>,
pub jwt_secret: String,
- pub jwt_max_age: time::Duration,
}
impl AppState {
- pub fn new(pool: Pool<Postgres>, jwt_secret: String, jwt_max_age: time::Duration) -> Self {
- Self {
- pool,
- jwt_secret,
- jwt_max_age,
- }
+ pub fn new(pool: Pool<Postgres>, jwt_secret: String) -> Self {
+ Self { pool, jwt_secret }
}
}