cube-microservice
v1.0.10
Published
TODO: Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
Readme
# MicroService Project
This project is a TypeScript-based microservice template that uses Express.js for creating a scalable and maintainable backend service. It includes essential middleware like Helmet for security, rate limiting, and a simple logging mechanism.
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Project Structure](#project-structure)
- [Configuration](#configuration)
- [Scripts](#scripts)
- [License](#license)
## Installation
To get started with this project, clone the repository and install the necessary dependencies.
```bash
git clone https://devops.ccn/PegasusCollection/Project%20Pegasus/_git/microservice
cd microservice
npm installUsage
Compile the TypeScript files:
npx tscStart the service:
node dist/index.js
Service Class
Creating a Microservice:
import { Service } from './microservice'; const port = 3000; // Specify the port for your microservice const myService = new Service(port);Adding Middleware:
// Import middleware functions import { validateApiKey, validateServiceID, whitelist, } from "./microservice"; // Adding middleware to the service myService.use(validateApiKey(["myApiKey1", "myApiKey2"])); // Add API key validation middleware myService.use(validateServiceID("myServiceID")); // Add service ID validation middleware myService.use(whitelist(["192.168.0.1", "192.168.0.2"])); // Add IP whitelist middlewareAdding Routes:
// Adding a route with a handler function myService.addRoute("get", "/example", (req, res, logger, getService) => { // Handler function logic here res.send("Hello from the microservice!"); });Starting the Microservice:
// Start the microservice myService.startServer();
Middleware Functions
validateApiKey:
import { validateApiKey } from "./microservice"; // Usage myService.use(validateApiKey(["myApiKey1", "myApiKey2"]));validateServiceID:
import { validateServiceID } from "./microservice"; // Usage myService.use(validateServiceID("myServiceID"));whitelist:
import { whitelist } from "./microservice"; // Usage myService.use(whitelist(["192.168.0.1", "192.168.0.2"]));
Project Structure
microservice/
├── dist/ # Compiled JavaScript files
├── src/ # Source files
│ ├── index.ts # Entry point
│ ├── microservice.ts # MicroService class definition
│ ├── db_logger.ts # Logger utility
│ ├── db_store.ts # Store connection utility
│ └── db_stream.ts # Stream connection utility
├── node_modules/ # Node.js modules
├── package.json # Project metadata and dependencies
├── tsconfig.json # TypeScript configuration
└── README.md # Project documentationConfiguration
The Service class requires a configuration object. This object can include various settings for your microservice. Currently, only the port setting is required.
Example of a configuration object:
const config = {
port: 3000,
};Scripts
The following scripts are defined in the package.json file:
build: Compiles the TypeScript files.start: Runs the compiled JavaScript files.
To run these scripts, use the following commands:
npm run build
npm startLicense
This project is licensed under the MIT License - see the LICENSE file for details.
This README provides usage examples for creating a microservice using the `Service` class, adding middleware, defining routes with handler functions, and starting the microservice. It also includes examples for using each middleware function individually.
This project is licensed under the MIT License.