Skip to content

Instantly share code, notes, and snippets.

View iKunalChhabra's full-sized avatar
🚀
Working on something great !

Kunal Chhabra iKunalChhabra

🚀
Working on something great !
View GitHub Profile
@iKunalChhabra
iKunalChhabra / kotlin_coroutines.kt
Created June 4, 2024 12:15
kotlin_coroutines.kt
package com.kunalchhabra
import kotlinx.coroutines.*
fun main() = runBlocking {
val startTime = System.currentTimeMillis()
// Launch a new coroutine in background and continue
val result1 = async { fetchDataFromSource1() }
val result2 = async { fetchDataFromSource2() }
@iKunalChhabra
iKunalChhabra / index.js
Created November 24, 2024 05:23
WebSocket in Express Node JS
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const PORT = process.env.PORT || 3000;
@iKunalChhabra
iKunalChhabra / index.js
Created November 24, 2024 06:20
ES 6 Style import express app with websocket and data validation
import express from 'express';
import http from 'http';
import { WebSocketServer } from 'ws';
import fetch from 'node-fetch';
import Joi from 'joi';
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
@iKunalChhabra
iKunalChhabra / main.kt
Created November 29, 2024 09:11
Consumer Producer in Kotlin Coroutine using channels
package com.example
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlin.random.Random
fun main(){
val channel = Channel<Int>(2)
runBlocking {
launch { produceValue(channel, 5) }
@iKunalChhabra
iKunalChhabra / main.kt
Created December 9, 2024 10:43
Kotlin connect to Postgres
package org.example
import java.sql.DriverManager
fun main() {
val rows: List<Map<String, Any?>>
DriverManager.getConnection("jdbc:postgresql://localhost:5432/myapp", "myapp", "password").use { connection ->
connection.createStatement().use { stmt ->
stmt.executeQuery("select * from salesforce.account").use { rs ->
rows = generateSequence {
@iKunalChhabra
iKunalChhabra / main.kt
Created December 9, 2024 14:41
JSON Web Token in Kotlin
package org.example
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.security.Keys
import java.time.Duration
import java.util.*
fun main() {
// val secretKey = SIG.HS512.key().build().encoded
val secretKey = "JNu4i@rL����&�#�k\"�\u000Bv0��hn����xH)�\u007FZ�]�&\u0013�]\u0011>8�qB~M�bA�1\u0017Y�U�K"
@iKunalChhabra
iKunalChhabra / main.py
Created April 3, 2025 13:48
FastAPI sever to execute unix commands and stream back the response via websockets
# main.py
import asyncio
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
app = FastAPI()
async def stream_shell_output(command: str, websocket: WebSocket) -> None:
"""
@iKunalChhabra
iKunalChhabra / server_raw.cpp
Created May 1, 2025 18:32
Raw HTTP Sever in c++
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <thread>
#include <vector>
@iKunalChhabra
iKunalChhabra / Makefile
Last active May 5, 2025 16:58
Simple c++ makefile
# Makefile
# '@' added to suppress printing commands on shell
# Variables
CXX = g++
CXXFLAGS = -O3
TARGET = main
SRC = main.cpp
@iKunalChhabra
iKunalChhabra / main.c
Created May 9, 2025 17:23
Create objects in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct person
{
const char* name;
int age;
void (*print)(struct person*);
} person_t;