Skip to content

Instantly share code, notes, and snippets.

View andykent's full-sized avatar

Andy Kent andykent

View GitHub Profile
# Lightweight Rack middleware to ease configuring cache headers
#
# Sinatra usage example
#
# configure do
# Rack::CacheHeaders.configure do |cache|
# cache.max_age(/^\/$/, 60)
# cache.max_age(/^\/search$/, 3600)
# cache.expires(/^\/about\/.+$/, "00:00")
# cache.private(/^\/account/)
@andykent
andykent / forward.coffee
Created July 3, 2010 17:39
Super simple, round robin, TCP port forwarding with node.js
net: require "net"
sys: require "sys"
LISTEN_PORT: 10000
SERVER_ADDRESS: '127.0.0.1'
AVAILABLE_PORTS: [10001, 10002, 10003, 10004, 10005, 10006]
CONNECTION_COUNTER: 0
reservePort: (callback) ->
if AVAILABLE_PORTS.length > 0
reservePort = function(callback) {
var tickCheck = function() {
if(AVAILABLE_PORTS.length > 0) {
callback(AVAILABLE_PORTS.shift())
} else {
process.nextTick(tickCheck)
}
}
tickCheck();
}
@andykent
andykent / read.js
Created November 5, 2010 16:34
Test Case that shows a high number of writes getting mangled along with a file to scan over and validate the data.
var fs = require('fs');
var lines = fs.readFileSync('output.json', 'utf8').split("\n");
lines.pop(); // remove the last (empty) new line
for(var n in lines) {
try {
JSON.parse(lines[n]);
} catch (e) {
console.log("Error at line " + (n+1) + " => " + lines[n]);
}
}
@andykent
andykent / mongoid.rb
Created January 25, 2011 11:14
Demonstrates a bug in mongoid many-to-many associations with string keys
class User
include Mongoid::Document
field :name
references_and_referenced_in_many :accounts
end
class Account
include Mongoid::Document
field :name
key :name
@andykent
andykent / gist:925458
Created April 18, 2011 14:31
Nginx Upstart
description "nginx http daemon"
start on runlevel [2]
stop on runlevel [016]
console owner
exec /opt/nginx/sbin/nginx -g "daemon off;"
respawn
@andykent
andykent / alert.js
Created May 24, 2011 16:46
Simple example showing advantages of async JS loading
alert('Hello from alert.js');
@andykent
andykent / connect.rb
Created July 2, 2012 10:30
A Small TCP Echo Server
require "socket"
s = TCPSocket.new 'localhost', 1234
s.puts "name: Andy"
s.puts "Hello World"
@andykent
andykent / gist:3048069
Created July 4, 2012 16:06
Basic Canvas Drawing
// add some interactivity
var canvas = document.querySelector("#canvas"),
ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.lineWidth = 10;
# name: Echo Bot
# description: Say someting and I'll say it back
# keyword: echo
bolt.run ->
if command.hasQuery
result(title:command.query, description: "That's what you said!")