chore: re-order backend file structure

This commit is contained in:
Nicolas Meienberger
2025-08-31 17:32:11 +02:00
parent a16fc37b44
commit 23f47bbb6b
28 changed files with 240 additions and 1129 deletions

View File

@@ -0,0 +1,58 @@
import { Hono } from "hono";
export const driverController = new Hono()
.post("/VolumeDriver.Capabilities", (c) => {
return c.json({
Capabilities: {
Scope: "global",
},
});
})
.post("/Plugin.Activate", (c) => {
return c.json({
Implements: ["VolumeDriver"],
});
})
.post("/VolumeDriver.Create", (c) => {
return c.json({
Err: "",
});
})
.post("/VolumeDriver.Remove", (c) => {
return c.json({
Err: "",
});
})
.post("/VolumeDriver.Mount", (c) => {
return c.json({
Mountpoint: `/mnt/something`,
});
})
.post("/VolumeDriver.Unmount", (c) => {
return c.json({
Err: "",
});
})
.post("/VolumeDriver.Path", (c) => {
return c.json({
Mountpoint: `/mnt/something`,
});
})
.post("/VolumeDriver.Get", (c) => {
return c.json({
Name: "my-volume",
Mountpoint: `/mnt/something`,
Status: {},
});
})
.post("/VolumeDriver.List", (c) => {
return c.json({
Volumes: [
{
Name: "my-volume",
Mountpoint: `/mnt/something`,
Status: {},
},
],
});
});

View File

@@ -0,0 +1,50 @@
import { Hono } from "hono";
import { validator } from "hono-openapi/arktype";
import { handleServiceError } from "../../utils/errors";
import {
createVolumeBody,
createVolumeDto,
type ListVolumesResponseDto,
listVolumesDto,
} from "./volume.dto";
import { volumeService } from "./volume.service";
export const volumeController = new Hono()
.get("/", listVolumesDto, async (c) => {
const volumes = await volumeService.listVolumes();
const response = {
volumes: volumes.map((volume) => ({
name: volume.name,
mountpoint: volume.path,
createdAt: volume.createdAt,
})),
} satisfies ListVolumesResponseDto;
return c.json(response, 200);
})
.post(
"/",
createVolumeDto,
validator("json", createVolumeBody),
async (c) => {
const body = c.req.valid("json");
const res = await volumeService.createVolume(body.name, body.config);
if (res.error) {
const { message, status } = handleServiceError(res.error);
return c.json(message, status);
}
return c.json({ message: "Volume created", volume: res.volume });
},
)
.get("/:name", (c) => {
return c.json({ message: `Details of volume ${c.req.param("name")}` });
})
.put("/:name", (c) => {
return c.json({ message: `Update volume ${c.req.param("name")}` });
})
.delete("/:name", (c) => {
return c.json({ message: `Delete volume ${c.req.param("name")}` });
});

View File

@@ -0,0 +1,60 @@
import { type } from "arktype";
import { describeRoute } from "hono-openapi";
import { resolver } from "hono-openapi/arktype";
import { volumeConfigSchema } from "../../db/schema";
/**
* List all volumes
*/
export const listVolumesResponse = type({
volumes: type({
name: "string",
mountpoint: "string",
createdAt: "number",
}).array(),
});
export type ListVolumesResponseDto = typeof listVolumesResponse.infer;
export const listVolumesDto = describeRoute({
description: "List all volumes",
tags: ["Volumes"],
responses: {
200: {
description: "A list of volumes",
content: {
"application/json": {
schema: resolver(listVolumesResponse),
},
},
},
},
});
/**
* Create a new volume
*/
export const createVolumeBody = type({
name: "string",
config: volumeConfigSchema,
});
export const createVolumeResponse = type({
name: "string",
mountpoint: "string",
createdAt: "number",
});
export const createVolumeDto = describeRoute({
description: "Create a new volume",
tags: ["Volumes"],
responses: {
201: {
description: "Volume created successfully",
content: {
"application/json": {
schema: resolver(createVolumeResponse),
},
},
},
},
});

View File

@@ -0,0 +1,44 @@
import * as path from "node:path";
import { eq } from "drizzle-orm";
import { ConflictError } from "http-errors-enhanced";
import slugify from "slugify";
import { config } from "../../core/config";
import { db } from "../../db/db";
import { type BackendConfig, volumesTable } from "../../db/schema";
const listVolumes = async () => {
const volumes = await db.query.volumesTable.findMany({});
return volumes;
};
const createVolume = async (name: string, backendConfig: BackendConfig) => {
const slug = slugify(name, { lower: true, strict: true });
const existing = await db.query.volumesTable.findFirst({
where: eq(volumesTable.name, slug),
});
if (existing) {
return { error: new ConflictError("Volume already exists") };
}
const volumePathHost = path.join(config.volumeRootHost);
const val = await db
.insert(volumesTable)
.values({
name: slug,
config: backendConfig,
path: path.join(volumePathHost, slug),
type: "nfs",
})
.returning();
return { volume: val[0], status: 201 };
};
export const volumeService = {
listVolumes,
createVolume,
};