update axum to 0.7

This commit is contained in:
quantulr
2024-03-21 17:09:21 +08:00
parent 44dd26198f
commit 3e35e895b7
10 changed files with 498 additions and 423 deletions

5
.env
View File

@ -1,2 +1,3 @@
DATABASE_URL=mysql://root:archlinux0311@localhost:3306/likeadmin DATABASE_URL=mysql://cas:Root123456789.@localhost:3306/caszl
UPLOAD_PATH="/Volumes/iMac Doc/likeadmin-java" UPLOAD_PATH=D:/Documents/中科云旧服务器备份/www/www/uploads/caszl
PORT=3000

861
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,13 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
axum = "0.6.20" axum = "0.7.4"
chrono = "0.4.31" chrono = "0.4.35"
dotenvy = "0.15.7" dotenvy = "0.15.7"
hyper = { version = "0.14.27", features = ["full"] } hyper = { version = "1.2.0", features = ["full"] }
sea-orm = { version = "0.12.4", features = ["sqlx-mysql", "runtime-tokio-native-tls", "macros"] } sea-orm = { version = "0.12.15", features = ["sqlx-mysql", "runtime-tokio-native-tls", "macros"] }
serde = { version = "1.0.192", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.108" serde_json = "1.0.114"
tokio = { version = "1.34.0", features = ["full"] } tokio = { version = "1.36.0", features = ["full"] }
tower = "0.4.13" tower = "0.4.13"
tower-http = { version = "0.4.4", features = ["fs"] } tower-http = { version = "0.5.2", features = ["fs"] }

View File

@ -1,17 +1,17 @@
use crate::entity::la_article;
use crate::params::article::{ArticleDetailParams, ArticleListParams}; use crate::params::article::{ArticleDetailParams, ArticleListParams};
use crate::state::app::AppState; use crate::state::app::AppState;
use axum::extract::{Query, State}; use axum::extract::{Query, State};
use axum::Json; use axum::Json;
use chrono::{TimeZone, Utc};
use sea_orm::{ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder, QueryTrait};
use serde_json::{json, Value};
use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
use crate::response::article::{ArticleDetail, ArticleListItem}; use crate::response::article::{ArticleDetail, ArticleListItem};
use crate::response::common::{BaseResponse, PageResponse}; use crate::response::common::{BaseResponse, PageResponse};
use crate::service::artile::ArticleService; use crate::service::article::ArticleService;
pub async fn article_list( pub async fn article_list(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,

View File

@ -9,18 +9,20 @@ mod service;
mod state; mod state;
use crate::state::app::AppState; use crate::state::app::AppState;
use axum::{Server, ServiceExt};
use sea_orm::Database; use sea_orm::Database;
use std::env; use std::env;
use std::sync::Arc; use std::sync::Arc;
use tokio::net::TcpListener;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
dotenvy::dotenv().ok(); dotenvy::dotenv().ok();
let db_url = env::var("DATABASE_URL").expect(".env 文件中没有设置 DATABASE_URL"); let db_url = env::var("DATABASE_URL").expect(".env 文件中没有设置 DATABASE_URL");
let upload_path = env::var("UPLOAD_PATH").expect(".env 文件中没有设置 UPLOAD_PATH"); let upload_path = env::var("UPLOAD_PATH").expect(".env 文件中没有设置 UPLOAD_PATH");
let port = env::var("PORT").expect(".env 文件中没有设置 PORT");
let db_conn = Database::connect(&db_url).await.expect("数据库链接失败"); let db_conn = Database::connect(&db_url).await.expect("连接数据库失败");
let app_state = AppState { let app_state = AppState {
db_conn, db_conn,
@ -28,8 +30,8 @@ async fn main() {
}; };
let app = routes::create_routes(Arc::new(app_state)); let app = routes::create_routes(Arc::new(app_state));
Server::bind(&"0.0.0.0:3000".parse().unwrap()) let listener = TcpListener::bind(format!("0.0.0.0:{}", port))
.serve(app)
.await .await
.unwrap(); .unwrap();
axum::serve(listener, app).await.unwrap();
} }

View File

@ -1,4 +1,4 @@
use crate::entity::la_article;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]

View File

@ -1,4 +1,4 @@
use axum::http::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]

View File

@ -1 +1 @@
pub mod artile; pub mod article;

View File

@ -32,17 +32,14 @@ impl ArticleService {
let articles_selection = la_article::Entity::find() let articles_selection = la_article::Entity::find()
.filter(la_article::Column::IsDelete.ne(1)) .filter(la_article::Column::IsDelete.ne(1))
.filter(la_article::Column::IsShow.ne(0)) .filter(la_article::Column::IsShow.ne(0))
.apply_if(Some(params.cid), |mut query, v| match v { .apply_if(Some(params.cid), |query, v| match v {
Some(cid) => query.filter(la_article::Column::Cid.eq(cid)), Some(cid) => query.filter(la_article::Column::Cid.eq(cid)),
None => query, None => query,
}); });
let count = match articles_selection.clone().count(&self.state.db_conn).await { let count = articles_selection.clone().count(&self.state.db_conn).await.unwrap_or_else(|_| 0);
Ok(total) => total,
Err(_) => 0,
};
let articles_result = articles_selection let articles_result = articles_selection
.apply_if(Some(params.sort), |mut query, v| { .apply_if(Some(params.sort), |query, v| {
match v { match v {
Some(sort_str) => { Some(sort_str) => {
// 最热 // 最热

View File

@ -1,4 +1,4 @@
use crate::service::artile::ArticleService; use crate::service::article::ArticleService;
pub struct ArticleState { pub struct ArticleState {
pub article_service: ArticleService, pub article_service: ArticleService,