Wednesday, July 24, 2019

Freshly Is it worth it? How good is freshly

Been living in NYC for few years and with city life
it’s hard to find time to cook sometimes

Buying groceries costs me more $$ since by the time
I decide to cook, groceries are already bad

Recently I came across Freshly! I was a bit skeptical
in the beginning about how good food in a box can be
that I need to microwave but I was surprised after eating
for a week. Meals were delicious, low carb, high in protein

Of course some meals are better than the others
but overall, it is definitely worth trying and there are plenty
of options to choose from

I get 6 meals every week for me and my wife and it works
really well . You can easily skip week/s which plays well if you
don’t want to eat Freshly every day

Use this link and you will get $20 off for your first two deliveries
and I will get the same. Win win!
http://refer.freshly.com/s/Atul26

How much does it cost?

More meals per week the less it is.  No brainer









Sunday, February 28, 2016

How to figure out static dependencies of a library for example libxml-2.0

Alright, this is fairly common question when you are trying to generate a static binary or have come across situations when you want to find out all the dependencies of a library.

Example:
The problem: How the hell do I figure out all the static dependencies of libxml-2 ?

The solution: Its easy, use pkg-config !!

Here is how to list all the static dependencies of libxml-2

Run the below command:

pkg-config --static --libs libxml-2.0

and this returns a list

-lxml2 -lpthread -lz -lm

Saturday, February 27, 2016

All permutation of a string using Go (Golang)

I have been playing around a bit with Golang and I thought the best way to learn about a prog. language is to write some code.

The problem: Write all permutations of a given string

The solution:

Run code here : http://play.golang.org/p/h4Kx6IS_lE

package main
import "fmt"
/*
Return all permutations of a string
Example,
Find all perm of abc:
We will find all perms of the given string based on all perms of its substring
To find all perms of abc, we need to find all perms of bc, and then add a to those perms
To find all perms of bc, we need to find all perms of c, and add b to those perms
To find all perms of c, well we know that is only c
Now we can find all perms of bc, insert c at every available space in b
bc --> bc, cb
Now we need to add a to all perms of bc
abc, bac, bca (insert a in bc) -- acb, cab, cba (insert a in cb)
*/
func main() {
fmt.Printf("All perms are - %v", getPerms("abc"))
}
func getPerms(str string) []string {
// base case, for one char, all perms are [char]
if len(str) == 1 {
return []string{str}
}
current := str[0:1] // current char
remStr := str[1:] // remaining string
perms := getPerms(remStr) // get perms for remaining string
allPerms := make([]string, 0) // array to hold all perms of the string based on perms of substring
// for every perm in the perms of substring
for _, perm := range perms {
// add current char at every possible position
for i := 0; i <= len(perm); i++ {
newPerm := insertAt(i, current, perm)
allPerms = append(allPerms, newPerm)
}
}
return allPerms
}
// Insert a char in a word
func insertAt(i int, char string, perm string) string {
start := perm[0:i]
end := perm[i:len(perm)]
return start + char + end
}
view raw permutations.go hosted with ❤ by GitHub



Saturday, January 16, 2016

Dockerizing Sinatra app with reloading (Docker + Sinatra + foreman + rerun)

Lately I have been trying to learn more about Docker, so I picked up a task to dockerize a Sinatra app.

Here is an excellent training material which includes 3 hours of video about docker:
https://training.docker.com/self-paced-training

Here is another introduction to docker by Codeship:
http://blog.codeship.com/using-docker-for-rails-development/

Assumptions:
  1. You have a Sinatra app up and running using foreman.
  2. You have docker machine installed. We will be using docker compose to dockerize

Lets get started:

Step 1: Create a file named Dockerfile in the root of your project with the following content

FROM ruby:2.2.0
RUN mkdir -p /var/app
COPY Gemfile* /var/app/
WORKDIR /var/app
ENV environment=development
RUN bundle install

Step 2: Create a file named docker-compose.yml in the root of your project with the following content

+web:
+ build: .
+ ports:
+ - '9292:9292'
+ volumes:
+ - '.:/var/app'
+ tty: true
+ command: ["rerun", "--force-polling", "foreman", "start"]


Step 3: Lets get reloading Sinatra app on changes working (ONLY if you want to reload your Sinatra app on changes using rerun gem)

At the moment to get rerun to work properly with Docker you have to supply --force-polling option
as mentioned here
https://github.com/alexch/rerun#vagrant-and-virtualbox

The catch is that the supporting code is only in master(as of now) and is not pushed to the latest gem.
So chances are if you install the latest version of rerun gem, you might not get the force-polling option 

use this in your gemfile development section to load the master branch of the gem
gem 'rerun', :git => 'https://github.com/alexch/rerun.git'
gem 'foreman'

Step 4: And thats all the config thats needed. Run
    a) docker-compose build
    b) docker-compose up


Saturday, April 4, 2015

Ever wondered how good is a restaurants kitchen ?
Wana check how good a restaurant is for real in NYC. ?

NYC Restaurants Grades is the place to go. Checkout http://www.nycrestaurantsgrades.com/

Check most recent NYC restaurant inspections and grades. You can also see past NYC restaurants inspections results and grades.

Website - http://www.nycrestaurantsgrades.com/




Saturday, September 6, 2014

Binary Tree using HTML5 Canvas and JavaScript

Hi Guys -

I was just playing around with HTML5 Canvas and thought it will be fun to create a binary tree implementation using JavaScript.

I tried this a few years ago with Java and applets. Well, yeah u guessed it, it was a PITA.

With JavaScript and Canvas, it was a smooth ride.

The code below is only for the purpose of demo. Some nodes might overlap if the tree has a lot
of nodes.

Tested on Google Chrome

