Skip to content

Instantly share code, notes, and snippets.

@chinmay185
chinmay185 / rest-api-design-with-otp-based-auth-example.md
Last active January 29, 2024 15:36
REST API design example - OTP based auth

Problem statement:

Design the auth flow apis (signup, login, etc) for an OTP based user authentication. Assume OTP will be created and presented to users upon a successful login.

API Signature:

- URL
- Request Method
- Request Headers
package grep
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
@chinmay185
chinmay185 / grep_parallel.go
Last active April 14, 2017 16:02
basic simulation of grep command using multiple goroutines (for tests, see https://gist.github.com/chinmay185/15e0a81deee73571ce9b74d65d1da7eb)
package grep
import (
"bufio"
"flag"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
@chinmay185
chinmay185 / josephus_using_channels_goroutines.go
Created March 25, 2017 11:48
Simulates Josephus problem solution using Goroutines and Channels
package main
import (
"fmt"
"time"
)
type Command struct {
message string
senderId int
@chinmay185
chinmay185 / gogrep.go
Created March 25, 2017 09:31
Grep like command implementation in Go
package main
import (
"io/ioutil"
"os"
"log"
"bufio"
"fmt"
"strings"
"path/filepath"
@chinmay185
chinmay185 / async-map-reduce.js
Last active August 29, 2015 14:20
Demonstration of async map reduce using promises
var Promise = require("bluebird");
var fileParts = ["http://myfiles.com/file1/part1",
"http://myfiles.com/file1/part2",
"http://myfiles.com/file1/part3"];
var fetchPart = function(partUrl) {
// returns the promise of the part of a file
};
var filePartPromises = fileParts.map(fetchPart);
@chinmay185
chinmay185 / parallel-async.js
Created April 27, 2015 15:46
using map to start multiple async operations at the same time
var Promise = require("bluebird");
var productIds = ["productId1", "productId2", "productId3"];
var getProductsFromDb = function(productId) {
// returns a promise of product
}
var productPromises = productIds.map(getProductFromDb);
Promise.all(productPromises).then(function(products) {
@chinmay185
chinmay185 / sequential-async.js
Last active August 29, 2015 14:19
Run async operation for each value in a collection where each async operation is executed only when the previous one completes.
var Promise = require("bluebird");
var apiUrls = ["url1", "url2", "url3"];
var request = function(url) {
// request the rate limited api (returns a promise)
};
var saveResponse = function(response) {
// save response to file/db (returns a promise)
};
@chinmay185
chinmay185 / deferred-fixed.js
Last active August 29, 2015 14:19
getting rid of unnecessary deferred
var getUserEmail = function (username) {
return userRepository.find(username).then(function (user) {
return user.email;
});
};
getUserEmail("emma").then(console.log);
@chinmay185
chinmay185 / deferred-abused.js
Last active August 29, 2015 14:19
unnecessary use of deferred.
var getUserEmail = function(username) {
var deferred = q.defer();
userRepository.find(username) // assume this returns a promise
.then(function(user){
deferred.resolve(user.email);
});
return deferred.promise;
};
getUserEmail("emma").then(console.log);