Skip to content

Commit 37bb8c0

Browse files
committed
feat: add new modules for service and openapi
1 parent af66601 commit 37bb8c0

File tree

13 files changed

+377
-13
lines changed

13 files changed

+377
-13
lines changed

Cargo.lock

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ members = [
1414
"crates/jz-migration",
1515
"crates/jz-model",
1616
"crates/jz-module",
17-
"crates/jz-openapi",
17+
"crates/jz-openapi", "crates/jz-service",
1818
"crates/jz-smart",
1919
"crates/jz-ssh",
2020
"crates/jz-stream",

crates/jz-api/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ workspace = true
4646
workspace = true
4747

4848
[dependencies.jz-smart]
49+
workspace = true
50+
51+
[dependencies.jz-openapi]
4952
workspace = true

crates/jz-api/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl Api {
5959
.route("/", actix_web::web::get().to(|| async { "Hello World!" }))
6060
.service(scope("/api").configure(v1::v1_route))
6161
.service(scope("/git").configure(jz_smart::git_router))
62+
.service(scope("/openapi").configure(jz_openapi::openapi_router))
6263
})
6364
.bind(format!("0.0.0.0:{}", PROTS.to_string()))
6465
.context("Failed to apply actix settings")?

crates/jz-module/src/repo/list.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1+
use sea_orm::*;
2+
use serde::{Deserialize, Serialize};
13
use serde_json::{json, Value};
24
use uuid::Uuid;
5+
use jz_model::repository;
36
use crate::AppModule;
47

8+
#[derive(Deserialize,Serialize)]
9+
pub struct RepositoryListParam {
10+
pub r#type: String,
11+
pub limit: Option<i64>,
12+
pub offset: Option<i64>,
13+
}
14+
#[derive(Deserialize,Serialize)]
15+
pub struct RepositoryListResult {
16+
creator: String, // owner name
17+
description: Option<String>,
18+
id: Uuid,
19+
image: Option<String>,
20+
runs: Option<i32>,
21+
size: Option<String>,
22+
r#type: String,
23+
}
24+
525
impl AppModule {
626
pub async fn repo_list_info(&self,ops_uid: Option<Uuid>, owner_name: String) -> anyhow::Result<Vec<Value>> {
727
let repos = self.repo_info_by_owner(owner_name).await?;
@@ -35,4 +55,57 @@ impl AppModule {
3555
}
3656
Ok(result)
3757
}
58+
pub async fn repo_list(&self, parma: RepositoryListParam) -> anyhow::Result<serde_json::Value> {
59+
let rtype = if parma.r#type == "all" {
60+
None
61+
} else {
62+
let rf = parma.r#type.to_lowercase();
63+
if rf == "code" {
64+
Some("Code".to_string())
65+
}else if rf == "data" {
66+
Some("Data".to_string())
67+
}else if rf == "model" {
68+
Some("Model".to_string())
69+
}else {
70+
return Err(anyhow::anyhow!("invalid rtype"));
71+
}
72+
};
73+
let mut condition = Condition::all()
74+
.add(repository::Column::IsPrivate.eq(false));
75+
if let Some(ref rtype) = rtype {
76+
condition = condition.add(repository::Column::Rtype.eq(rtype));
77+
};
78+
let repos = repository::Entity::find()
79+
.filter(condition)
80+
.order_by_desc(repository::Column::CreatedAt)
81+
.limit(parma.limit.unwrap_or(20) as u64)
82+
.offset(parma.offset.unwrap_or(0) as u64)
83+
.all(&self.read)
84+
.await?;
85+
let mut result: Vec<RepositoryListResult> = Vec::new();
86+
for i in repos {
87+
result.push(RepositoryListResult {
88+
creator: i.owner_name,
89+
description: i.description,
90+
id: i.uid,
91+
image: None,
92+
runs: None,
93+
size: None,
94+
r#type: i.rtype,
95+
});
96+
}
97+
let total = repository::Entity::find()
98+
.filter(repository::Column::Rtype.eq(parma.r#type))
99+
.filter(repository::Column::IsPrivate.eq(false))
100+
.count(&self.read)
101+
.await?;
102+
Ok(json!({
103+
"data": result,
104+
"limit": parma.limit,
105+
"offset": parma.offset,
106+
"total": total,
107+
"type": rtype,
108+
}))
109+
110+
}
38111
}

crates/jz-module/src/users/token.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,12 @@ impl AppModule {
132132
.await
133133
.map_err(|e| anyhow::anyhow!("{}", e))?)
134134
}
135+
pub async fn token_find(&self, token: String) -> anyhow::Result<token::Model> {
136+
token::Entity::find()
137+
.filter(token::Column::Token.eq(token))
138+
.one(&self.read)
139+
.await
140+
.map_err(|e| anyhow::anyhow!("{}", e))?
141+
.ok_or(anyhow::anyhow!("token not found"))
142+
}
135143
}

