mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-06 13:21:53 +00:00
Initial commit
This commit is contained in:
62
routes/api/registries/+server.ts
Normal file
62
routes/api/registries/+server.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getRegistries, createRegistry, setDefaultRegistry } from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
|
||||
export const GET: RequestHandler = async ({ cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('registries', 'view')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const registries = await getRegistries();
|
||||
// Don't expose passwords in the response
|
||||
const safeRegistries = registries.map(({ password, ...rest }) => ({
|
||||
...rest,
|
||||
hasCredentials: !!password
|
||||
}));
|
||||
return json(safeRegistries);
|
||||
} catch (error) {
|
||||
console.error('Error fetching registries:', error);
|
||||
return json({ error: 'Failed to fetch registries' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const POST: RequestHandler = async ({ request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('registries', 'create')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await request.json();
|
||||
|
||||
if (!data.name || !data.url) {
|
||||
return json({ error: 'Name and URL are required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const registry = await createRegistry({
|
||||
name: data.name,
|
||||
url: data.url,
|
||||
username: data.username || undefined,
|
||||
password: data.password || undefined,
|
||||
isDefault: data.isDefault || false
|
||||
});
|
||||
|
||||
// If this registry should be default, set it
|
||||
if (data.isDefault) {
|
||||
await setDefaultRegistry(registry.id);
|
||||
}
|
||||
|
||||
// Don't expose password in response
|
||||
const { password, ...safeRegistry } = registry;
|
||||
return json({ ...safeRegistry, hasCredentials: !!password }, { status: 201 });
|
||||
} catch (error: any) {
|
||||
console.error('Error creating registry:', error);
|
||||
if (error.message?.includes('UNIQUE constraint failed')) {
|
||||
return json({ error: 'A registry with this name already exists' }, { status: 400 });
|
||||
}
|
||||
return json({ error: 'Failed to create registry' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
96
routes/api/registries/[id]/+server.ts
Normal file
96
routes/api/registries/[id]/+server.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getRegistry, updateRegistry, deleteRegistry, setDefaultRegistry } from '$lib/server/db';
|
||||
import { authorize } from '$lib/server/authorize';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('registries', 'view')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid registry ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const registry = await getRegistry(id);
|
||||
if (!registry) {
|
||||
return json({ error: 'Registry not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Don't expose password
|
||||
const { password, ...safeRegistry } = registry;
|
||||
return json({ ...safeRegistry, hasCredentials: !!password });
|
||||
} catch (error) {
|
||||
console.error('Error fetching registry:', error);
|
||||
return json({ error: 'Failed to fetch registry' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const PUT: RequestHandler = async ({ params, request, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('registries', 'edit')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid registry ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await request.json();
|
||||
const registry = await updateRegistry(id, {
|
||||
name: data.name,
|
||||
url: data.url,
|
||||
username: data.username,
|
||||
password: data.password,
|
||||
isDefault: data.isDefault
|
||||
});
|
||||
|
||||
if (!registry) {
|
||||
return json({ error: 'Registry not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// If this registry should be default, set it
|
||||
if (data.isDefault) {
|
||||
await setDefaultRegistry(id);
|
||||
}
|
||||
|
||||
// Don't expose password
|
||||
const { password, ...safeRegistry } = registry;
|
||||
return json({ ...safeRegistry, hasCredentials: !!password });
|
||||
} catch (error: any) {
|
||||
console.error('Error updating registry:', error);
|
||||
if (error.message?.includes('UNIQUE constraint failed')) {
|
||||
return json({ error: 'A registry with this name already exists' }, { status: 400 });
|
||||
}
|
||||
return json({ error: 'Failed to update registry' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
export const DELETE: RequestHandler = async ({ params, cookies }) => {
|
||||
const auth = await authorize(cookies);
|
||||
if (auth.authEnabled && !await auth.can('registries', 'delete')) {
|
||||
return json({ error: 'Permission denied' }, { status: 403 });
|
||||
}
|
||||
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid registry ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const deleted = await deleteRegistry(id);
|
||||
if (!deleted) {
|
||||
return json({ error: 'Registry not found or cannot be deleted' }, { status: 404 });
|
||||
}
|
||||
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting registry:', error);
|
||||
return json({ error: 'Failed to delete registry' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
23
routes/api/registries/[id]/default/+server.ts
Normal file
23
routes/api/registries/[id]/default/+server.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { setDefaultRegistry, getRegistry } from '$lib/server/db';
|
||||
|
||||
export const POST: RequestHandler = async ({ params }) => {
|
||||
try {
|
||||
const id = parseInt(params.id);
|
||||
if (isNaN(id)) {
|
||||
return json({ error: 'Invalid registry ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const registry = await getRegistry(id);
|
||||
if (!registry) {
|
||||
return json({ error: 'Registry not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
await setDefaultRegistry(id);
|
||||
return json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error setting default registry:', error);
|
||||
return json({ error: 'Failed to set default registry' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user