Skip to content

Commit

Permalink
feat: create apic-call supports payload up to 50M, configurable via s…
Browse files Browse the repository at this point in the history
…ettings.
  • Loading branch information
stmh committed Nov 2, 2024
1 parent 4861a1b commit aa10be2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ debug: false
api:
bind_address: "0.0.0.0:21342"
access_token: "mysecret"
create_app_max_size: "50M"
scheduler:
running_app_check: "1m"
ttl_check: "10m"
Expand Down
8 changes: 7 additions & 1 deletion src/api/router.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use axum::extract::DefaultBodyLimit;
use axum::middleware;
use axum::routing::get;
use axum::routing::post;
Expand Down Expand Up @@ -98,7 +99,12 @@ impl ApiRoutes {
.route("/api/v1/apps/rebuild/:app_id", get(rebuild_app_handler))
.route("/api/v1/apps/info/:app_id", get(info_app_handler))
.route("/api/v1/apps/destroy/:app_id", get(destroy_app_handler))
.route("/api/v1/apps/create", post(create_app_handler))
.route(
"/api/v1/apps/create",
post(create_app_handler).layer(DefaultBodyLimit::max(
state.settings.api.create_app_max_size,
)),
)
.route("/api/v1/tasks", get(task_list_handler))
.route("/api/v1/task/:uuid", get(task_detail_handler))
.route("/api/v1/validate-token", post(validate_token_handler))
Expand Down
20 changes: 13 additions & 7 deletions src/scottyctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,19 @@ async fn create_app(server: &ServerSettings, cmd: &CreateCommand) -> anyhow::Res
files: file_list,
};

let result = get_or_post(
server,
"apps/create",
"POST",
Some(serde_json::to_value(payload).unwrap()),
)
.await?;
let payload = serde_json::to_value(&payload).context("Failed to serialize payload")?;
let size = match payload.to_string().len() {
bytes if bytes < 1024 => format!("{}b", bytes),
kilobytes if kilobytes < 1024 * 1024 => format!("{:.2}kb", kilobytes as f64 / 1024.0),
megabytes => format!("{:.2}mb", megabytes as f64 / (1024.0 * 1024.0)),
};
println!(
"🚀 Beaming your app {} up to {} ({})... \n",
&cmd.app_name.yellow(),
&server.server.yellow(),
size.blue()
);
let result = get_or_post(server, "apps/create", "POST", Some(payload)).await?;

let context: RunningAppContext =
serde_json::from_value(result).context("Failed to parse context from API")?;
Expand Down
25 changes: 23 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ impl From<SchedulerInterval> for chrono::Duration {
pub struct ApiServer {
pub bind_address: String,
pub access_token: Option<String>,
#[serde(deserialize_with = "deserialize_bytes")]
pub create_app_max_size: usize,
}

fn deserialize_bytes<'de, D>(deserializer: D) -> Result<usize, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let s = s.trim().to_uppercase();

let (num_part, suffix) = s.split_at(s.len().saturating_sub(1));
let multiplier = match suffix {
"G" => 1_024 * 1_024 * 1_024,
"M" => 1_024 * 1_024,
"K" => 1_024,
_ => 1,
};

let num: usize = num_part.parse().map_err(serde::de::Error::custom)?;
Ok(num * multiplier)
}

#[derive(Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -241,6 +262,7 @@ impl Default for Settings {
api: ApiServer {
bind_address: "0.0.0.0:21342".to_string(),
access_token: None,
create_app_max_size: 1024 * 1024 * 10,
},
scheduler: Scheduler {
running_app_check: SchedulerInterval::Minutes(1),
Expand Down Expand Up @@ -282,6 +304,7 @@ impl Settings {

let mut builder = Config::builder()
.set_default("api.bind_address", "0.0.0.0:8080")?
.set_default("api.create_app_max_size", 10 * 1024 * 1024)?
.set_default("apps.max_depth", 3u32)?
.set_default("docker.connection", "local")?
// Start off by merging in the "default" configuration file
Expand Down Expand Up @@ -347,8 +370,6 @@ mod tests {
.add_source(config::File::with_name(
"tests/test_docker_registry_password.yaml",
))
// Add in settings from the environment (with a prefix of APP)
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
.add_source(Settings::get_environment())
.build()
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions tests/test_docker_registry_password.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
debug: false
api:
bind_address: "0.0.0.0:21342"
create_app_max_size: "50M"
scheduler:
running_app_check: "10m"
ttl_check: "10m"
Expand Down

0 comments on commit aa10be2

Please sign in to comment.