Skip to content

Commit

Permalink
Merge pull request #190 from Enraged-Dun-Cookie-Development-Team/feat…
Browse files Browse the repository at this point in the history
…-腾讯cdn刷新

腾讯云包,cdn刷新url
  • Loading branch information
phidiaLam authored Jun 9, 2024
2 parents 732c845 + 8fc7896 commit 885656a
Show file tree
Hide file tree
Showing 80 changed files with 1,555 additions and 423 deletions.
18 changes: 14 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,19 @@ bitmap-convert = { version = "0.1.0", path = "./libs/bitmap-convert" }
bitmaps = "3.2.0"
qiniu-cdn-upload = { version = "0.1.0", path = "middle-server/qiniu-cdn-upload" }
mob-push-server = { version = "0.1.0", path = "middle-server/mob-push-server" }
tencent-cloud-server = { version = "0.1.0", path = "middle-server/tencent-cloud-server" }
qiniu_service = { version = "0.1.0", path = "service/qiniu_service" }
persistence = { version = "0.1.0", path = "persistence/persistence" }
time-utils = { version = "0.1.0", path = "libs/time-utils" }
bson = { version = "2.4.0", features = ["chrono-0_4", "uuid-1"] }
secrecy = "0.8.0"
sha2 = "0.10.2"
hmac = "0.12.1"
hex = "0.4"
serde_qs = "0.13.0"
request-clients = { version = "0.1.0", path = "middle-server/request-clients" }
mime = "0.3.16"

[dependencies]

# async
Expand All @@ -111,8 +120,8 @@ chrono = { workspace = true, features = ["serde"] }
# config
figment = { version = "0.10", features = ["yaml", "json", "toml", "env"] }
futures = { workspace = true }
hex = "0.4"
hmac = "0.12.1"
hex = { workspace = true }
hmac = { workspace = true }
http-body = "1"
#encrypt
jwt = "0.16.0"
Expand All @@ -122,7 +131,7 @@ pin-project = "1"
rand = "0.8.5"
# request
reqwest = { workspace = true }
sha2 = "0.10.2"
sha2 = { workspace = true }
tap = { workspace = true }
thiserror = { workspace = true }
tower = { version = "0.4", features = ["limit"] }
Expand All @@ -135,7 +144,7 @@ tracing = { workspace = true }
dotenv = "0.15"
tracing-unwrap = { workspace = true }
uuid = { version = "1.2.2", features = ["v4"] }
request-clients = { version = "0.1.0", path = "middle-server/request-clients" }
request-clients.workspace = true
general-request-client.workspace = true
scheduler-notifier.workspace = true
bool_or.workspace = true
Expand All @@ -146,6 +155,7 @@ qq-channel-warning = { version = "0.1.0", path = "middle-server/qq-channel-warni
bitmap-convert.workspace = true
bitmaps.workspace = true
persistence = { workspace = true, features = ["prelude", "migrate"] }
tencent-cloud-server.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.5.4"
Expand Down
2 changes: 1 addition & 1 deletion libs/qiniu-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ axum-core = { workspace = true }
axum-starter = { workspace = true }
bytes = { version = "1.2.1", features = ["serde"] }
futures = { workspace = true }
mime = "0.3.16"
mime.workspace = true
mime_guess = "2.0.4"
qiniu-objects-manager = { version = "0.2.0", features = ["async", "reqwest"] }
qiniu-upload-manager = { version = "0.2.0", features = ["async", "reqwest"] }
Expand Down
2 changes: 2 additions & 0 deletions libs/status-err/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ bincode = "1"
status-err-derive = { path = "../../macros/status-err-derive" }
thiserror = { workspace = true }
tonic = "0.9.2"
serde_qs.workspace = true
hmac.workspace = true

[dependencies.serde]
workspace = true
Expand Down
14 changes: 14 additions & 0 deletions libs/status-err/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,17 @@ status_error!(
0x000A
]->"Version格式检查错误"
);

status_error!(
serde_qs::Error[
ErrPrefix::PARSE,
0x000A
]->"Query 序列化、反序列化异常"
);