1 - Create a file index.html and add the below code to it.
      It contains an
      a - Input box where the user will enter the number to be added
      b - A button to add entered number
      c - A canvas element
      d - A script tag

<!DOCTYPE html>
<html>
<head>
<style>
.center {margin: auto; width: 50%;}
<style>
</head>
<body>
<div class='input-box'>
<input id='tree-input' type='number' placeholder='Enter number'>
<button id='add-to-tree' onclick="addToTree()">Add To Tree</button>
</div>
<div class="tree center">
<canvas id='my-canvas' width="400" height="400">
Your browser doesnot support canvas
</canvas>
</div>
<script>
</script>
</body>
</html>
view raw index.html hosted with ❤ by GitHub

     
2 - Next we will add a node object. Node, as the name suggests, represent a node in the tree
     Add the below code in the script tag of the index.html

// Represents the node in the tree. Will be displayed as a small circle in the browser.
// x, y --> x, y co-ordinates of the center of circle
// r --> radius of the circle
// ctx --> context of the canvas
// data --> data to be displayed (Only number)
var Node = function(x,y,r, ctx, data) {
// left child of a node
this.leftNode = null;
// right child of a node
this.rightNode = null;
// draw function. Responsible for drawing the node
this.draw = function() {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2*Math.PI);
ctx.stroke();
ctx.closePath();
ctx.strokeText(data, x, y);
};
// Simple getters
this.getData = function() { return data; };
this.getX = function() { return x; };
this.getY = function() { return y; };
this.getRadius = function() { return r; };
// Returns coordinate for the left child
// Go back 3 times radius in x axis and
// go down 3 times radius in y axis
this.leftCoordinate = function() {
return {cx: (x - (3*r)), cy: (y + (3*r))}
};
// Same concept as above but for right child
this.rightCoordinate = function() {
return {cx: (x + (3*r)), cy: (y+(3*r))}
};
};
view raw node.js hosted with ❤ by GitHub
   

3 - Next we will add code to draw a line from parent to child. Add the below code to the script tag    
   
// Draws a line from one circle(node) to another circle (node)
var Line = function() {
// Takes
// x,y - starting x,y coordinate
// toX, toY - ending x,y coordinate
this.draw = function(x, y, toX, toY, r, ctx) {
var moveToX = x;
var moveToY = y + r;
var lineToX = toX;
var lineToY = toY - r;
ctx.beginPath();
ctx.moveTo(moveToX, moveToY);
ctx.lineTo(lineToX, lineToY);
ctx.stroke();
};
};
view raw line.js hosted with ❤ by GitHub



4 - Next we add the core logic of a binary tree. To understand that logic, you should know how
     a binary tree works. Add the blow code the script tag

   
// Represents the btree logic
var BTree = function() {
var c = document.getElementById('my-canvas');
var ctx = c.getContext('2d');
var line = new Line();
this.root = null;
var self = this;
// Getter for root
this.getRoot = function() { return this.root; };
// Adds element to the tree
this.add = function( data) {
// If root exists, then recursively find the place to add the new node
if(this.root) {
this.recursiveAddNode(this.root, null, null, data);
} else {
// If not, the add the element as a root
this.root = this.addAndDisplayNode(200, 20, 15, ctx, data);
return;
}
};
// Recurively traverse the tree and find the place to add the node
this.recursiveAddNode = function(node, prevNode, coordinateCallback, data) {
if(!node) {
// This is either node.leftCoordinate or node.rightCoordinate
var xy = coordinateCallback();
var newNode = this.addAndDisplayNode(xy.cx, xy.cy, 15, ctx, data);
line.draw(prevNode.getX(), prevNode.getY(), xy.cx, xy.cy, prevNode.getRadius(), ctx)
return newNode;
}
else {
if(data <= node.getData()) {
node.left = this.recursiveAddNode(node.left, node, node.leftCoordinate, data);
}
else {
node.right = this.recursiveAddNode(node.right, node, node.rightCoordinate, data);
}
return node;
}
};
// Adds the node to the tree and calls the draw function
this.addAndDisplayNode = function(x, y, r, ctx, data) {
var node = new Node(x, y, r, ctx, data);
node.draw();
return node;
};
};
view raw btree.js hosted with ❤ by GitHub


5 - Now we  need to create a binary tree object to which we can add node
 
     Add the below code to the script tag

     var btree = new BTree();

6 - Ok. We are almost done. We just need to wire the input box and add to tree button
     Add the below code to the script tag

var addToTree = function() {
input = document.getElementById('tree-input');
value = parseInt(input.value);
if(value)
btree.add(value);
else
alert("Wrong input");
};
view raw addToTree.js hosted with ❤ by GitHub

    

Thursday, August 21, 2014

Elasticsearch 101 - How secure is it ?

So, I wanted to add full text search functionality to one of my projects and after doing some research I decided to go with Elasticsearch.

While reading the docs, I came across two RESTful apis which were good but also dangerously exploitable.

So what are those apis - (assuming that a local instance is running on port - 9200 )

1 - curl XGET 'http://localhost:9200/_nodes?pretty=true'
   
     Run this command from your terminal and you will see a lot of details about your machine

  • Paths - 
    • logs: /Users/I/am/a/hacker/logs
    • home: /Users/I/can/see/your/home
  • OS info -
    • cpu details
    • memory details
    • and yeah JVM details
  • Network info - 
    • IP address
    • mac address

2 - curl -XPOST 'http://localhost:9200/_cluster/nodes/_master/_shutdown'   or
     curl -XPOST 'http://localhost:9200/_shutdown'

     Any user can execute the above command and bring down your whole cluster. 
     Still most big companies have pretty good firewall setup so its hard to get access to a machine but
     still, imo, there should be some kind of permission to execute these commands


Anyways these are some interesting things which I came across while reading the docs.