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::db::Site;
use crate::errors::*; use crate::errors::*;
use crate::page::Page; use crate::page::Page;
use crate::settings::Settings;
use crate::subdomains::get_random_subdomain;
use crate::utils::get_random; use crate::utils::get_random;
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] #[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
@ -29,30 +31,30 @@ use crate::utils::get_random;
pub struct AddSite { pub struct AddSite {
pub repo_url: String, pub repo_url: String,
pub branch: String, pub branch: String,
pub hostname: String,
pub owner: String, pub owner: String,
} }
impl AddSite { impl AddSite {
fn to_site(self) -> Site { fn to_site(self, s: &Settings) -> Site {
let site_secret = get_random(32); let site_secret = get_random(32);
let hostname = get_random_subdomain(s);
Site { Site {
site_secret, site_secret,
repo_url: self.repo_url, repo_url: self.repo_url,
branch: self.branch, branch: self.branch,
hostname: self.hostname, hostname,
owner: self.owner, owner: self.owner,
} }
} }
} }
impl Ctx { impl Ctx {
pub async fn add_site(&self, site: AddSite) -> ServiceResult<()> { pub async fn add_site(&self, site: AddSite) -> ServiceResult<Page> {
let db_site = site.to_site(); let db_site = site.to_site(&self.settings);
self.db.add_site(&db_site).await?; self.db.add_site(&db_site).await?;
let page = Page::from_site(&self.settings, db_site); let page = Page::from_site(&self.settings, db_site);
page.update(&page.branch)?; page.update(&page.branch)?;
Ok(()) Ok(page)
} }
pub async fn update_site(&self, secret: &str, branch: Option<String>) -> ServiceResult<()> { 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 (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await; let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await; let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let hostname = ctx.get_test_hostname(NAME); let page = ctx.add_test_site(NAME.into()).await;
ctx.add_test_site(NAME.into(), hostname.clone()).await;
let app = get_app!(ctx).await; let app = get_app!(ctx).await;
let page = ctx.db.get_site(NAME, &hostname).await.unwrap();
let mut payload = DeployEvent { let mut payload = DeployEvent {
secret: page.site_secret.clone(), secret: page.secret.clone(),
branch: page.branch.clone(), branch: page.branch.clone(),
}; };
@ -154,13 +151,11 @@ mod tests {
let (_dir, ctx) = tests::get_ctx().await; let (_dir, ctx) = tests::get_ctx().await;
let _ = ctx.delete_user(NAME, PASSWORD).await; let _ = ctx.delete_user(NAME, PASSWORD).await;
let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await; let (_, _signin_resp) = ctx.register_and_signin(NAME, EMAIL, PASSWORD).await;
let hostname = ctx.get_test_hostname(NAME); let page = ctx.add_test_site(NAME.into()).await;
ctx.add_test_site(NAME.into(), hostname.clone()).await;
let app = get_app!(ctx).await; let app = get_app!(ctx).await;
let page = ctx.db.get_site(NAME, &hostname).await.unwrap();
let mut payload = DeploySecret { let mut payload = DeploySecret {
secret: page.site_secret.clone(), secret: page.secret.clone(),
}; };
let resp = test::call_service( let resp = test::call_service(
@ -173,7 +168,7 @@ mod tests {
let response: DeployInfo = actix_web::test::read_body_json(resp).await; let response: DeployInfo = actix_web::test::read_body_json(resp).await;
assert_eq!(response.head, page.branch); assert_eq!(response.head, page.branch);
assert_eq!(response.remote, page.repo_url); assert_eq!(response.remote, page.repo);
payload.secret = page.branch.clone(); payload.secret = page.branch.clone();

View File

@ -25,7 +25,7 @@ pub struct Preview<'a> {
impl<'a> Preview<'a> { impl<'a> Preview<'a> {
pub fn new(ctx: &'a AppCtx) -> Self { pub fn new(ctx: &'a AppCtx) -> Self {
Self { Self {
base: &ctx.settings.server.domain, base: &ctx.settings.page.base_domain,
delimiter: ".", delimiter: ".",
prefix: "deploy-preview-", 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::api::v1::pages::AddSite;
use crate::ctx::Ctx; use crate::ctx::Ctx;
use crate::errors::*; use crate::errors::*;
use crate::page::Page;
use crate::settings::Settings; use crate::settings::Settings;
use crate::*; use crate::*;
const HOSTNAME: &str = "example.org";
pub const REPO_URL: &str = "https://github.com/mCaptcha/website/"; pub const REPO_URL: &str = "https://github.com/mCaptcha/website/";
pub const BRANCH: &str = "gh-pages"; pub const BRANCH: &str = "gh-pages";
@ -268,17 +268,12 @@ impl Ctx {
assert_eq!(resp_err.error, format!("{}", err)); 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 { let msg = AddSite {
repo_url: REPO_URL.into(), repo_url: REPO_URL.into(),
branch: BRANCH.into(), branch: BRANCH.into(),
hostname,
owner, owner,
}; };
self.add_site(msg).await.unwrap(); self.add_site(msg).await.unwrap()
}
pub fn get_test_hostname(&self, unique: &str) -> String {
format!("{unique}.{HOSTNAME}")
} }
} }