Skip to content

Instantly share code, notes, and snippets.

@dougalcampbell
dougalcampbell / phpfpm-mon.cron
Created Feb 14, 2012
Crontab entry to monitor for php-fpm problems
View phpfpm-mon.cron
## Auto-restart PHP when it's returning errors
#
# Make sure that http://localhost/test.php is an actual PHP script. If it starts returning
# 500 errors, restart the PHP-FPM service
* * * * * /usr/bin/curl --head -sf http://localhost/test.php -o /dev/null || /usr/sbin/service php5-fpm restart
@dougalcampbell
dougalcampbell / utfluv.js
Created Mar 12, 2012
Handle invalid JS character sequences
View utfluv.js
/**
* encode to handle invalid UTF
*
* If Chrome tells you "Could not decode a text frame as UTF-8" when you try sending
* data from nodejs, try using these functions to encode/decode your JSON objects.
*
* see discussion here: http://code.google.com/p/v8/issues/detail?id=761#c8
* see also, for browsers that don't have native JSON: https://github.com/douglascrockford/JSON-js
*
* Any time you need to send data between client and server (or vice versa), encode before sending,
@dougalcampbell
dougalcampbell / NeoPixelTest.ino
Created Oct 31, 2013
Example of driving an Adafruit NeoPixel Ring with the Digispark Arduino-compatible board
View NeoPixelTest.ino
#include <Adafruit_NeoPixel.h>
#define PIN 1
#define STRIPSIZE 16
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
@dougalcampbell
dougalcampbell / .bashrc
Created Feb 15, 2016
Set terminal title in bash
View .bashrc
# set title of current terminal
function _set_term_title() {
echo -ne "\033]0;${1}\007"
}
alias trmtitle=_set_term_title
@dougalcampbell
dougalcampbell / keybase.md
Last active Sep 20, 2019
Keybase verification
View keybase.md

Keybase proof

I hereby claim:

  • I am dougalcampbell on github.
  • I am dougalcampbell (https://keybase.io/dougalcampbell) on keybase.
  • I have a public key whose fingerprint is 9692 F34E 074B 52B9 919F 9180 A9A9 336D F0C4 0C65

To claim this, I am signing this object:

@dougalcampbell
dougalcampbell / ds-duino.ino
Created Sep 10, 2013
Digispark and nodejs - talking to the Digispark Arduino-compatible microcontroller via USB with the node-hid library
View ds-duino.ino
/*
* Accept control commands via USB.
*
* Commands start with '!' and end with '.'
* Commands have three parts: action, pin, value: !AAPPVV.
*
* E.g. '!01p101.' is DigitalWrite, Pin1, value = 1
*
* Note: This is currently *very* crude. Much improvement could be made.
* I think the use of strncpy is eating a lot of memory. Refactor?
View opengraphbookmarklet.js
javascript:$=jQuery;if(0!==$("#myogdiv").length)$("#myogdiv").hide().remove();else{var%20myogdiv=$('<div%20id="myogdiv"/>');$("body").append(myogdiv);$('head%20meta[property^="og:"],head%20meta[property^="fb:"],head%20meta[name^="twitter:"]').each(function(d,b){var%20a=$(b).attr("property"),c=$(b).attr("content");void%200==a&&(a=$(b).attr("name"));$(myogdiv).append("<div><b>"+a+":</b>"+c+"</div>");"og:image"==a&&$(myogdiv).append('<br/><img%20src="'+c+'"%20style="max-width:250px"/>');"twitter:image"==a&&$(myogdiv).append('<br/><img%20src="'+c+'"%20style="max-width:250px"/>')});$(myogdiv).css("position","absolute").css("top","0").css("zIndex",9999999).css("padding","0.5em").css("border","1px%20solid%20red").css("backgroundColor","white")}void%200;
@dougalcampbell
dougalcampbell / haproxy.cfg
Created Jun 15, 2018
Let's Encrypt renewal with haproxy
View haproxy.cfg
# The key bit is the 'bind' statement in the frontend
frontend https-in
# match the filename here to your $HAPCERTFILE
bind 10.0.0.1:443 ssl crt /etc/haproxy/certs/combined.pem
reqadd X-Forwarded-Proto:\ https
# acl, use_backend, and other statements...
acl srv_host_1 hdr(host) -i mydomain.com
@dougalcampbell
dougalcampbell / tabifytextareas.js
Created Nov 12, 2013
Bookmarklet: allow tabs in textareas (requires jQuery) Create a toolbar bookmark with this code. Click it when you want to be able to use tabs in a textarea.
View tabifytextareas.js
javascript:$(document).delegate('textarea', 'keydown', function(e) { var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); var start = $(this).get(0).selectionStart; var end = $(this).get(0).selectionEnd; $(this).val($(this).val().substring(0, start) + "\t" + $(this).val().substring(end)); $(this).get(0).selectionStart = $(this).get(0).selectionEnd = start + 1; }});void(0);
@dougalcampbell
dougalcampbell / toType.js
Created Dec 10, 2013
Really get an object's type in JavaScript
View toType.js
function toType(obj) {
var systype = ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1];
if (systype === "Object") {
/**
* See if this is a custom user type, a la:
* function Foo() {}
* foo = new Foo();
* toType(foo); // returns "Foo"
* toType(Foo); // returns "Function"
*/
You can’t perform that action at this time.