feat(compose): add import from base64 in create service dropdown

Adds an "Import" option to the Create Service dropdown that lets users
paste a base64-encoded compose export, preview the template (compose YAML,
domains, envs, mounts) before confirming, and create the service only on
confirm. Adds a `previewTemplate` tRPC procedure that processes the base64
without touching the DB, with server access validation via session.
This commit is contained in:
Mauricio Siu
2026-05-12 13:12:14 -06:00
parent a714e0f83f
commit 754774ea02
3 changed files with 564 additions and 0 deletions

View File

@@ -862,6 +862,76 @@ export const composeRouter = createTRPCRouter({
}
}),
previewTemplate: protectedProcedure
.input(
z.object({
base64: z.string(),
appName: z.string(),
serverId: z.string().optional(),
}),
)
.mutation(async ({ input, ctx }) => {
try {
if (input.serverId) {
const accessibleIds = await getAccessibleServerIds(ctx.session);
if (!accessibleIds.has(input.serverId)) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this server",
});
}
}
const decodedData = Buffer.from(input.base64, "base64").toString(
"utf-8",
);
let serverIp = "127.0.0.1";
if (input.serverId) {
const server = await findServerById(input.serverId);
serverIp = server.ipAddress;
} else if (process.env.NODE_ENV !== "development") {
const settings = await getWebServerSettings();
serverIp = settings?.serverIp || "127.0.0.1";
}
const templateData = JSON.parse(decodedData);
const config = parse(templateData.config) as CompleteTemplate;
if (!templateData.compose || !config) {
throw new TRPCError({
code: "BAD_REQUEST",
message:
"Invalid template format. Must contain compose and config fields",
});
}
const configModified = {
...config,
variables: {
APP_NAME: input.appName,
...config.variables,
},
};
const processedTemplate = processTemplate(configModified, {
serverIp,
projectName: input.appName,
});
return {
compose: templateData.compose,
template: processedTemplate,
};
} catch (error) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error processing template: ${error instanceof Error ? error.message : error}`,
});
}
}),
import: protectedProcedure
.input(
z.object({