Skip to content

Commit

Permalink
feat: auto cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Sep 10, 2023
1 parent 4291412 commit 1162b8a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ pub fn no_cors() -> bool {
Err(_) => false,
}
}

/// Enable auto source cleanup by setting the environment variable "AUTO_CLEANUP" to "1" or "true".
/// If the variable is not set, a default value of false is returned.
/// If enabled, the server will delete the source files after compilation.
pub fn auto_cleanup() -> bool {
match env::var("AUTO_CLEANUP") {
Ok(val) => val == "1" || val == "true",
Err(_) => false,
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use compilers::get_compiler_for_language;
use sha256::digest;
use std::{env::temp_dir, fs, path::PathBuf};

use crate::config::auto_cleanup;

mod cli;
mod compile;
mod compilers;
Expand Down Expand Up @@ -64,7 +66,9 @@ async fn main() {
.compile(code.as_str(), workspace.to_str().unwrap())
.expect("Unable to compile source file.");

fs::remove_dir_all(&workspace).unwrap();
if auto_cleanup() {
fs::remove_dir_all(&workspace).unwrap();
}

fs::write(output, wasm).expect("Unable to write output file.");
eprintln!("Done!");
Expand Down
2 changes: 1 addition & 1 deletion src/server/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn rocket() -> Rocket<Build> {
let server = server.attach(version::fairing());

let server = if !no_cors() {
server.attach(cors::CORS)
server.attach(cors::Cors)
} else {
server
};
Expand Down
4 changes: 2 additions & 2 deletions src/server/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::Header;
use rocket::{Request, Response};

pub struct CORS;
pub struct Cors;

#[rocket::async_trait]
impl Fairing for CORS {
impl Fairing for Cors {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
Expand Down
6 changes: 4 additions & 2 deletions src/server/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::jwt::Token;
use super::system::{Status, SystemInfo};
use crate::compile::{CodeSubmission, CompileResult, WasmCache};
use crate::compilers::{get_compiler_for_language, get_compilers};
use crate::config::no_cache;
use crate::config::{auto_cleanup, no_cache};
use base64::engine::general_purpose;
use base64::Engine;
use rocket::serde::json::Json;
Expand Down Expand Up @@ -143,7 +143,9 @@ pub fn compile(

let wasm = compiler.compile(code, workspace.to_str().unwrap());

fs::remove_dir_all(&workspace).unwrap();
if auto_cleanup() {
fs::remove_dir_all(&workspace).unwrap();
}

// Write the compiled wasm to the cache
match wasm {
Expand Down

0 comments on commit 1162b8a

Please sign in to comment.