Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] feat: standalone express router #198

Closed
wants to merge 10 commits into from
37 changes: 28 additions & 9 deletions js/flow/src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,31 +831,50 @@ function wrapAsAction<
);
}

export function startFlowsServer(params?: {
export function createFlowsExpressRouter(params?: {
flows?: Flow<any, any, any>[];
port?: number;
cors?: CorsOptions;
pathPrefix?: string;
}) {
const router = express.Router();
router.use(bodyParser.json());
router.use(cors(params?.cors));

const port =
params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400;
const pathPrefix = params?.pathPrefix ?? '';
const app = express();
app.use(bodyParser.json());
app.use(cors(params?.cors));

const flows = params?.flows || createdFlows();
logger.info(`Starting flows server on port ${port}`);
flows.forEach((f) => {
const flowPath = `/${pathPrefix}${f.name}`;
logger.info(` - ${flowPath}`);
// Add middlware
f.middleware?.forEach((m) => {
app.post(flowPath, m);
router.post(flowPath, m);
});
app.post(flowPath, f.expressHandler);
router.post(flowPath, f.expressHandler);
});

return router;
}

export function startFlowsServer(params?: {
flows?: Flow<any, any, any>[];
port?: number;
cors?: CorsOptions;
pathPrefix?: string;
}) {
const port =
params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400;
const router = createFlowsExpressRouter({
flows: params?.flows,
cors: params?.cors,
pathPrefix: params?.pathPrefix,
});

logger.info(`Starting flows server on port ${port}`);
const app = express();
app.use(router);

app.listen(port, () => {
console.log(`Flows server listening on port ${port}`);
});
Expand Down
Loading