This section explains how to set up a user microservice using s42-core.
Files
package.json
Defines the package metadata, dependencies, and scripts for the microservice. Key scripts include build, start, and test commands.
.local.env
Contains environment variables for local development, including ports and database URIs.
tsconfig.json
Configures the TypeScript compiler options for the project, ensuring consistent and error-free compilation.
src/index.ts
Sets up the microservice server, initializes necessary dependencies, and starts the server. Uses Cluster for process management, ensuring optimal resource utilization.
src/controllers/index.ts
Exports controllers to be used in the microservice.
src/controllers/health.ts
Defines a simple health check endpoint to verify that the server is running.
src/controllers/users.ts
Defines the user creation endpoint. Validates the request data, inserts a new user into the database, and emits a user creation event.
src/test/index.ts
Contains tests for the microservice, including emitting user creation events and verifying their handling
Creating a Cell to Listen for User Creation Events and Send an Email
This section explains how to create a cell that listens for user creation events and sends a welcome email.
Files
.local.env
Contains environment variables for the cell, including ports and database URIs.
package.json
Defines the package metadata, dependencies, and scripts for the cell. Key scripts include build, start, and test commands.
tsconfig.json
Configures the TypeScript compiler options for the cell, ensuring consistent and error-free compilation.
src/index.ts
Sets up the cell to listen for user creation events. When an event is received, it sends a welcome email and emits a confirmation event.
test/index.ts
Contains tests for the cell, including emitting user creation events and verifying email sending.
Overview of the Process
Setting Up the Cell: Initialize EventsDomain and RedisClient, and subscribe to the users.created event.
Subscribing to Events: Use the listenEvent method to handle user creation events and send welcome emails.
Sending Emails: Implement the logic to send emails within the event handler.
Handling Shutdown: Use the Shutdown class to ensure the cell disconnects properly from the event system when stopping.
By following these steps, you can create a robust and scalable system that handles user creation and email sending efficiently using s42-core.
For assistance, you can contact me via my personal Telegram channel or by email: