Skip to content

Instantly share code, notes, and snippets.

View marcuswestin's full-sized avatar

Marcus Westin marcuswestin

  • New York
View GitHub Profile
@marcuswestin
marcuswestin / thread-safe-pool.go
Created September 24, 2013 21:34
A thread-safe concurrent pool of Db connections
type Pool struct {
queue chan *sql.DB
}
// Query with fixed args
func (p *Pool) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
conn := <-p.queue
defer func() { p.queue <- conn }()
// ... use conn.Query to your heart's content
}
@marcuswestin
marcuswestin / bind.go
Created September 20, 2013 17:35
Bind a go function
package bind
import (
"reflect"
)
func Bind(f interface{}, bindArgs ...interface{}) (bound func(...interface{}) interface{}) {
if reflect.TypeOf(f).NumOut() != 1 {
panic("Bind function must return 1 value")
}
@marcuswestin
marcuswestin / gist:5621944
Created May 21, 2013 18:09
Proxy remote requests in a few lines of Obj-C.
// See https://github.com/marcuswestin/webviewproxy
[WebViewProxy handleRequestsWithHost:@"example.proxy" handler:^(NSURLRequest *req, WVPResponse *res) {
NSString* proxyUrl = [req.URL.absoluteString stringByReplacingOccurrencesOfString:@"example.proxy" withString:@"example.com"];
NSURLRequest* proxyReq = [NSURLRequest requestWithURL:[NSURL URLWithString:proxyUrl]];
[NSURLConnection connectionWithRequest:proxyReq delegate:res];
}];
@marcuswestin
marcuswestin / gist:5605389
Last active December 17, 2015 11:49
Exploration of object type detection by equality testing of member methods (notably toString).
function isArray(obj) {
return obj && obj.toString == Array.prototype.toString
}
function isNumber(obj) {
return obj && obj.toString == Number.prototype.toString
}
function isString(obj) {
return obj && obj.toString == String.prototype.toString
}
function isArguments(obj) {
@marcuswestin
marcuswestin / gist:5267401
Last active August 18, 2023 23:00
Setup a new machine
SETUP NEW MAC
=============
Use https://github.com/marcuswestin/machine
# gitup
curl https://raw.github.com/gist/3375528/gitup.sh > /tmp/gitup.sh
chmod +x /tmp/gitup.sh
mv /tmp/gitup.sh /usr/local/bin/gitup
# gitupn_push
curl https://raw.github.com/gist/3375528/gitupn_push.sh > /tmp/gitupn_push.sh
chmod +x /tmp/gitupn_push.sh
mv /tmp/gitupn_push.sh /usr/local/bin/gitupn_push
@marcuswestin
marcuswestin / ios getIpAddress
Created February 10, 2013 03:13
get IP address of iOS device
static NSString* getAddress() {
id myhost =[NSClassFromString(@"NSHost") performSelector:@selector(currentHost)];
if (myhost) {
for (NSString* address in [myhost performSelector:@selector(addresses)]) {
if ([address rangeOfString:@"::"].location == NSNotFound) {
return address;
}
}
}
@marcuswestin
marcuswestin / encrypt secrets
Created February 9, 2013 01:34
Short makefile commands to keep dev and prod secrets encrypted in your repo.
# Assumes a folder "secrets/" with "dev/" and "prod/" inside it
# Also, make sure "secrets/dev" and "secrets/prod" are in your .gitignore file.
# But do add secrets/prod.tar.bfe and secrets/dev.tar.bfe to your repo
run-dev: secrets/dev
echo "Run dev with secrets/dev/*"
run-prod: secrets/prod
echo "Run prod with secrets/prod/*"
@marcuswestin
marcuswestin / aws-ssl-termination-digicert.md
Last active August 28, 2018 15:18
How to set up an AWS SSL terminating Elastic Load Balancer with a Digicert certificate

1: Generate CSR

openssl req -new -newkey rsa:2048 -nodes -keyout server-cert.key -out server-cert-sign-req.csr

# Country Name (2 letter code) [AU]:US
# State or Province Name (full name) [Some-State]:California
# Locality Name (eg, city) []:
# Organization Name (eg, company) [Internet Widgits Pty Ltd]:Flutterby Labs, Inc.
# Organizational Unit Name (eg, section) []:
# Common Name (eg, YOUR name) []:www.dogo.co
@marcuswestin
marcuswestin / openApp.js
Last active December 12, 2015 06:08
Open an app from a mobile webpage, with a callback for devices which do not have the app installed.
// openApp('myapp://foo/bar?qwe=cat', function noApp() { alert('You should install myapp!') })
function openApp(appUrl, noAppCallback) {
var isAndroid = /Android/.test(window.navigator.userAgent)
var delayBeforeCheck = isAndroid ? 500 : 1000
var acceptableElapse = isAndroid ? 100 : 500
var startTime = new Date().getTime()
setTimeout(_attemptOpen, 50)
function _attemptOpen() {
var iframe = document.createElement("iframe")