Skip to content

Commit

Permalink
fix: mq vhost error
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Mar 11, 2024
1 parent 91068cb commit 2e2d87d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 106 deletions.
34 changes: 27 additions & 7 deletions rust-common/src/mq/connection.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
use lapin::uri::{AMQPAuthority, AMQPUri, AMQPUserInfo};
use lapin::Connection;

use crate::mq::utils::parse_amqp_settings;
use crate::settings::Settings;

pub async fn new_amqp_connection(settings: &Settings) -> Connection {
let url = parse_amqp_settings(settings);
let mq_settings = settings.mq.as_ref().unwrap();

log::debug!("Connecting to AMQP server: {}", url);

Connection::connect(&url, lapin::ConnectionProperties::default())
.await
.unwrap()
Connection::connect_uri(
AMQPUri {
authority: AMQPAuthority {
host: mq_settings.host.clone().unwrap_or("localhost".into()),
port: mq_settings.port.unwrap_or(5672),
userinfo: AMQPUserInfo {
username: mq_settings.username.clone().unwrap_or_default(),
password: mq_settings.password.clone().unwrap_or_default(),
},
},
vhost: if let Some(vhost) = mq_settings.vhost.clone() {
if vhost.starts_with('/') {
vhost
} else {
format!("/{}", vhost)
}
} else {
"/".into()
},
..Default::default()
},
lapin::ConnectionProperties::default().with_connection_name("lapin".into()),
)
.await
.unwrap()
}
1 change: 0 additions & 1 deletion rust-common/src/mq/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod connection;
pub(crate) mod utils;
92 changes: 0 additions & 92 deletions rust-common/src/mq/utils.rs

This file was deleted.

13 changes: 7 additions & 6 deletions rust-common/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use uuid::Uuid;
pub struct Settings {
pub namespace: String,
pub version: String,
pub instance_id: Option<String>, // always exist
pub instance_id: Option<String>,
// always exist
pub debug: bool,
pub telegram_bot: Option<TelegramBot>,
pub server: Option<Server>,
Expand Down Expand Up @@ -69,7 +70,7 @@ pub struct Database {
#[serde(rename = "type")]
pub database_type: DatabaseType,
pub host: String,
pub port: i64,
pub port: u16,
pub username: Option<String>,
pub password: Option<String>,
pub name: Option<String>,
Expand All @@ -81,7 +82,7 @@ pub struct Database {
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct Mq {
pub host: Option<String>,
pub port: Option<i64>,
pub port: Option<u16>,
pub username: Option<String>,
pub password: Option<String>,
pub vhost: Option<String>,
Expand Down Expand Up @@ -150,17 +151,17 @@ pub enum RedisMode {
pub struct Redis {
pub mode: Option<RedisMode>,
pub host: Option<String>,
pub port: Option<i64>,
pub port: Option<u16>,
pub username: Option<String>,
pub password: Option<String>,
pub db: Option<i64>,
pub db: Option<u8>,
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct Server {
pub network: Option<String>,
pub address: Option<String>,
pub port: Option<i64>,
pub port: Option<u16>,
}

#[derive(Clone, PartialEq, Serialize, Deserialize)]
Expand Down

0 comments on commit 2e2d87d

Please sign in to comment.