diff --git a/packages/api/package.json b/packages/api/package.json index 1fc94b17c..b358c82ab 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -7,8 +7,12 @@ "@t3-oss/env-core": "^0.7.1", "@trpc/server": "^10.44.1", "@usevenice/trpc-openapi": "^1.3.8", + "express": ">=5.0.0-beta.1", "trpc-panel": "^1.3.4", "zod": "^3.22.4", "zod-openapi": "^2.11.0" + }, + "devDependencies": { + "@types/express": "^4.17.17" } } diff --git a/packages/api/router.ts b/packages/api/router.ts index 4205363c5..459b640a0 100644 --- a/packages/api/router.ts +++ b/packages/api/router.ts @@ -1,17 +1,12 @@ import { createHTTPServer } from '@trpc/server/adapters/standalone'; -import { createOpenApiHttpHandler, generateOpenApiDocument } from '@usevenice/trpc-openapi'; -import http from 'http'; +import { createOpenApiExpressMiddleware, generateOpenApiDocument } from '@usevenice/trpc-openapi'; +import express from 'express'; import { renderTrpcPanel } from 'trpc-panel'; import { z } from 'zod'; import { createContext, t } from './context'; import { engagementRouter } from './routers/engagement'; export const metaRouter = t.router({ - hello: t.procedure - .meta({ openapi: { method: 'GET', path: '/hello' } }) - .input(z.void()) - .output(z.string()) - .query(() => 'world'), getOpenApiSpec: t.procedure .meta({ openapi: { method: 'GET', path: '/openapi.json' } }) .input(z.void()) @@ -21,36 +16,45 @@ export const metaRouter = t.router({ export const appRouter = t.mergeRouters(t.router({ engagement: engagementRouter }), metaRouter); +const port = 3000; + export const openApiSpec = generateOpenApiDocument(appRouter, { title: 'Supaglue OpenAPI', openApiVersion: '3.1.0', version: '0.0.1', - baseUrl: 'http://localhost:3000', + baseUrl: `http://localhost:${port}`, }); if (require.main === module) { - // trpc server + // openapi server, running in the Docker container with express + const app = express(); + app.get('/', (_req, res) => { + res.send('Our current API routes'); + }); + app.use( + createOpenApiExpressMiddleware({ + router: appRouter, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + createContext: ({ req }) => createContext({ headers: req.headers }), + }) + ); + + app.listen(port, () => { + // eslint-disable-next-line no-console + console.log(`Server is running on port ${port}`); + }); + + // trpc server, running in api routes in next.js for our internal management ui use createHTTPServer({ router: appRouter, // eslint-disable-next-line @typescript-eslint/no-explicit-any createContext: ({ req }) => createContext({ headers: req.headers }), middleware(req, res, next) { if (req.url === '/_panel') { - res.end(renderTrpcPanel(appRouter, { url: 'http://localhost:3000' })); + res.end(renderTrpcPanel(appRouter, { url: 'http://localhost:3001' })); return; } next(); }, - }).listen(3000); - - // openapi server - http - .createServer( - createOpenApiHttpHandler({ - router: appRouter, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - createContext: ({ req }) => createContext({ headers: req.headers }), - }) - ) - .listen(3001); + }).listen(3001); }