feat: auto assign default deployment hostnames using crate::subdomains

utils
This commit is contained in:
Aravinth Manivannan 2022-11-10 17:35:20 +05:30
parent 30be3a293d
commit ed68b4570c
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 17 additions and 25 deletions

View file

@ -22,6 +22,8 @@ use crate::ctx::Ctx;
use crate::db::Site;
use crate::errors::*;
use crate::page::Page;
use crate::settings::Settings;
use crate::subdomains::get_random_subdomain;
use crate::utils::get_random;
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
@ -29,30 +31,30 @@ use crate::utils::get_random;
pub struct AddSite {
pub repo_url: String,
pub branch: String,
pub hostname: String,
pub owner: String,
}
impl AddSite {
fn to_site(self) -> Site {
fn to_site(self, s: &Settings) -> Site {
let site_secret = get_random(32);
let hostname = get_random_subdomain(s);
Site {
site_secret,
repo_url: self.repo_url,
branch: self.branch,
hostname: self.hostname,
hostname,
owner: self.owner,
}
}
}
impl Ctx {
pub async fn add_site(&self, site: AddSite) -> ServiceResult<()> {
let db_site = site.to_site();
pub async fn add_site(&self, site: AddSite) -> ServiceResult<Page> {
let db_site = site.to_site(&self.settings);
self.db.add_site(&db_site).await?;
let page = Page::from_site(&self.settings, db_site);
page.update(&page.branch)?;
Ok(())
Ok(page)
}
pub async fn update_site(&self, secret: &str, branch: Option<String>) -> ServiceResult<()> {

View file

@ -117,14 +117,11 @@ mod tests {
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let hostname = ctx.get_test_hostname(NAME);
ctx.add_test_site(NAME.into(), hostname.clone()).await;
let page = ctx.add_test_site(NAME.into()).await;
let app = get_app!(ctx).await;
let page = ctx.db.get_site(NAME, &hostname).await.unwrap();
let mut payload = DeployEvent {
secret: page.site_secret.clone(),
secret: page.secret.clone(),
branch: page.branch.clone(),
};
@ -154,13 +151,11 @@ mod tests {
let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let hostname = ctx.get_test_hostname(NAME);
ctx.add_test_site(NAME.into(), hostname.clone()).await;
let page = ctx.add_test_site(NAME.into()).await;
let app = get_app!(ctx).await;
let page = ctx.db.get_site(NAME, &hostname).await.unwrap();
let mut payload = DeploySecret {
secret: page.site_secret.clone(),
secret: page.secret.clone(),
};
let resp = test::call_service(
@ -173,7 +168,7 @@ mod tests {
let response: DeployInfo = actix_web::test::read_body_json(resp).await;
assert_eq!(response.head, page.branch);
assert_eq!(response.remote, page.repo_url);
assert_eq!(response.remote, page.repo);
payload.secret = page.branch.clone();

View file

@ -25,7 +25,7 @@ pub struct Preview<'a> {
impl<'a> Preview<'a> {
pub fn new(ctx: &'a AppCtx) -> Self {
Self {
base: &ctx.settings.server.domain,
base: &ctx.settings.page.base_domain,
delimiter: ".",
prefix: "deploy-preview-",
}

View file

@ -29,10 +29,10 @@ use crate::ctx::api::v1::auth::{Login, Register};
use crate::ctx::api::v1::pages::AddSite;
use crate::ctx::Ctx;
use crate::errors::*;
use crate::page::Page;
use crate::settings::Settings;
use crate::*;
const HOSTNAME: &str = "example.org";
pub const REPO_URL: &str = "https://github.com/mCaptcha/website/";
pub const BRANCH: &str = "gh-pages";
@ -268,17 +268,12 @@ impl Ctx {
assert_eq!(resp_err.error, format!("{}", err));
}
pub async fn add_test_site(&self, owner: String, hostname: String) {
pub async fn add_test_site(&self, owner: String) -> Page {
let msg = AddSite {
repo_url: REPO_URL.into(),
branch: BRANCH.into(),
hostname,
owner,
};
self.add_site(msg).await.unwrap();
}
pub fn get_test_hostname(&self, unique: &str) -> String {
format!("{unique}.{HOSTNAME}")
self.add_site(msg).await.unwrap()
}
}