Skip to content

Commit

Permalink
create indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybutera committed Aug 3, 2023
1 parent 5d2252c commit 40f4e14
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 13 deletions.
8 changes: 8 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
rustfmt
pre-commit
rustPackages.clippy

# for exif/rexiv2 crates
/*
pkg-config
libexif
gexiv2
glib
*/
];
RUST_SRC_PATH = rustPlatform.rustLibSrc;
};
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ async fn main_async() -> tide::Result<()> {
}

async fn get_index(req: Request<ServerState>) -> tide::Result {
log::info!("get_index");
let name = normalize_topic(req.param("name")?);
let mut path = req.state().args.root_dir.clone();
path.push("indexes");
path.push(format!("{}.json", name));

let index = smol::fs::read_to_string(path).await?;
Expand All @@ -113,8 +115,13 @@ async fn create_index(mut req: Request<ServerState>) -> tide::Result {
let index: Index = req.body_json().await?;
let name = normalize_topic(&index.name);
let mut path = req.state().args.root_dir.clone();
path.push(format!("{}.json", name));
path.push("indexes");

if !path.exists() {
smol::fs::create_dir_all(&path).await?;
}

path.push(format!("{}.json", name));
if path.exists() {
return Err(to_badreq(anyhow!("Index already exists")));
}
Expand Down
35 changes: 34 additions & 1 deletion ui/src/lib/img.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Img server address
//export const img_server: string = "http://localhost:2342";
//export const img_server: string = "http://127.0.0.1:2342";
export const img_server: string = "https://img.smdhi.xyz:8080";

interface Index {
name: string;
topics: string[];
}

export async function get_image_names(topic: string): Promise<string[]> {
try {
// Make a GET request to the endpoint
Expand Down Expand Up @@ -38,3 +43,31 @@ async function processFile(topic: string, file) {
throw new Error(`Error in upload: ${response.statusText}`);
}
}

export async function get_index(index: string): Promise<Index> {
const response = await fetch(`${img_server}/index/${index}`);
//const response = await fetch(`http://127.0.0.1:2342/index/test`);

console.log("response\n", response);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

// Parse the json and check it matches the `Index` type
const data: Index = await response.json();

return data;
}

export async function create_index(
index: string,
topics: list[string]): Promise<void> {
// Make a POST request to the img server /new-index with a json body
const response = await fetch(`${img_server}/new-index`, {
method: 'POST',
body: JSON.stringify({
name: index,
topics: topics
}),
});
}
2 changes: 0 additions & 2 deletions ui/src/routes/+page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { get_image_names } from '$lib/img.ts';

export function load({ params }) {
return {
}
Expand Down
4 changes: 2 additions & 2 deletions ui/src/routes/[topic]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get_image_names } from '$lib/img.ts';

export function load({ params }) {
const imgs = get_image_names(params.topic);
export async function load({ params }) {
const imgs = await get_image_names(params.topic);
return {
topic: params.topic,
images: imgs,
Expand Down
6 changes: 3 additions & 3 deletions ui/src/routes/index/[index]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script>
import Nav from "../../components/Nav.svelte";
import Nav from "../../../components/Nav.svelte";
import { img_server, handle_file_upload } from "$lib/img.ts";
import { goto } from "$app/navigation";
import Uploading from '../../components/Uploading.svelte';
import Uploading from '../../../components/Uploading.svelte';
export let data;
const index = data.index_name;
const topics = data.topics;
Expand All @@ -17,7 +17,7 @@
<ul>
{#each topics as topic}
<li>
<a href="/{index}/{topic}">{topic}</a>
<a href="/{topic}">{topic}</a>
</li>
{/each}
</ul>
8 changes: 4 additions & 4 deletions ui/src/routes/index/[index]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { get_image_names, get_index } from '$lib/img.ts';
import { get_index } from '$lib/img.ts';

export function load({ params }) {
const imgs = get_image_names(params.index);
export async function load({ params }) {
const index = await get_index(params.index);
return {
index_name: params.index,
topics: get_index(params.index).topics,
topics: index.topics,
}
}
1 change: 1 addition & 0 deletions ui/src/routes/new-index/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import { create_index } from "$lib/img.ts";
import Nav from '../../components/Nav.svelte';
let name;
let topics;
let submit_result;
Expand Down

0 comments on commit 40f4e14

Please sign in to comment.