Skip to content

Commit

Permalink
Fixed build mode to work in docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Szeliga authored and szagi3891 committed Apr 26, 2023
1 parent 31057d3 commit 2d23e3d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
35 changes: 13 additions & 22 deletions crates/vertigo-cli/src/build/cargo_build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use cargo::core::compiler::{CompileTarget, CompileKind, MessageFormat};
use cargo::{ops, Config};
use cargo::util::command_prelude::CompileMode;
use cargo::{Config};
use std::path::PathBuf;

use crate::command::CommandRun;

use super::cargo_workspace::get_workspace;

const TARGET: &str = "wasm32-unknown-unknown";
Expand All @@ -11,7 +11,15 @@ const MODE: &str = "release";
pub fn run_cargo_build(package_name: &str, public_path: &str) -> Result<PathBuf, String> {
log::info!("Building {package_name}");

std::env::set_var("VERTIGO_PUBLIC_PATH", public_path);
CommandRun::new("cargo")
.add_param("build")
.add_param("--release")
.add_param("--target")
.add_param("wasm32-unknown-unknown")
.add_param("--package")
.add_param(package_name)
.env("VERTIGO_PUBLIC_PATH", public_path)
.run();

let mut config_opt = Config::default();
let workspace = match get_workspace(&mut config_opt) {
Expand All @@ -23,22 +31,5 @@ pub fn run_cargo_build(package_name: &str, public_path: &str) -> Result<PathBuf,
}
};

let mut options = ops::CompileOptions::new(workspace.config(), CompileMode::Build).unwrap();
options.spec = ops::Packages::from_flags(false, vec![], vec![package_name.to_string()]).unwrap();
let target = CompileTarget::new(TARGET).unwrap();
options.build_config.requested_kinds = vec![CompileKind::Target(target)];
options.build_config.requested_profile = MODE.into();
options.build_config.message_format = MessageFormat::Human;
options.build_config.keep_going = true;

match ops::compile(&workspace, &options) {
Ok(_success) => {
log::info!("WASM built successfully");
Ok(workspace.target_dir().join(TARGET).join(MODE).into_path_unlocked())
},
Err(err) => {
log::error!("WASM build failed: {err}");
Err(err.to_string())
}
}
Ok(workspace.target_dir().join(TARGET).join(MODE).into_path_unlocked())
}
63 changes: 60 additions & 3 deletions crates/vertigo-cli/src/serve/serve_command.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use axum::{
extract::{State, RawQuery},
extract::{State, RawQuery, Json},
http::{StatusCode, Uri},
http::{header::HeaderMap},
response::Response,
Router, body::BoxBody, routing::get,
Router, body::BoxBody, routing::{get},
};
use clap::Args;
use serde_json::Value;
use std::{time::{Instant, Duration}, sync::Arc};
use tokio::sync::{OnceCell, RwLock};
use tower_http::services::ServeDir;
Expand Down Expand Up @@ -119,6 +121,47 @@ async fn get_response(target_url: String) -> Response<BoxBody> {
response
}

async fn post_response(target_url: String, headers: HeaderMap, body: Value) -> Response<BoxBody> {
let client = reqwest::Client::new();
let body = serde_json::to_vec(&body).unwrap();
let response = match client.post(target_url.clone())
.headers(headers)
.body(body).send().await {
Ok(response) => response,
Err(error) => {
let message = format!("Error fetching from url={target_url} error={error}");

let mut response = message.into_response();
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;

return response;
}
};

let headers = response.headers().clone();
let status = response.status();
let body = match response.bytes().await {
Ok(body) => body.to_vec(),
Err(error) => {
let message = format!("Error fetching body from url={target_url} error={error}");

let mut response = message.into_response();
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;

return response;

}
};

use axum::response::IntoResponse;
let mut response: Response<BoxBody> = body.into_response();

*response.headers_mut() = headers;
*response.status_mut() = status;

response
}

fn install_proxy(
app: Router<Arc<RwLock<Arc<ServerState>>>>,
path: String,
Expand All @@ -127,16 +170,30 @@ fn install_proxy(
) -> Router<Arc<RwLock<Arc<ServerState>>>> {
let router = Router::new().fallback(get ({
let path = path.clone();
let target = target.clone();

move |url: Uri| {
async move {
let from_url = format!("{path}{url}");
let target_url = format!("{target}{url}");
log::info!("proxy {from_url} -> {target_url}");
log::info!("proxy get {from_url} -> {target_url}");

get_response(target_url).await
}
}
}).post({
let path = path.clone();

move |url: Uri, headers: HeaderMap, body: Json<Value>| {
async move {
let from_url = format!("{path}{url}");
let target_url = format!("{target}{url}");
let Json(body) = body;
log::info!("proxy post {from_url} -> {target_url}");

post_response(target_url, headers, body).await
}
}
})).with_state(ref_state.clone());

app.nest_service(path.as_str(), router)
Expand Down

0 comments on commit 2d23e3d

Please sign in to comment.