From 44e77d4bb42db46ec18261513b00c64cfea13f08 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 27 Jun 2026 20:45:58 -0500 Subject: [PATCH] feat(mesh): two-node teapot/teacup example Signed-off-by: Diego Resendez --- example/server.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/example/server.ts b/example/server.ts index 09c4b3b..7ab6c97 100644 --- a/example/server.ts +++ b/example/server.ts @@ -1,4 +1,4 @@ -import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin, needs, param, Sse, Ws, inbound, channel } from '../src/index'; +import { createApp, Provider, Step, Route, Get, Module, Transformer, JsonTransformer, Unauthorized, Plugin, needs, param, Sse, Ws, inbound, channel, MESH_CONTROL_PATH } from '../src/index'; interface User { id: string; name: string } @@ -70,3 +70,55 @@ if (require.main === module) { app.listen(3000).then(() => console.log('green-tea on http://localhost:3000')); streamApp.listen(3001).then(() => console.log('green-tea streams on http://localhost:3001')); } + +// ── Mesh Demo (teapot / teacup) ─────────────────────────────────────────── + +@Provider({ provides: 'config', export: true }) +class ConfigProvider { + provide() { + return { config: { env: 'demo' } }; + } +} + +@Step({ provides: 'auth', needs: [], export: true }) +class AuthStep { + run(ctx: { headers?: Record }) { + const tok = ctx.headers?.['x-token']; + return { auth: Array.isArray(tok) ? tok[0] ?? 'anonymous' : tok ?? 'anonymous' }; + } +} + +@Route('/svc') +class PingController { + @Get('/ping', { export: true }) + ping(@needs('config') config: { env: string }, @needs('auth') auth: string) { + return { pong: true, config, auth }; + } +} + +@Module({ mountpoint: '/mesh', providers: [ConfigProvider], steps: [AuthStep], controllers: [PingController] }) +class TeapotModule {} + +export const teapotApp = createApp({ modules: [TeapotModule], mesh: { secret: 'demo-secret' } }); + +@Route('/remote') +class TeacupController { + @Get('/status') + status(@needs('config') config: { env: string }, @needs('auth') auth: string) { + return { via: 'teacup', config, auth }; + } +} + +@Module({ mountpoint: '/', controllers: [TeacupController] }) +class TeacupModule {} + +if (require.main === module) { + teapotApp.listen(3002).then(() => { + console.log('teapot on http://localhost:3002'); + const teacupApp = createApp({ + modules: [TeacupModule], + mesh: { teapots: [{ url: `ws://127.0.0.1:3002${MESH_CONTROL_PATH}`, secret: 'demo-secret' }] }, + }); + teacupApp.listen(3003).then(() => console.log('teacup on http://localhost:3003')); + }); +}