Switch to Hono
This commit is contained in:
parent
9601e179bc
commit
9f0e1f8b61
10 changed files with 152 additions and 331 deletions
|
|
@ -7,10 +7,8 @@
|
|||
"dependencies": {
|
||||
"@deltachat/jsonrpc-client": "^1.151.1",
|
||||
"@deltachat/stdio-rpc-server": "^1.151.1",
|
||||
"@hapi/hapi": "^21.4.3",
|
||||
"@hapipal/schmervice": "^3.0.0",
|
||||
"@hapipal/toys": "^4.0.0",
|
||||
"hapi-pino": "^13.0.0",
|
||||
"@hono/node-server": "^1.13.8",
|
||||
"hono": "^4.7.4",
|
||||
"pino": "^9.6.0",
|
||||
"pino-pretty": "^13.0.0"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,39 +1,29 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import hapiPino from "hapi-pino";
|
||||
import Schmervice from "@hapipal/schmervice";
|
||||
import { serve } from "@hono/node-server";
|
||||
import DeltaChatService from "./service.ts";
|
||||
import {
|
||||
ConfigureBotRoute,
|
||||
GetBotRoute,
|
||||
SendMessageRoute,
|
||||
UnconfigureBotRoute,
|
||||
HealthRoute,
|
||||
} from "./routes.ts";
|
||||
import { createRoutes } from "./routes.ts";
|
||||
import { createLogger } from "./lib/logger";
|
||||
|
||||
const logger = createLogger("bridge-deltachat-index");
|
||||
|
||||
const server = Hapi.server({ port: 5001 });
|
||||
|
||||
const startServer = async () => {
|
||||
await server.register({ plugin: hapiPino });
|
||||
|
||||
server.route(ConfigureBotRoute);
|
||||
server.route(GetBotRoute);
|
||||
server.route(SendMessageRoute);
|
||||
server.route(UnconfigureBotRoute);
|
||||
server.route(HealthRoute);
|
||||
|
||||
await server.register(Schmervice);
|
||||
server.registerService(DeltaChatService);
|
||||
|
||||
await server.start();
|
||||
|
||||
return server;
|
||||
};
|
||||
|
||||
const main = async () => {
|
||||
await startServer();
|
||||
const service = new DeltaChatService();
|
||||
await service.initialize();
|
||||
|
||||
const app = createRoutes(service);
|
||||
const port = parseInt(process.env.PORT || "5001", 10);
|
||||
|
||||
serve({ fetch: app.fetch, port }, (info) => {
|
||||
logger.info({ port: info.port }, "bridge-deltachat listening");
|
||||
});
|
||||
|
||||
const shutdown = async () => {
|
||||
logger.info("Shutting down...");
|
||||
await service.teardown();
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
process.on("SIGTERM", shutdown);
|
||||
process.on("SIGINT", shutdown);
|
||||
};
|
||||
|
||||
main().catch((err) => {
|
||||
|
|
|
|||
|
|
@ -1,111 +1,54 @@
|
|||
import * as Hapi from "@hapi/hapi";
|
||||
import Toys from "@hapipal/toys";
|
||||
import DeltaChatService from "./service.ts";
|
||||
import { Hono } from "hono";
|
||||
import type DeltaChatService from "./service.ts";
|
||||
import { createLogger } from "./lib/logger";
|
||||
|
||||
const withDefaults = Toys.withRouteDefaults({
|
||||
options: {
|
||||
cors: true,
|
||||
},
|
||||
});
|
||||
const logger = createLogger("bridge-deltachat-routes");
|
||||
|
||||
const getService = (request: Hapi.Request): DeltaChatService => {
|
||||
const { deltaChatService } = request.services();
|
||||
export function createRoutes(service: DeltaChatService): Hono {
|
||||
const app = new Hono();
|
||||
|
||||
return deltaChatService as DeltaChatService;
|
||||
};
|
||||
app.post("/api/bots/:id/configure", async (c) => {
|
||||
const id = c.req.param("id");
|
||||
const { email, password } = await c.req.json<{ email: string; password: string }>();
|
||||
|
||||
interface ConfigureRequest {
|
||||
email: string;
|
||||
password: string;
|
||||
try {
|
||||
const result = await service.configure(id, email, password);
|
||||
logger.info({ id, email }, "Bot configured");
|
||||
return c.json(result);
|
||||
} catch (err: any) {
|
||||
logger.error({ id, error: err.message }, "Failed to configure bot");
|
||||
return c.json({ error: err.message }, 500);
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/api/bots/:id", async (c) => {
|
||||
const id = c.req.param("id");
|
||||
return c.json(await service.getBot(id));
|
||||
});
|
||||
|
||||
app.post("/api/bots/:id/send", async (c) => {
|
||||
const id = c.req.param("id");
|
||||
const { email, message, attachments } = await c.req.json<{
|
||||
email: string;
|
||||
message: string;
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
||||
}>();
|
||||
|
||||
const result = await service.send(id, email, message, attachments);
|
||||
logger.info({ id, attachmentCount: attachments?.length || 0 }, "Sent message");
|
||||
return c.json({ result });
|
||||
});
|
||||
|
||||
app.post("/api/bots/:id/unconfigure", async (c) => {
|
||||
const id = c.req.param("id");
|
||||
await service.unconfigure(id);
|
||||
logger.info({ id }, "Bot unconfigured");
|
||||
return c.body(null, 200);
|
||||
});
|
||||
|
||||
app.get("/api/health", (c) => {
|
||||
return c.json({ status: "ok" });
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
interface SendMessageRequest {
|
||||
email: string;
|
||||
message: string;
|
||||
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
||||
}
|
||||
|
||||
export const ConfigureBotRoute = withDefaults({
|
||||
method: "post",
|
||||
path: "/api/bots/{id}/configure",
|
||||
options: {
|
||||
description: "Configure a bot with email credentials",
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { email, password } = request.payload as ConfigureRequest;
|
||||
const service = getService(request);
|
||||
|
||||
try {
|
||||
const result = await service.configure(id, email, password);
|
||||
request.logger.info({ id, email }, "Bot configured at %s", new Date().toISOString());
|
||||
return h.response(result).code(200);
|
||||
} catch (err: any) {
|
||||
request.logger.error({ id, error: err.message }, "Failed to configure bot");
|
||||
return h.response({ error: err.message }).code(500);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const GetBotRoute = withDefaults({
|
||||
method: "get",
|
||||
path: "/api/bots/{id}",
|
||||
options: {
|
||||
description: "Get bot status",
|
||||
async handler(request: Hapi.Request, _h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const service = getService(request);
|
||||
return service.getBot(id);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const SendMessageRoute = withDefaults({
|
||||
method: "post",
|
||||
path: "/api/bots/{id}/send",
|
||||
options: {
|
||||
description: "Send a message",
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const { email, message, attachments } = request.payload as SendMessageRequest;
|
||||
const service = getService(request);
|
||||
|
||||
const result = await service.send(id, email, message, attachments);
|
||||
request.logger.info(
|
||||
{ id, attachmentCount: attachments?.length || 0 },
|
||||
"Sent a message at %s",
|
||||
new Date().toISOString(),
|
||||
);
|
||||
|
||||
return h.response({ result }).code(200);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const UnconfigureBotRoute = withDefaults({
|
||||
method: "post",
|
||||
path: "/api/bots/{id}/unconfigure",
|
||||
options: {
|
||||
description: "Unconfigure and remove a bot",
|
||||
async handler(request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
const { id } = request.params;
|
||||
const service = getService(request);
|
||||
|
||||
await service.unconfigure(id);
|
||||
request.logger.info({ id }, "Bot unconfigured at %s", new Date().toISOString());
|
||||
|
||||
return h.response().code(200);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const HealthRoute = withDefaults({
|
||||
method: "get",
|
||||
path: "/api/health",
|
||||
options: {
|
||||
description: "Health check",
|
||||
async handler(_request: Hapi.Request, h: Hapi.ResponseToolkit) {
|
||||
return h.response({ status: "ok" }).code(200);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import { Server } from "@hapi/hapi";
|
||||
import { Service } from "@hapipal/schmervice";
|
||||
import { startDeltaChat, DeltaChat } from "@deltachat/stdio-rpc-server";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
|
@ -17,14 +15,13 @@ interface BotMapping {
|
|||
[botId: string]: number;
|
||||
}
|
||||
|
||||
export default class DeltaChatService extends Service {
|
||||
export default class DeltaChatService {
|
||||
private dc: DeltaChat | null = null;
|
||||
private botMapping: BotMapping = {};
|
||||
private dataDir: string;
|
||||
private mappingFile: string;
|
||||
|
||||
constructor(server: Server, options: never) {
|
||||
super(server, options);
|
||||
constructor() {
|
||||
this.dataDir = process.env.DELTACHAT_DATA_DIR || "/home/node/deltachat-data";
|
||||
this.mappingFile = path.join(this.dataDir, "bot-mapping.json");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
import type DeltaChatService from "./service.ts";
|
||||
|
||||
declare module "@hapipal/schmervice" {
|
||||
interface SchmerviceDecorator {
|
||||
(namespace: "deltachat"): DeltaChatService;
|
||||
}
|
||||
type ServiceFunctionalInterface = { name: string };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue