Files
Shade/packages/shade-cli/templates/bun-server/src/index.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Hono } from 'hono';
import { createShade } from '@shade/sdk';
/**
* __PROJECT_NAME__ Shade-enabled Bun server template.
*
* Exposes two endpoints:
* POST /send encrypt a message to a peer
* POST /receive decrypt an incoming envelope
*/
const shade = await createShade({
prekeyServer: process.env.SHADE_PREKEY_SERVER ?? '__PREKEY_SERVER__',
storage: process.env.SHADE_DB_PATH ?? 'sqlite:./.shade/client.db',
address: '__PROJECT_NAME__',
});
console.log(`Shade initialized as ${shade.myAddress}`);
console.log(`Fingerprint: ${await shade.fingerprint}`);
shade.onMessage((from, msg) => {
console.log(`[${from}] ${msg}`);
});
const app = new Hono();
app.get('/', (c) => c.text(`__PROJECT_NAME__ — Shade-enabled backend`));
app.post('/send', async (c) => {
const { to, message } = await c.req.json();
const envelope = await shade.send(to, message);
return c.json({ envelope });
});
app.post('/receive', async (c) => {
const { from, envelope } = await c.req.json();
const plaintext = await shade.receive(from, envelope);
return c.json({ plaintext });
});
export default {
port: Number(process.env.PORT ?? 3000),
fetch: app.fetch,
};
console.log(`Server listening on :${process.env.PORT ?? 3000}`);