status_error!(
hmac::digest::InvalidLength[
ErrPrefix::PARSE,
0x000B
]->"HMAC加密长度错误"
);
2 changes: 2 additions & 0 deletions libs/status-err/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ impl ErrPrefix {
pub const SEA_ORM: Self = Self('D', HttpCode::INTERNAL_SERVER_ERROR);
/// actix 框架产生的异常
pub const SERVE: Self = Self('F', HttpCode::INTERNAL_SERVER_ERROR);
/// 腾讯云服务异常
pub const TC_CLOUD: Self = Self('T', HttpCode::INTERNAL_SERVER_ERROR);
/// 权限认证异常
pub const UNAUTHORIZED: Self = Self('A', HttpCode::UNAUTHORIZED);

Expand Down
18 changes: 17 additions & 1 deletion libs/time-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
pub use chrono;
use chrono::{DateTime, Local, NaiveDateTime};
use chrono::{DateTime, Local, NaiveDate, NaiveDateTime};
use mongodb::bson;

const TIME_FORMAT: &str = "%Y-%m-%d %T";
const DATE_FORMAT: &str = "%Y-%m-%d";

pub fn naive_date_format(date: NaiveDate) -> String {
date.format(DATE_FORMAT).to_string()
}

pub fn naive_date_time_format(date_time: NaiveDateTime) -> String {
date_time.format(TIME_FORMAT).to_string()
}

pub fn bson_date_time_format(date: bson::DateTime) -> String {
date.to_chrono().format(TIME_FORMAT).to_string()
}

#[cfg(feature = "with-sea-orm")]
pub fn get_now_naive_date_time_value() -> sea_orm::Value {
Expand Down
3 changes: 3 additions & 0 deletions logic/bakery_logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ serde.workspace = true
serde_json.workspace = true
typed-builder.workspace = true
persistence = { workspace = true, features = ["model-bakery", "help-crates"] }
chrono.workspace = true
tencent-cloud-server.workspace = true
serde_qs.workspace = true
9 changes: 9 additions & 0 deletions logic/bakery_logic/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ use std::convert::Infallible;

use persistence::bakery::mansion::OperateError as MansionOperateError;
use status_err::StatusErr;
use tencent_cloud_server::error::TcCloudError;
use thiserror::Error;

#[derive(Debug, Error, StatusErr)]
pub enum LogicError {
#[error(transparent)]
#[status_err(err = "transparent")]
TempListOperateError(#[from] MansionOperateError),

#[error(transparent)]
#[status_err(err = "transparent")]
TcCloudError(#[from] TcCloudError),

#[error(transparent)]
#[status_err(err = "transparent")]
SerdeQs(#[from] serde_qs::Error),
}

impl From<Infallible> for LogicError {
Expand Down
96 changes: 94 additions & 2 deletions logic/bakery_logic/src/impletements/mansion.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use chrono::Duration;
use persistence::{
bakery::{mansion::ToMansion, ToBakery},
bakery::{
mansion::ToMansion,
models::mansion::{
checked::{Mansion, Mid},
models::{MansionId, ModelMansion},
},
ToBakery,
},
help_crates::tracing::debug,
mongodb::MongoDatabaseOperate,
};
use tencent_cloud_server::cloud_manager::TcCloudManager;

use super::BakeryLogic;
use crate::{error::LogicResult, view::MansionRecentPredictResp};
use crate::{
error::LogicResult,
view::{BakeryTcCdnPath, MansionRecentPredictResp},
};

impl BakeryLogic {
/// 获取最近日期的预测,没有就获取结果
Expand All @@ -19,4 +32,83 @@ impl BakeryLogic {
}
Ok(recent_predict.map(|item| item.into()))
}

/// 保存大厦
pub async fn save_mansion(
mongo: MongoDatabaseOperate, tc_cloud: TcCloudManager,
mid: Option<MansionId>, mansion: Mansion,
) -> LogicResult<()> {
let mut paths = vec![
BakeryTcCdnPath::MANSION_ID_PATH,
BakeryTcCdnPath::RECENT_PREDICT_PATH,
BakeryTcCdnPath::MANSION_INFO_PATH(&mansion.id.to_string())?,
];
match mid {
Some(mid) => {
debug!(
mansion.id.provide = true,
mansion.saveMode = "Update"
);
mongo
.bakery()
.mansion()
.update(mid.clone(), mansion.clone())
.await?;
if mansion.id != mid {
paths.push(BakeryTcCdnPath::MANSION_INFO_PATH(
&mid.to_string(),
)?);
}
}
None => {
debug!(
mansion.id.provide = false,
mansion.saveMode = "Create"
);
mongo.bakery().mansion().create(mansion.clone()).await?;
}
}
tc_cloud.purge_urls_cache(&paths).await?;
Ok(())
}

/// 获取最近90天大厦id
pub async fn get_recent_id_by_90(
mongo: MongoDatabaseOperate,
) -> LogicResult<Vec<String>> {
Ok(mongo
.bakery()
.mansion()
.get_mansion_id_list_by_time(Duration::days(90))
.await?)
}

/// 获取所有大厦id
pub async fn get_all_mansion_id(
mongo: MongoDatabaseOperate,
) -> LogicResult<Vec<String>> {
Ok(mongo.bakery().mansion().get_all_mansion_id_list().await?)
}

/// 根据id获取大厦
pub async fn get_mansion(
db: MongoDatabaseOperate, mid: Mid,
) -> LogicResult<ModelMansion> {
Ok(db.bakery().mansion().get_mansion_by_id(&mid.id).await?)
}

/// 根据id删除大厦
pub async fn remove_mansion(
db: MongoDatabaseOperate, tc_cloud: TcCloudManager, mid: Mid,
) -> LogicResult<()> {
db.bakery().mansion().delete(&mid.id).await?;

let paths = [
BakeryTcCdnPath::MANSION_ID_PATH,
BakeryTcCdnPath::RECENT_PREDICT_PATH,
BakeryTcCdnPath::MANSION_INFO_PATH(&mid.id.to_string())?,
];
tc_cloud.purge_urls_cache(&paths).await?;
Ok(())
}
}
120 changes: 117 additions & 3 deletions logic/bakery_logic/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use persistence::{
bakery::models::mansion::preludes::{
Daily, Info, Predict, RecentPredict,
bakery::models::mansion::{
checked::Mansion,
models::ModelMansion,
preludes::{Daily, Info, Predict, RecentPredict},
},
help_crates::chrono::NaiveDate,
help_crates::{bson_date_time_format, chrono::NaiveDate},
};
use serde::{Deserialize, Serialize};
use tencent_cloud_server::cdn::purge_urls_cache::PurgeCachePath;
use typed_builder::TypedBuilder;

#[derive(Debug, Clone, Serialize, Deserialize, TypedBuilder)]
Expand Down Expand Up @@ -61,3 +64,114 @@ impl From<RecentPredict> for MansionRecentPredictResp {
}
}
}

#[derive(Debug, Clone, Serialize, TypedBuilder)]
pub struct MansionResp {
pub id: String,
pub description: String,
#[serde(rename = "cv_link")]
pub cvlink: String,
pub fraction: u8,
pub daily: Vec<ViewDaily>,
}

impl From<Mansion> for MansionResp {
fn from(
Mansion {
id,
link,
description,
fraction,
daily,
}: Mansion,
) -> Self {
Self {
id: id.to_string(),
description,
cvlink: link,
fraction: fraction as u8,
daily: daily.into_iter().map(Into::into).collect(),
}
}
}
impl From<ModelMansion> for MansionResp {
fn from(val: ModelMansion) -> Self {
let ModelMansion {
id,
description,
cvlink,
fraction,
daily,
..
} = val;
MansionResp {
id: id.to_string(),
description,
cvlink,
fraction,
daily: daily.into_iter().map(Into::into).collect(),
}
}
}

#[derive(Debug, Clone, Serialize, TypedBuilder)]
pub struct MansionWithTimeResp {
pub id: String,
pub description: String,
#[serde(rename = "cv_link")]
pub cvlink: String,
pub create_time: String,
pub modify_time: String,
pub fraction: u8,
pub daily: Vec<ViewDaily>,
}

impl From<ModelMansion> for MansionWithTimeResp {
fn from(val: ModelMansion) -> Self {
let ModelMansion {
id,
description,
cvlink,
fraction,
daily,
create_time,
modify_time,
} = val;
MansionWithTimeResp {
id: id.to_string(),
description,
cvlink,
fraction,
daily: daily.into_iter().map(Into::into).collect(),
create_time: bson_date_time_format(create_time),
modify_time: bson_date_time_format(modify_time),
}
}
}

pub(crate) struct BakeryTcCdnPath;

impl BakeryTcCdnPath {
/// 饼学大厦id
pub const MANSION_ID_PATH: PurgeCachePath =
PurgeCachePath::new("/cdn/bakery/mansionId");
/// 最新饼学大厦信息
pub const RECENT_PREDICT_PATH: PurgeCachePath =
PurgeCachePath::new("/cdn/bakery/mansion/recentPredict");

/// 饼学大厦信息
#[allow(non_snake_case)]
pub fn MANSION_INFO_PATH(
mid: &str,
) -> Result<PurgeCachePath, serde_qs::Error> {
#[derive(Serialize)]
struct MansionId<'a> {
mansion_id: &'a str,
}

PurgeCachePath::new_with_query(
"/canteen/bakery/mansionInfo",
&MansionId { mansion_id: mid },
)
}
}
Loading

0 comments on commit 885656a

Please sign in to comment.