mirror of
https://github.com/Xevion/banner.git
synced 2025-12-06 03:14:24 -06:00
chore: drop redis
This commit is contained in:
74
Cargo.lock
generated
74
Cargo.lock
generated
@@ -188,7 +188,6 @@ dependencies = [
|
|||||||
"once_cell",
|
"once_cell",
|
||||||
"poise",
|
"poise",
|
||||||
"rand 0.9.2",
|
"rand 0.9.2",
|
||||||
"redis",
|
|
||||||
"regex",
|
"regex",
|
||||||
"reqwest 0.12.23",
|
"reqwest 0.12.23",
|
||||||
"reqwest-middleware",
|
"reqwest-middleware",
|
||||||
@@ -338,20 +337,6 @@ dependencies = [
|
|||||||
"windows-link 0.2.0",
|
"windows-link 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "combine"
|
|
||||||
version = "4.6.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd"
|
|
||||||
dependencies = [
|
|
||||||
"bytes",
|
|
||||||
"futures-core",
|
|
||||||
"memchr",
|
|
||||||
"pin-project-lite",
|
|
||||||
"tokio",
|
|
||||||
"tokio-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "command_attr"
|
name = "command_attr"
|
||||||
version = "0.5.3"
|
version = "0.5.3"
|
||||||
@@ -1667,16 +1652,6 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-bigint"
|
|
||||||
version = "0.4.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
|
||||||
dependencies = [
|
|
||||||
"num-integer",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-bigint-dig"
|
name = "num-bigint-dig"
|
||||||
version = "0.8.4"
|
version = "0.8.4"
|
||||||
@@ -2043,17 +2018,6 @@ version = "5.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "r2d2"
|
|
||||||
version = "0.8.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "51de85fb3fb6524929c8a2eb85e6b6d363de4e8c48f9e2c2eac4944abc181c93"
|
|
||||||
dependencies = [
|
|
||||||
"log",
|
|
||||||
"parking_lot",
|
|
||||||
"scheduled-thread-pool",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rand"
|
name = "rand"
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
@@ -2122,29 +2086,6 @@ dependencies = [
|
|||||||
"bitflags 2.9.4",
|
"bitflags 2.9.4",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "redis"
|
|
||||||
version = "0.32.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "7cd3650deebc68526b304898b192fa4102a4ef0b9ada24da096559cb60e0eef8"
|
|
||||||
dependencies = [
|
|
||||||
"bytes",
|
|
||||||
"cfg-if",
|
|
||||||
"combine",
|
|
||||||
"futures-util",
|
|
||||||
"itoa",
|
|
||||||
"num-bigint",
|
|
||||||
"percent-encoding",
|
|
||||||
"pin-project-lite",
|
|
||||||
"r2d2",
|
|
||||||
"ryu",
|
|
||||||
"sha1_smol",
|
|
||||||
"socket2 0.6.0",
|
|
||||||
"tokio",
|
|
||||||
"tokio-util",
|
|
||||||
"url",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.17"
|
version = "0.5.17"
|
||||||
@@ -2466,15 +2407,6 @@ dependencies = [
|
|||||||
"windows-sys 0.59.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "scheduled-thread-pool"
|
|
||||||
version = "0.2.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3cbc66816425a074528352f5789333ecff06ca41b36b0b0efdfbb29edc391a19"
|
|
||||||
dependencies = [
|
|
||||||
"parking_lot",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@@ -2653,12 +2585,6 @@ dependencies = [
|
|||||||
"digest",
|
"digest",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sha1_smol"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha2"
|
name = "sha2"
|
||||||
version = "0.10.9"
|
version = "0.10.9"
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ futures = "0.3"
|
|||||||
http = "1.3.1"
|
http = "1.3.1"
|
||||||
poise = "0.6.1"
|
poise = "0.6.1"
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
redis = { version = "0.32.5", features = ["tokio-comp", "r2d2"] }
|
|
||||||
regex = "1.10"
|
regex = "1.10"
|
||||||
reqwest = { version = "0.12.23", features = ["json", "cookies"] }
|
reqwest = { version = "0.12.23", features = ["json", "cookies"] }
|
||||||
reqwest-middleware = { version = "0.4.2", features = ["json"] }
|
reqwest-middleware = { version = "0.4.2", features = ["json"] }
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ pub struct Config {
|
|||||||
pub port: u16,
|
pub port: u16,
|
||||||
/// Database connection URL
|
/// Database connection URL
|
||||||
pub database_url: String,
|
pub database_url: String,
|
||||||
/// Redis connection URL
|
|
||||||
pub redis_url: String,
|
|
||||||
/// Graceful shutdown timeout duration
|
/// Graceful shutdown timeout duration
|
||||||
///
|
///
|
||||||
/// Accepts both numeric values (seconds) and duration strings
|
/// Accepts both numeric values (seconds) and duration strings
|
||||||
|
|||||||
@@ -118,8 +118,7 @@ async fn main() {
|
|||||||
.expect("Failed to create BannerApi");
|
.expect("Failed to create BannerApi");
|
||||||
|
|
||||||
let banner_api_arc = Arc::new(banner_api);
|
let banner_api_arc = Arc::new(banner_api);
|
||||||
let app_state = AppState::new(banner_api_arc.clone(), &config.redis_url, db_pool.clone())
|
let app_state = AppState::new(banner_api_arc.clone(), db_pool.clone());
|
||||||
.expect("Failed to create AppState");
|
|
||||||
|
|
||||||
// Create BannerState for web service
|
// Create BannerState for web service
|
||||||
let banner_state = BannerState {
|
let banner_state = BannerState {
|
||||||
|
|||||||
38
src/state.rs
38
src/state.rs
@@ -3,51 +3,29 @@
|
|||||||
use crate::banner::BannerApi;
|
use crate::banner::BannerApi;
|
||||||
use crate::banner::Course;
|
use crate::banner::Course;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use redis::AsyncCommands;
|
|
||||||
use redis::Client;
|
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub banner_api: Arc<BannerApi>,
|
pub banner_api: Arc<BannerApi>,
|
||||||
pub redis: Arc<Client>,
|
|
||||||
pub db_pool: PgPool,
|
pub db_pool: PgPool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new(
|
pub fn new(banner_api: Arc<BannerApi>, db_pool: PgPool) -> Self {
|
||||||
banner_api: Arc<BannerApi>,
|
Self {
|
||||||
redis_url: &str,
|
|
||||||
db_pool: PgPool,
|
|
||||||
) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
|
|
||||||
let redis_client = Client::open(redis_url)?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
banner_api,
|
banner_api,
|
||||||
redis: Arc::new(redis_client),
|
|
||||||
db_pool,
|
db_pool,
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a course by CRN with Redis cache fallback to Banner API
|
/// Get a course by CRN directly from Banner API
|
||||||
pub async fn get_course_or_fetch(&self, term: &str, crn: &str) -> Result<Course> {
|
pub async fn get_course_or_fetch(&self, term: &str, crn: &str) -> Result<Course> {
|
||||||
let mut conn = self.redis.get_multiplexed_async_connection().await?;
|
self.banner_api
|
||||||
|
.get_course_by_crn(term, crn)
|
||||||
let key = format!("class:{crn}");
|
.await?
|
||||||
if let Some(serialized) = conn.get::<_, Option<String>>(&key).await? {
|
.ok_or_else(|| anyhow::anyhow!("Course not found for CRN {crn}"))
|
||||||
let course: Course = serde_json::from_str(&serialized)?;
|
|
||||||
return Ok(course);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: fetch from Banner API
|
|
||||||
if let Some(course) = self.banner_api.get_course_by_crn(term, crn).await? {
|
|
||||||
let serialized = serde_json::to_string(&course)?;
|
|
||||||
let _: () = conn.set(&key, serialized).await?;
|
|
||||||
return Ok(course);
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(anyhow::anyhow!("Course not found for CRN {crn}"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the total number of courses in the database
|
/// Get the total number of courses in the database
|
||||||
|
|||||||
@@ -69,18 +69,8 @@ async fn status(State(_state): State<BannerState>) -> Json<Value> {
|
|||||||
async fn metrics(State(_state): State<BannerState>) -> Json<Value> {
|
async fn metrics(State(_state): State<BannerState>) -> Json<Value> {
|
||||||
// For now, return basic metrics structure
|
// For now, return basic metrics structure
|
||||||
Json(json!({
|
Json(json!({
|
||||||
"redis": {
|
"banner_api": {
|
||||||
"status": "connected",
|
"status": "connected"
|
||||||
"connected_clients": "TODO: implement client counting",
|
|
||||||
"used_memory": "TODO: implement memory tracking"
|
|
||||||
},
|
|
||||||
"cache": {
|
|
||||||
"courses": {
|
|
||||||
"count": "TODO: implement course counting"
|
|
||||||
},
|
|
||||||
"subjects": {
|
|
||||||
"count": "TODO: implement subject counting"
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"timestamp": chrono::Utc::now().to_rfc3339()
|
"timestamp": chrono::Utc::now().to_rfc3339()
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user