microgate
v0.0.5
Published
**MicroGate** is a lightweight RPC gateway built on Redis Streams for scalable, asynchronous, and distributed request handling between services.
Maintainers
Readme
📦 MicroGate
MicroGate is a lightweight RPC gateway built on Redis Streams for scalable, asynchronous, and distributed request handling between services.
🚀 Features
- Distributed architecture ("controller → worker" model)
- Supports multiple parallel controllers and workers
- Built-in event-based routing (
event) - Safe response delivery via per-request
uuidstreams - Minimal dependencies (only
redis@^5.5.6)
📦 Installation
npm install microgate🧠 Example
Configuration
export default {
version: '1.0.0',
connection: {
host: 'localhost',
password: '',
port: 6379
}
};Worker
import { RedisWorker } from 'microgate';
import config from './config'
import jwt from 'jsonwebtoken';
const worker = new RedisWorker(config, 'auth');
worker.on('signup', async (message) => {
// ... do register
const token = jwt.sign({
username: message.username
}, SECRET_KEY);
return {
token
};
});
worker.on('signin', async (message) => {
const token = message.token;
try {
return {
success: jwt.verify(token, config.SECRET_KEY)
}
} catch (error) {
return {
success: false
}
}
});
worker.init();Controller
import express from 'express';
import BodyParser from 'body-parser';
const authController = new RedisController(config, 'auth');
const app = express();
app.post('/auth/signup', async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
res.send('Invalid credentials');
return;
}
try {
const authResponse = await authController.request('signup', {
username, password
});
if (authResponse.error) {
res.send(authResponse.error.message);
return;
}
res.send({
token: authResponse.token
});
} catch (error) {
res.send((error as Error).message);
}
});
app.use('/api', async (req, res, next) => {
// ... Extract token
const authResponse = await authController.request('signin', {
token
});
if (!authResponse.success) {
res.statusCode = 401;
res.send('Invalid token');
return;
}
next();
});
app.listen(8080);