crates/jz-openapi/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,38 @@ description.workspace = true
1212
homepage.workspace = true
1313

1414
[dependencies]
15+
actix-web = { version = "4", features = ["cookies"] }
16+
actix-session = { version = "0.10.1", features = ["redis-pool","redis-session"] }
17+
actix-files = { version = "0.6.6", features = ["tokio-uring"] }
18+
actix-multipart = { version = "0.7.2", features = ["derive"] }
19+
actix-web-lab = { version = "0.24.1", features = ["derive"] }
20+
actix-settings = { version = "0.8.0", features = [] }
21+
actix-web-httpauth = { version = "0.8.2", features = [] }
22+
23+
tokio = { version = "1", features = ["full"] }
24+
25+
serde = { version = "1", features = ["derive"] }
26+
serde_json = { version = "1", features = ["raw_value"] }
27+
uuid = { version = "1"}
28+
chrono = { version = "0.4"}
29+
30+
anyhow = { version = "1", features = ["backtrace"] }
31+
32+
captcha-rs = { version = "0.2.11", features = [] }
33+
log = "0.4.26"
34+
lazy_static = "1.5.0"
35+
sha256 = { version = "1.6.0", features = [] }
36+
[dependencies.jz-module]
37+
workspace = true
38+
39+
[dependencies.jz-model]
40+
workspace = true
41+
42+
[dependencies.jz-git]
43+
workspace = true
44+
45+
[dependencies.jz-dragonfly]
46+
workspace = true
47+
48+
[dependencies.jz-smart]
49+
workspace = true

crates/jz-openapi/src/bare.rs

Whitespace-only changes.

crates/jz-openapi/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
pub fn add(left: u64, right: u64) -> u64 {
2-
left + right
3-
}
1+
use actix_web::web::{get, scope};
2+
use crate::repo::repo_list;
43

5-
#[cfg(test)]
6-
mod tests {
7-
use super::*;
4+
pub mod repo;
5+
pub mod bare;
86

9-
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
13-
}
14-
}
7+
pub fn openapi_router(cfg: &mut actix_web::web::ServiceConfig) {
8+
cfg
9+
.service(
10+
scope("/repo")
11+
.route("",get().to(repo_list))
12+
)
13+
;
14+
}

crates/jz-openapi/src/repo.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use actix_web::web::Data;
2+
use actix_web_httpauth::extractors::bearer::BearerAuth;
3+
use jz_module::AppModule;
4+
use jz_module::repo::list::RepositoryListParam;
5+
6+
pub async fn repo_list(
7+
service: Data<AppModule>,
8+
query: actix_web::web::Query<RepositoryListParam>,
9+
credentials: Option<BearerAuth>,
10+
)
11+
-> actix_web::Result<actix_web::HttpResponse>
12+
{
13+
let Some(credentials) = credentials else {
14+
return Ok(actix_web::HttpResponse::Unauthorized().body("token invalid"))
15+
};
16+
let Ok(token) = service.token_find(credentials.token().to_string()).await else {
17+
return Ok(actix_web::HttpResponse::Unauthorized().body("token invalid"))
18+
};
19+
let _access = token.access;
20+
let result = service.repo_list(query.into_inner()).await;
21+
match result {
22+
Ok(data) => {
23+
Ok(actix_web::HttpResponse::Ok().json(data))
24+
}
25+
Err(err) => {
26+
Ok(actix_web::HttpResponse::InternalServerError().body(err.to_string()))
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)