Skip to content

Commit

Permalink
chore(websocket): remove unnecessary dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kasugamirai committed Sep 23, 2024
1 parent 1c5a483 commit 96e30ed
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 33 deletions.
15 changes: 0 additions & 15 deletions websocket/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ axum-macros = "0.4"
base64 = "0.22.1"
cached = "0.53.1"
chrono = { version = "0.4", features = ["serde"] }
dashmap = "6.1.0"
dotenv = "0.15.0"
futures-util = "0.3"
google-cloud-storage = "0.18"
Expand Down
32 changes: 32 additions & 0 deletions websocket/crates/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,35 @@ impl Config {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::env;

#[test]
fn test_from_env() {
env::set_var("SERVER_ADDR", "192.168.1.1:9000");
env::set_var(
"AUTH_SERVICE_URL",
"http://192.168.1.1:8080/api/validate-token",
);

let config = Config::from_env().unwrap();
assert_eq!(config.server_addr, "192.168.1.1:9000");
assert_eq!(
config.auth_service_url,
"http://192.168.1.1:8080/api/validate-token"
);

env::remove_var("SERVER_ADDR");
env::remove_var("AUTH_SERVICE_URL");

let config = Config::from_env().unwrap();
assert_eq!(config.server_addr, "127.0.0.1:8000");
assert_eq!(
config.auth_service_url,
"http://127.0.0.1:8081/api/validate-token"
);
}
}
1 change: 0 additions & 1 deletion websocket/crates/infra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ axum-macros.workspace = true
base64.workspace = true
cached.workspace = true
chrono.workspace = true
dashmap.workspace = true
dotenv.workspace = true
futures-util.workspace = true
google-cloud-storage.workspace = true
Expand Down
62 changes: 46 additions & 16 deletions websocket/crates/infra/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod socket;

#[tokio::main]
async fn main() -> std::io::Result<()> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "trace,tower_http=debug,".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
async fn main() {
init_tracing_subscriber();

let config = Config::from_env().expect("Failed to load configuration");
let config = match Config::from_env() {
Ok(cfg) => cfg,
Err(e) => {
tracing::error!("Failed to load configuration: {}", e);
return;
}
};

let auth_client = match AuthServiceClient::new(&config.auth_service_url) {
Ok(client) => client,
Err(e) => {
tracing::error!("Failed to create auth client: {}", e);
return;
}
};

let auth_client =
AuthServiceClient::new(&config.auth_service_url).expect("Failed to create auth client");
let jwt_validator = JwtValidator::new(auth_client, Duration::from_secs(300));

let state = Arc::new(AppState::default());
Expand All @@ -55,13 +61,37 @@ async fn main() -> std::io::Result<()> {
)
.with_state(state);

let listener = tokio::net::TcpListener::bind(&config.server_addr)
.await
.unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(
let listener = match tokio::net::TcpListener::bind(&config.server_addr).await {
Ok(listener) => listener,
Err(e) => {
tracing::error!("Failed to bind to address {}: {}", &config.server_addr, e);
std::process::exit(1);
}
};
tracing::debug!(
"listening on {}",
listener.local_addr().unwrap_or_else(|e| {
tracing::error!("Failed to get local address: {}", e);
std::process::exit(1);
})
);
if let Err(e) = axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.await
{
tracing::error!("server error: {}", e);
}
}

#[inline]
fn init_tracing_subscriber() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "trace,tower_http=debug,".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
}

0 comments on commit 96e30ed

Please sign in to comment.