Skip to content

Instantly share code, notes, and snippets.

@exupero
exupero / long-polling.coffee
Created May 5, 2011 18:31
CoffeeScript function to create a long-polling server
http = require 'http'
createLongPollingServer = (interval, {getData, returnData}) ->
http.createServer (request, response) ->
poll = ->
data = getData request
return setTimeout poll, interval if not data
response.writeHead 200, 'Content-Type': 'text/plain'
@exupero
exupero / pool.go
Created July 19, 2012 00:59
Passing channels over channels to create a connection pool
package main
import (
"fmt"
"math/rand"
"time"
)
type Request struct {}
@exupero
exupero / README.md
Last active March 29, 2024 21:10
Alias a domain to a local port (Mac)

I run a lot of web servers for different projects, all of them on different ports. Generally I start with port 8000 and increment from there as I spin up new servers, but it became tiresome to remember what projects were running on which ports and what the next available port was.

/etc/hosts won't let you specify a port, but a combination of aliasing 127.0.0.1 to 127.0.0.X, forwarding ports from 8000 to 80, and adding the 127.0.0.X IP under an alias in /etc/hosts did work.

This script finds the next available value of X, aliases it with ifconfig, forwards the given port to port 80 with ipfw, and adds a new entry to /etc/hosts that aliases the IP to the domain you want.

Now I can add a server alias with sudo domain-alias funproject 8000, run the web server at 127.0.0.X:8000, and load up http://funproject/ in my browser.

(Because I needed it to work on a Mac, I couldn't use iptables. pfctl seems to work.)

from contextlib import contextmanager
@contextmanager
def assert_change_by(fn, delta):
original = fn()
yield
final = fn()
assert final - original == delta
@exupero
exupero / whichpull.py
Last active January 13, 2016 16:25
Getting the pull request for a branch with Python and Jq.
import json
import sys
for p in json.load(sys.stdin):
if p['head']['ref'] == sys.argv[1]:
print p['html_url']
#!/bin/bash
domain=$1
port=$2
if [ -z "$domain" ] || [ -z "$port" ]; then
echo "Usage: domain-alias <domain> <port>"
exit 1
fi
@exupero
exupero / README.md
Last active December 20, 2015 19:58
Uncertain Future Projection

A line graph with a projected future that waves to indicate uncertainty in the projection.

@exupero
exupero / README.md
Last active December 21, 2015 03:28 — forked from mbostock/.block
D3-raylabel plugin

Demonstrates the D3 ray-label plugin for labeling slices of pie and donut graphs based on the angle of each slice's centroid.

Label font sizes are scaled (within bounds) based on the size of the slice.

Plugin is hosted on Github.

@exupero
exupero / index.html
Last active December 30, 2015 08:38 — forked from mbostock/.block
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #fcfcfa;
}
.stroke {
fill: none;
@exupero
exupero / git-review
Last active August 29, 2015 14:18
git-review
#!/bin/bash
if [[ -n "$TMUX" ]]; then
tmux send "git diff --cached" C-m \; split-window -v \; send "git commit $* && exit" C-m
else
git commit $*
fi