Skip to content

Instantly share code, notes, and snippets.

View Vatsalya-singhi's full-sized avatar

Vatsalya Singhi Vatsalya-singhi

View GitHub Profile
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class LoggerMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
next(); // Pass control to the next handler
}
}
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
controllers: [UsersController], // Registers the controller
providers: [UsersService], // Registers the service
exports: [UsersService], // Allows other modules to use UsersService
imports: []
})
// Users Service (Provides Business Logic)
import { Injectable } from '@nestjs/common';
// injectables include wide range of configurations such as when and how to initialise the provider
@Injectable()
export class UsersService {
private users = ['Alice', 'Bob', 'Charlie'];
findAll(): string[] {
return this.users;
import { Controller, Get } from '@nestjs/common';
@Controller('users') // Base route: /users
export class UsersController {
@Get() // Handles GET /users
findAll(): string {
return 'This action returns all users';
}
}
// security
// security
app.use(helmet());
app.disable('x-powered-by');
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
limit: 100, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
message: 'Too many requests from this IP, please try again later.',
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
mqtt_sub.js
// When a message is received
client.on('message', (receivedTopic, message) => {
console.log('Received message on topic:', receivedTopic.toString());
// console.log('Message:', message.toString());
// Call Kafka's produceMessage function with received MQTT message
produceKafkaMessage(kafka_topic, message.toString())
.then(() => {
console.log('Message published to Kafka');
@Vatsalya-singhi
Vatsalya-singhi / ui_codebase.ts
Created February 26, 2024 14:58
ui code base
// MQTT PUB CODE
const publish_led_status = (value) => {
// Publish the message to the specified topic
client.publish(topic, String(value), (err) => {
if (!err) {
console.log('Message published:', value);
}
setLedStatus(value);
});
};
public onicecandidateListener(event: RTCPeerConnectionIceEvent, id: string) {
if (event.candidate && event.candidate.sdpMid && event.candidate.sdpMLineIndex) {
this.socket.sendCandidate(id, event.candidate);
} else {
console.log('candidate not sent');
}
}
public candidateListen() {
this.socket.candidateListen()
.pipe(takeWhile(() => this.alive))
.subscribe((data) => {
let id: string = data.id;
let candidate: RTCIceCandidate = new RTCIceCandidate(data.candidate);
if (!this.peerConnections[id] || this.peerConnections[id] == null) {
this.peerConnections[id] = new RTCPeerConnection(this.connectionConfig);
}
public offerListener() {
this.socket.offerListen()
.pipe(takeWhile(() => this.alive))
.subscribe(async (data) => {
// receive user id and peer offer
let id: string = data.id;
let offer: RTCSessionDescription = data.offer;
if (!this.peerConnections[id] || this.peerConnections[id] == null) {
this.peerConnections[id] = new RTCPeerConnection(this.connectionConfig);