Skip to content

Commit

Permalink
add models endpoint, fix server determination, add readiness schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnrushefsky committed Aug 28, 2024
1 parent bce1dfc commit 37b1ce3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "comfyui-wrapper",
"version": "1.2.0",
"version": "1.3.0",
"description": "Wraps comfyui to make it easier to use as a web service",
"main": "dist/src/index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ const model_dirs = fs.readdirSync(MODEL_DIR);
for (const model_dir of model_dirs) {
const model_path = path.join(MODEL_DIR, model_dir);
if (fs.statSync(model_path).isDirectory()) {
const all = fs.readdirSync(model_path);
const all = fs
.readdirSync(model_path)
.filter((f) => !(f.startsWith("put_") && f.endsWith("_here")));
config.models[model_dir] = {
dir: model_path,
all,
Expand Down
91 changes: 65 additions & 26 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ const server = Fastify({
server.setValidatorCompiler(validatorCompiler);
server.setSerializerCompiler(serializerCompiler);

const modelSchema: any = {};
for (const modelType in config.models) {
modelSchema[modelType] = z.string().array();
}

const ModelResponseSchema = z.object(modelSchema);
type ModelResponse = z.infer<typeof ModelResponseSchema>;

const modelResponse: ModelResponse = {};
for (const modelType in config.models) {
modelResponse[modelType] = config.models[modelType].all;
}

server.register(fastifySwagger, {
openapi: {
openapi: "3.0.0",
info: {
title: "Comfy Wrapper API",
version,
},
servers: [
{
url: `http://localhost:${config.wrapperPort}`,
description: "Local server",
},
],
},
transform: jsonSchemaTransform,
});
Expand All @@ -62,36 +69,68 @@ server.register(fastifySwaggerUI, {

server.after(() => {
const app = server.withTypeProvider<ZodTypeProvider>();
app.route({
method: "GET",
url: "/health",
schema: {
response: {
200: z.object({
version: z.string(),
status: z.literal("healthy"),
}),
500: z.object({
version: z.string(),
status: z.literal("not healthy"),
}),
app.get(
"/health",
{
schema: {
response: {
200: z.object({
version: z.literal(version),
status: z.literal("healthy"),
}),
500: z.object({
version: z.literal(version),
status: z.literal("not healthy"),
}),
},
},
},
handler: async (request, reply) => {
async (request, reply) => {
// 200 if ready, 500 if not
if (warm) {
return reply.code(200).send({ version, status: "healthy" });
}
return reply.code(500).send({ version, status: "not healthy" });
}
);

app.get(
"/ready",
{
schema: {
response: {
200: z.object({
version: z.literal(version),
status: z.literal("ready"),
}),
500: z.object({
version: z.literal(version),
status: z.literal("not ready"),
}),
},
},
},
});
async (request, reply) => {
if (warm) {
return reply.code(200).send({ version, status: "ready" });
}
return reply.code(500).send({ version, status: "not ready" });
}
);

app.get("/ready", async (request, reply) => {
if (warm) {
return reply.code(200).send({ version, status: "ready" });
app.get(
"/models",
{
schema: {
response: {
200: ModelResponseSchema,
},
},
},
async (request, reply) => {
return modelResponse;
}
return reply.code(500).send({ version, status: "not ready" });
});
);

app.post<{
Body: PromptRequest;
Expand Down

0 comments on commit 37b1ce3

Please sign in to comment.