Skip to content

Commit 66f5a39

Browse files
welkeyeverAsterDY
authored andcommitted
feat: add a http route to connect rust and go plugin (cloudwego#26)
1 parent e9dad7b commit 66f5a39

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

src/compress/golang/plugin/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module abcoder/go_ast
2+
3+
go 1.20
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1
7+
golang.org/x/tools v0.17.0
8+
)
9+
10+
require golang.org/x/mod v0.14.0 // indirect

src/compress/golang/plugin/go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
4+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
5+
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
6+
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
7+
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=

src/compress/golang/plugin/go_ast.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ func main() {
271271
os.Exit(1)
272272
}
273273

274-
// p.generateStruct()
275-
m, _ := p.getMain(-1)
274+
//p.generateStruct()
275+
//m, _ := p.getMain(-1)
276276

277277
out := bytes.NewBuffer(nil)
278278
encoder := json.NewEncoder(out)
279279
encoder.SetEscapeHTML(false)
280-
err := encoder.Encode(m)
280+
err := encoder.Encode(p.repo)
281281
if err != nil {
282282
fmt.Println("Error marshalling functions to JSON:", err)
283283
os.Exit(1)

src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::arch::asm;
2+
use std::cell::RefCell;
23
use std::net::SocketAddr;
34
// Import necessary types from the standard library
45
use std::path::{Path, PathBuf};
56
use std::ptr::addr_of_mut;
6-
use std::sync::Arc;
7+
use std::rc::Rc;
8+
use std::sync::{Arc, Mutex};
79

810
use futures::future::BoxFuture;
911
use futures::FutureExt;
@@ -17,6 +19,7 @@ use tokio::runtime::Runtime;
1719
// use crate::llm::llm;
1820
use utils::llm::count_tokens_rough;
1921

22+
use crate::compress::compress::Identity;
2023
use crate::compress::golang;
2124
use crate::compress::parser::LanguageParser;
2225
use crate::utils::cmd;
@@ -130,6 +133,27 @@ fn code_analyze(ctx: &mut RequestContext) -> BoxFuture<'_, ()> {
130133
}).boxed()
131134
}
132135

136+
// repo_compress handler
137+
fn repo_compress(ctx: &mut RequestContext) -> BoxFuture<'_, ()> {
138+
let parsed: std::collections::HashMap<String, String> = serde_urlencoded::from_str(ctx.req.uri().query().unwrap()).unwrap();
139+
let repo = parsed.get("repo").unwrap().clone();
140+
(async move {
141+
let repo_dir = check_repo_exist(&repo);
142+
143+
if let Ok(output) = cmd::run_command("./go_ast", vec![repo_dir.as_str()]) {
144+
let mut repo = compress::compress::from_json(output.as_str()).unwrap();
145+
compress::compress::compress_all(&mut repo).await;
146+
let compress = serde_json::to_string(&repo).unwrap();
147+
println!("compressed repo:\n{}", compress);
148+
149+
*ctx.resp.body_mut() = Body::from(compress);
150+
return;
151+
}
152+
153+
*ctx.resp.body_mut() = Body::from("analyze failed.");
154+
}).boxed()
155+
}
156+
133157
// issue_trace handler
134158
fn issue_trace(ctx: &mut RequestContext) -> BoxFuture<'_, ()> {
135159
let parsed: std::collections::HashMap<String, String> = serde_urlencoded::from_str(ctx.req.uri().query().unwrap()).unwrap();
@@ -182,6 +206,8 @@ async fn main() {
182206
h.get("/repo_structure", Arc::new(tree_structure)).await;
183207
h.get("/code_analyze", Arc::new(code_analyze)).await;
184208

209+
h.get("/repo_compress", Arc::new(repo_compress)).await;
210+
185211

186212
h.spin(SocketAddr::from(([0, 0, 0, 0, 0, 0, 0, 0], 8888))).await.expect("TODO: panic message");
187213

0 commit comments

Comments
 (0)