Skip to content

Commit

Permalink
feat: use project config for deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
christos-h authored Feb 25, 2022
1 parent 41ed5c7 commit 6258661
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ async fn get_deployment(state: &State<ApiState>, id: Uuid) -> Result<Value, Depl
}

#[post("/deployments", data = "<crate_file>")]
async fn create_deployment(state: &State<ApiState>, crate_file: Data<'_>) -> Result<Value, DeploymentError> {
let project = ProjectConfig {
name: "some_project".to_string()
};

let deployment = state.deployment_manager.deploy(crate_file, &project).await?;
async fn create_deployment(state: &State<ApiState>, crate_file: Data<'_>, config: ProjectConfig) -> Result<Value, DeploymentError> {
let deployment = state.deployment_manager
.deploy(crate_file, &config)
.await?;

Ok(json!(deployment))
}
Expand Down
3 changes: 2 additions & 1 deletion cargo-unveil/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use lib::{DeploymentMeta, DeploymentStateMeta, ProjectConfig};
use lib::{DeploymentMeta, DeploymentStateMeta, ProjectConfig, UNVEIL_PROJECT_HEADER};
use std::{fs::File, thread::sleep, time::Duration};

pub(crate) type ApiKey = String;
Expand All @@ -13,6 +13,7 @@ pub(crate) fn deploy(package_file: File, api_key: ApiKey, project: ProjectConfig
let mut res: DeploymentMeta = client
.post(url.clone())
.body(package_file)
.header(UNVEIL_PROJECT_HEADER, serde_json::to_string(&project)?)
.basic_auth(api_key.clone(), Some(""))
.send()
.context("failed to send deployment to the Unveil server")?
Expand Down
3 changes: 3 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ publish = false

[dependencies]
serde = "1.0.136"
serde_json = "1.0.79"
uuid = { version = "0.8.2", features = ["v4", "serde"] }
rocket = "0.5.0-rc.1"
anyhow = "1.0.55"
29 changes: 29 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use rocket::http::Status;
use rocket::Request;
use rocket::request::{FromRequest, Outcome};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

pub const UNVEIL_PROJECT_HEADER: &'static str = "Unveil-Project";

pub type DeploymentId = Uuid;

/// Deployment metadata. This serves two purposes. Storing information
Expand All @@ -16,6 +21,7 @@ pub struct DeploymentMeta {
pub runtime_logs: Option<String>,
}


impl DeploymentMeta {
pub fn new(config: &ProjectConfig) -> Self {
Self {
Expand Down Expand Up @@ -47,3 +53,26 @@ pub enum DeploymentStateMeta {
pub struct ProjectConfig {
pub name: String,
}

#[derive(Debug)]
pub enum ProjectConfigError {
Missing,
Malformed,
}

#[rocket::async_trait]
impl<'r> FromRequest<'r> for ProjectConfig {
type Error = ProjectConfigError;

async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let config_string = match req.headers().get_one(UNVEIL_PROJECT_HEADER) {
None => return Outcome::Failure((Status::BadRequest, ProjectConfigError::Missing)),
Some(config_string) => config_string
};

match serde_json::from_str::<ProjectConfig>(config_string) {
Ok(config) => Outcome::Success(config),
Err(_) => Outcome::Failure((Status::BadRequest, ProjectConfigError::Malformed))
}
}
}

0 comments on commit 6258661

Please sign in to comment.