From 398f08c07a12bea75c8024518e661d732f232125 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 5 Dec 2022 15:48:16 +0530 Subject: [PATCH] feat: view site details, deploy secret and latest events closes: https://git.batsense.net/LibrePages/librepages/issues/10 --- src/pages/dash/home.rs | 18 +++- src/pages/dash/sites/add.rs | 43 +++++--- src/pages/dash/sites/mod.rs | 6 ++ src/pages/dash/sites/view.rs | 152 +++++++++++++++++++++++++++ src/pages/routes.rs | 15 ++- templates/pages/dash/index.html | 2 +- templates/pages/dash/sites/view.html | 71 +++++++++++++ 7 files changed, 285 insertions(+), 22 deletions(-) create mode 100644 src/pages/dash/sites/view.rs create mode 100644 templates/pages/dash/sites/view.html diff --git a/src/pages/dash/home.rs b/src/pages/dash/home.rs index 682f8af..2cdda16 100644 --- a/src/pages/dash/home.rs +++ b/src/pages/dash/home.rs @@ -40,8 +40,20 @@ pub struct Home { #[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] pub struct TemplateSite { - site: Site, - last_update: Option, + pub site: Site, + pub view: String, + pub last_update: Option, +} + +impl TemplateSite { + pub fn new(site: Site, last_update: Option) -> Self { + let view = PAGES.dash.site.get_view(site.pub_id.clone()); + Self { + site, + last_update, + view, + } + } } impl CtxError for Home { @@ -77,7 +89,7 @@ async fn get_site_data(ctx: &AppCtx, id: &Identity) -> ServiceResult + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::cell::RefCell; + +use actix_identity::Identity; +use actix_web::http::header::ContentType; +use serde::{Deserialize, Serialize}; +use tera::Context; +use uuid::Uuid; + +use super::get_auth_middleware; + +use crate::db::Site; +use crate::pages::dash::TemplateSiteEvent; +use crate::pages::errors::*; +use crate::settings::Settings; +use crate::AppCtx; + +pub use super::*; + +pub const DASH_SITE_VIEW: TemplateFile = + TemplateFile::new("dash_site_view", "pages/dash/sites/view.html"); + +const SHOW_DEPLOY_SECRET_KEY: &str = "show_deploy_secret"; + +pub struct View { + ctx: RefCell, +} + +impl CtxError for View { + fn with_error(&self, e: &ReadableError) -> String { + self.ctx.borrow_mut().insert(ERROR_KEY, e); + self.render() + } +} + +impl View { + pub fn new(settings: &Settings, payload: Option) -> Self { + let ctx = RefCell::new(context(settings)); + if let Some(payload) = payload { + ctx.borrow_mut().insert(PAYLOAD_KEY, &payload); + } + + Self { ctx } + } + + pub fn show_deploy_secret(&mut self) { + self.ctx.borrow_mut().insert(SHOW_DEPLOY_SECRET_KEY, &true); + } + + pub fn render(&self) -> String { + TEMPLATES + .render(DASH_SITE_VIEW.name, &self.ctx.borrow()) + .unwrap() + } +} + +#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)] +pub struct TemplateSiteWithEvents { + pub site: Site, + pub view: String, + pub last_update: Option, + pub events: Vec, +} + +impl TemplateSiteWithEvents { + pub fn new( + site: Site, + last_update: Option, + events: Vec, + ) -> Self { + let view = PAGES.dash.site.get_view(site.pub_id.clone()); + Self { + site, + last_update, + view, + events, + } + } +} + + +#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)] +pub struct ViewOptions { + show_deploy_secret: Option +} + +#[actix_web_codegen_const_routes::get( + path = "PAGES.dash.site.view", + wrap = "get_auth_middleware()" +)] +#[tracing::instrument(name = "Dashboard add site webpage", skip(ctx, id))] +pub async fn get_view_site( + ctx: AppCtx, + id: Identity, + path: web::Path, + query: web::Query +) -> PageResult { + let site_id = path.into_inner(); + let owner = id.identity().unwrap(); + + let site = ctx + .db + .get_site_from_pub_id(site_id, owner) + .await + .map_err(|e| PageError::new(View::new(&ctx.settings, None), e))?; + let last_update = ctx + .db + .get_latest_update_event(&site.hostname) + .await + .map_err(|e| PageError::new(View::new(&ctx.settings, None), e))?; + + let last_update = last_update.map(|e| e.into()); + + let mut db_events = ctx + .db + .list_all_site_events(&site.hostname) + .await + .map_err(|e| PageError::new(View::new(&ctx.settings, None), e))?; + + let mut events = Vec::with_capacity(db_events.len()); + for e in db_events.drain(0..) { + events.push(e.into()); + } + + let payload = TemplateSiteWithEvents::new(site, last_update, events); + let mut page = View::new(&ctx.settings, Some(payload)); + if let Some(true) = query.show_deploy_secret { + page.show_deploy_secret(); + } + let add = page.render(); + let html = ContentType::html(); + Ok(HttpResponse::Ok().content_type(html).body(add)) +} + +pub fn services(cfg: &mut web::ServiceConfig) { + cfg.service(get_view_site); +} diff --git a/src/pages/routes.rs b/src/pages/routes.rs index 97d40fd..56352b5 100644 --- a/src/pages/routes.rs +++ b/src/pages/routes.rs @@ -16,6 +16,7 @@ */ use actix_auth_middleware::{Authentication, GetLoginRoute}; use serde::*; +use uuid::Uuid; /// constant [Pages](Pages) instance pub const PAGES: Pages = Pages::new(); @@ -85,15 +86,25 @@ impl Dash { #[derive(Serialize)] /// Dashboard Site routes pub struct DashSite { - /// home route + /// add site route pub add: &'static str, + /// view site route + pub view: &'static str, } impl DashSite { /// create new instance of DashSite route pub const fn new() -> DashSite { let add = "/dash/site/add"; - DashSite { add } + let view = "/dash/site/view/{deployment_pub_id}"; + DashSite { add, view } + } + + pub fn get_view(&self, deployment_pub_id: Uuid) -> String { + self.view.replace( + "{deployment_pub_id}", + deployment_pub_id.to_string().as_ref(), + ) } } diff --git a/templates/pages/dash/index.html b/templates/pages/dash/index.html index d7f34dd..a09fb45 100644 --- a/templates/pages/dash/index.html +++ b/templates/pages/dash/index.html @@ -10,7 +10,7 @@ {% if payload|length > 0 %} {% for deployment in payload %} - +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + {% if payload.last_updated %} + + {% else %} + + {% endif %} + +
Hostname{{ payload.site.hostname }}
Repository{{ payload.site.repo_url }}
Secret + {% if show_deploy_secret %} + {{ payload.site.site_secret }} Hide + {% else %} + **** + + Show + + {% endif %} +
Branch{{ payload.site.branch }}
Last Updated{{ payload.last_updated.time }}N/A
+
+ +
+

Events

+ + + + + + + + + + {% for event in payload.events %} + + + + + + {% endfor %} + +
IDEvent TypeTime
{{ event.id }}{{ event.event_type.name }}{{ event.time }}
+
+
+ + +{% endblock main %}