Skip to content

Commit

Permalink
feat: don't show shutdown button in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
frectonz committed Aug 7, 2024
1 parent 3e16bf8 commit 1087ab1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2936,8 +2936,9 @@ mod responses {
}

#[derive(Serialize)]
pub struct Version {
pub struct Metadata {
pub version: String,
pub can_shutdown: bool,
}
}

Expand All @@ -2946,7 +2947,7 @@ mod handlers {
use tokio::sync::mpsc;
use warp::Filter;

use crate::{rejections, responses::Version, Database};
use crate::{rejections, responses::Metadata, Database};

fn with_state<T: Clone + Send>(
state: &T,
Expand Down Expand Up @@ -2982,7 +2983,10 @@ mod handlers {
.and(warp::path!("query"))
.and(warp::body::json::<QueryBody>())
.and_then(query);
let version = warp::get().and(warp::path!("version")).and_then(version);
let metadata = warp::get()
.and(warp::path!("metadata"))
.and(warp::any().map(move || no_shutdown))
.and_then(metadata);
let shutdown = warp::post()
.and(warp::path!("shutdown"))
.and(with_state(&shutdown_signal))
Expand All @@ -2994,7 +2998,7 @@ mod handlers {
.or(table)
.or(query)
.or(data)
.or(version)
.or(metadata)
.or(shutdown)
}

Expand Down Expand Up @@ -3058,9 +3062,10 @@ mod handlers {
Ok(warp::reply::json(&tables))
}

async fn version() -> Result<impl warp::Reply, warp::Rejection> {
let version = Version {
async fn metadata(no_shutdown: bool) -> Result<impl warp::Reply, warp::Rejection> {
let version = Metadata {
version: env!("CARGO_PKG_VERSION").to_owned(),
can_shutdown: !no_shutdown,
};

Ok(warp::reply::json(&version))
Expand Down
5 changes: 3 additions & 2 deletions ui/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ const query = z.object({
rows: z.any().array().array(),
});

const version = z.object({
const metadata = z.object({
version: z.string(),
can_shutdown: z.boolean(),
});

const $fetch = createZodFetcher();
Expand All @@ -84,7 +85,7 @@ export const fetchQuery = (value: string) =>
},
body: JSON.stringify({ query: value }),
});
export const fetchVersion = () => $fetch(version, `${BASE_URL}/version`);
export const fetchMetadata = () => $fetch(metadata, `${BASE_URL}/metadata`);

export const sendShutdown = () =>
fetch(`${BASE_URL}/shutdown`, { method: "POST" });
8 changes: 4 additions & 4 deletions ui/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useQuery } from "@tanstack/react-query";
import { Link, Outlet, createRootRoute } from "@tanstack/react-router";

import { cn } from "@/lib/utils";
import { fetchVersion, sendShutdown } from "@/api";
import { fetchMetadata, sendShutdown } from "@/api";
import { setTheme, useTheme } from "@/provider/theme.provider";

import {
Expand Down Expand Up @@ -46,8 +46,8 @@ export function Root() {
const changeTheme = setTheme();

const { data } = useQuery({
queryKey: ["version"],
queryFn: () => fetchVersion(),
queryKey: ["metadata"],
queryFn: () => fetchMetadata(),
});

return (
Expand Down Expand Up @@ -94,7 +94,7 @@ export function Root() {
<p className="text-primary hidden sm:block text-xs text-right">
[{data?.version ?? ""}]
</p>
<Shutdown />
{data?.can_shutdown && <Shutdown />}
<button
className="text-foreground "
onClick={() => {
Expand Down

0 comments on commit 1087ab1

Please sign in to comment.