Skip to content

Commit

Permalink
feat: added reclone button to ui
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Aug 10, 2024
1 parent 6091306 commit 2afb6c9
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 6 deletions.
9 changes: 9 additions & 0 deletions backend/src/handlers_prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod logout;
pub use logout::*;
mod github_hook;
pub use github_hook::*;
mod reclone;
pub use reclone::*;

use color_eyre::{
eyre::{Context, ContextCompat},
Expand Down Expand Up @@ -80,6 +82,13 @@ async fn find_user(state: &AppState, headers: HeaderMap) -> color_eyre::Result<O
Ok(None)
}

/// This function is used to add permissions to endpoints.
///
/// When placed at the top of an Axum handler, you can specify permission(s)
/// to require. If they are missing from the user, it will return an error,
/// which you can propagate through the handler with `?`.
// TODO: Write unit tests for this. May require refactoring so that
// it only needs a database, instead of the whole app state
pub async fn require_perms(
State(state): State<&AppState>,
headers: HeaderMap,
Expand Down
12 changes: 12 additions & 0 deletions backend/src/handlers_prelude/reclone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use axum::{extract::State, http::HeaderMap};
use reqwest::StatusCode;

use crate::{perms::Permission, AppState};

use super::{eyre_to_axum_err, require_perms};

pub async fn post_reclone_handler(State(state): State<AppState>, headers: HeaderMap) -> Result<(), (StatusCode, String)> {
require_perms(State(&state), headers, &[Permission::ManageUsers]).await?;
state.git.reclone().map_err(eyre_to_axum_err)?;
Ok(())
}
10 changes: 6 additions & 4 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ async fn start_server(state: AppState, cli_args: Args) -> Result<()> {
let app = Router::new()
.route("/api/hello", get(|| async { "Hello world" }))
.route("/api/logout", get(get_logout_handler))
.route("/api/reclone", post(post_reclone_handler))
.route("/api/hooks/github", post(github_hook_handler))
.route("/api/doc", get(get_doc_handler))
.route("/api/doc", put(put_doc_handler))
Expand Down Expand Up @@ -267,11 +268,12 @@ async fn start_server(state: AppState, cli_args: Args) -> Result<()> {
// closures to attach a value to the initially empty field in the info_span
// created above.
})
.on_response(|response: &Response, latency: Duration, span: &Span| {
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
// I don't know if this is strictly needed, should be tested
let _span = span.clone().entered();
let latency_ms = format!("{}ms", latency.as_millis());
info!(latency=%latency_ms, status=%response.status());
// Commented out because it was creating duplicate logs for endpoints
// let _span = span.clone().entered();
// let latency_ms = format!("{}ms", latency.as_millis());
// info!(latency=%latency_ms, status=%response.status());
}),
);

Expand Down
7 changes: 5 additions & 2 deletions frontend/src/lib/components/dashboard/AdminDashboard.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script lang="ts">
import GroupTab from './GroupTab.svelte';
import UserTab from './UserTab.svelte';
import ServerTab from './ServerTab.svelte';
export let dialog: HTMLDialogElement;
let selectedTab = 0;
let tabs = [
{ name: 'User Management', id: 0, component: UserTab },
{ name: 'Group Management', id: 1, component: GroupTab }
{ name: 'Group Management', id: 1, component: GroupTab },
{ name: 'Server Management', id: 2, component: ServerTab }
];
// E must be defined as any because for some reason typescript thinks parentElement doesn't exist on e.target
Expand Down Expand Up @@ -72,7 +74,8 @@
margin-bottom: 0;
padding-left: 0;
height: 100%;
width: 33.33%;
min-width: 8rem;
max-width: 8rem;
border-right: 1.5px solid var(--background-3);
list-style-type: none;
}
Expand Down
108 changes: 108 additions & 0 deletions frontend/src/lib/components/dashboard/ServerTab.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<script lang="ts">
import { apiAddress } from '$lib/net';
import { addToast, dismissToast, ToastType } from '$lib/toast';
</script>

<div class="container">
<!-- Actions -->
<div class="header">
<p>Actions</p>
<hr />
</div>
<!-- Admin Dashboard -->
<div>
<button
on:click={async () => {
const tid = addToast({
message: `Cloning fresh repository, this may take a while...`,
type: ToastType.Info,
dismissible: false
});
const response = await fetch(`${apiAddress}/api/reclone`, {
method: 'POST',
credentials: 'include'
});
dismissToast(tid);
if (response.ok) {
addToast({
message: `Cloned fresh repository successfully`,
type: ToastType.Success,
dismissible: true,
timeout: 3000
});
} else {
addToast({
message: `Clone failed, check server logs`,
type: ToastType.Error,
dismissible: true,
timeout: 3000
});
}
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
height="24px"
viewBox="0 -960 960 960"
width="24px"
fill="#e8eaed"
><path
d="M160-160v-80h110l-16-14q-52-46-73-105t-21-119q0-111 66.5-197.5T400-790v84q-72 26-116 88.5T240-478q0 45 17 87.5t53 78.5l10 10v-98h80v240H160Zm400-10v-84q72-26 116-88.5T720-482q0-45-17-87.5T650-648l-10-10v98h-80v-240h240v80H690l16 14q49 49 71.5 106.5T800-482q0 111-66.5 197.5T560-170Z"
/></svg
>
Reclone Repository
</button>
</div>
<!-- Metrics -->
<div class="header">
<p>Metrics</p>
<hr />
</div>
TODO
</div>

<style>
.container {
margin: 0.3rem;
width: 100%;
}
.container * {
font-size: 1rem;
color: var(--foreground-3);
}
.header p {
margin-top: 0.3rem;
margin-bottom: 0;
padding-left: 0.3rem;
font-size: 0.7rem;
color: var(--foreground-3);
}
.header hr {
margin: 0.2rem;
border-color: var(--foreground-5);
}
button {
display: flex;
align-items: center;
padding-left: 1rem;
width: 100%;
height: 2rem;
border-radius: 0.5rem;
background-color: transparent;
border: none;
}
button:hover {
background-color: var(--background-2);
cursor: pointer;
}
svg {
fill: var(--foreground-3);
padding-right: 0.2rem;
}
</style>

0 comments on commit 2afb6c9

Please sign in to comment.