mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-04 13:19:57 +00:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { json } from '@sveltejs/kit';
|
|
import { startContainer, inspectContainer } from '$lib/server/docker';
|
|
import { authorize } from '$lib/server/authorize';
|
|
import { auditContainer } from '$lib/server/audit';
|
|
import type { RequestHandler } from './$types';
|
|
|
|
export const POST: RequestHandler = async (event) => {
|
|
const { params, url, cookies } = event;
|
|
const auth = await authorize(cookies);
|
|
|
|
const envId = url.searchParams.get('env');
|
|
const envIdNum = envId ? parseInt(envId) : undefined;
|
|
|
|
// Permission check with environment context
|
|
if (auth.authEnabled && !await auth.can('containers', 'start', envIdNum)) {
|
|
return json({ error: 'Permission denied' }, { status: 403 });
|
|
}
|
|
|
|
// Environment access check (enterprise only)
|
|
if (envIdNum && auth.isEnterprise && !await auth.canAccessEnvironment(envIdNum)) {
|
|
return json({ error: 'Access denied to this environment' }, { status: 403 });
|
|
}
|
|
|
|
try {
|
|
|
|
await startContainer(params.id, envIdNum);
|
|
const details = await inspectContainer(params.id, envIdNum);
|
|
const containerName = details.Name.replace(/^\//, '');
|
|
|
|
// Audit log
|
|
await auditContainer(event, 'start', params.id, containerName, envIdNum);
|
|
|
|
return json({ success: true });
|
|
} catch (error) {
|
|
console.error('Error starting container:', error);
|
|
return json({ error: 'Failed to start container' }, { status: 500 });
|
|
}
|
|
};
|