1
Fork 0

API works now

This commit is contained in:
Jan-Erik Rediger 2024-05-24 23:52:52 +02:00
parent c5323cd2e2
commit 063295696b
3 changed files with 33 additions and 34 deletions

12
backend/request.hurl Normal file
View file

@ -0,0 +1,12 @@
POST http://localhost:8080/v1/exec
content-type: application/json
origin: https://codapi.org
```
{
"sandbox": "hare",
"command": "run",
"files": {
"": "use fmt;\nexport fn main() void = {\nfmt::println(\"hello world\")!;\n};\n"
}
}
```

View file

@ -1,7 +1,6 @@
mod sandbox;
/*
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Instant;
use axum::{ use axum::{
extract::MatchedPath, extract::MatchedPath,
http::{Request, StatusCode}, http::{Request, StatusCode},
@ -13,6 +12,8 @@ use tower_http::trace::TraceLayer;
use tracing::info_span; use tracing::info_span;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod sandbox;
#[derive(Default, Debug, Clone, PartialEq, Deserialize)] #[derive(Default, Debug, Clone, PartialEq, Deserialize)]
pub struct ExecRequest { pub struct ExecRequest {
pub sandbox: String, pub sandbox: String,
@ -24,18 +25,28 @@ pub struct ExecRequest {
pub struct ExecResponse { pub struct ExecResponse {
pub id: String, pub id: String,
pub ok: bool, pub ok: bool,
pub duration: i64, pub duration: u64,
pub stdout: String, pub stdout: String,
pub stderr: String, pub stderr: String,
} }
async fn exec(Json(req): Json<ExecRequest>) -> (StatusCode, Json<ExecResponse>) { async fn exec(Json(req): Json<ExecRequest>) -> (StatusCode, Json<ExecResponse>) {
let code = req.files.get("").unwrap();
let now = Instant::now();
let output = sandbox::run_code(code);
let duration = now.elapsed().as_millis() as u64;
let ok = output.status.success();
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
let resp = ExecResponse { let resp = ExecResponse {
id: "random".to_string(), id: "random".to_string(),
ok: true, ok,
duration: 10, duration,
stdout: "none".to_string(), stdout,
stderr: "".to_string(), stderr,
}; };
(StatusCode::OK, Json(resp)) (StatusCode::OK, Json(resp))
@ -74,23 +85,3 @@ async fn main() {
tracing::debug!("listening on {}", listener.local_addr().unwrap()); tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
*/
fn main() {
sandbox::run_code(r#"
use fmt;
export fn main() void = {
const greetings = [
"Hello, world!",
"¡Hola Mundo!",
"Γειά σου Κόσμε!",
"Привіт, світ!",
"こんにちは世界!",
];
for (let i = 0z; i < len(greetings); i+= 1) {
fmt::println(greetings[i])!;
};
};
"#);
}

View file

@ -106,7 +106,7 @@ fn getids() -> (u32, u32) {
unsafe { (libc::getuid(), libc::getgid()) } unsafe { (libc::getuid(), libc::getgid()) }
} }
pub fn run_code(code: &str) { pub fn run_code(code: &str) -> std::process::Output {
let (uid, gid) = getids(); let (uid, gid) = getids();
let mut bwrap_cmd = Command::new("bwrap"); let mut bwrap_cmd = Command::new("bwrap");
@ -131,9 +131,5 @@ pub fn run_code(code: &str) {
bwrap_cmd.args(cmd_args); bwrap_cmd.args(cmd_args);
let output = bwrap_cmd.output().expect("running bwrap failed"); let output = bwrap_cmd.output().expect("running bwrap failed");
let stdout = String::from_utf8_lossy(&output.stdout); output
let stderr = String::from_utf8_lossy(&output.stderr);
println!("stdout:\n{stdout}");
println!("stderr:\n{stderr}");
} }