Skip to content

Instantly share code, notes, and snippets.

View peacefixation's full-sized avatar

Matt Jarvis peacefixation

  • Melbourne, Australia
View GitHub Profile
@peacefixation
peacefixation / Makefile
Last active July 18, 2019 00:26
Docker commands
# https://docs.docker.com/engine/reference/commandline/docker/
name = container_name
dbname = db_name
version = 0.1
# build the container (with name:tag)
build:
docker build -t $(name):$(version) .
@peacefixation
peacefixation / scale.js
Last active March 27, 2019 01:34
Scale a number to a new range
// https://stats.stackexchange.com/questions/281162/scale-a-number-between-a-range
function scale(n, oldMin, oldMax, newMin, newMax) {
return ((n - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin;
}
@peacefixation
peacefixation / lerp-rgb.js
Last active August 1, 2023 10:01
Linear interpolate RGB colors
// interpolate between colors { r, g, b } where 0 < t < 1
// when assigning a color for a number in a range larger than 0-1, scale the number to the 0-1 range first
function lerpRGB(color1, color2, t) {
let color = {};
color.r = color1.r + ((color2.r - color1.r) * t);
color.g = color1.g + ((color2.g - color1.g) * t);
color.b = color1.b + ((color2.b - color1.b) * t);
return color;
}
@peacefixation
peacefixation / hipchat.sh
Last active March 6, 2019 23:47
Send a hipchat alert
#!/bin/bash
# https://www.hipchat.com/docs/apiv2/method/send_room_notification
AUTH_TOKEN="" # generate an auth token from the hipchat admin interface
ROOM_ID=1 # get the room id from the hipchat admin interface
HIPCHAT_URL="https://api.hipchat.com/v2/room" # or self hosted hiphat URL
FROM="some sender"
MESSAGE="some message"
@peacefixation
peacefixation / munki.sh
Created February 8, 2019 04:16
Setup a Munki repo on Debian with Apache WebDAV
# https://github.com/munki/munkiwebadmin/wiki/MunkiWebAdmin-Ubuntu-14.04-LTS-Setup
echo Installing Apache
/usr/bin/apt-get install -y apache2
echo Creating Munki repo in Apache web root
/bin/mkdir -p /var/www/munki/pkgsinfo \
/var/www/munki/catalogs \
/var/www/munki/manifests \
/var/www/munki/pkgs \
@peacefixation
peacefixation / arduino.txt
Last active March 6, 2019 23:43
Set up Arduino development environment
Arduino IDE:
- download from https://www.arduino.cc/en/Main/Donate
USB -> Serial drivers:
- download CH340G Driver from https://wiki.wemos.cc/downloads
- download CP210x Driver from https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
Open Arduino IDE
> Preferences:
@peacefixation
peacefixation / Dockerfile
Created March 6, 2019 23:41
Debian Postgresql container
FROM debian:stretch
LABEL maintainer="First Last <email@somewhere.com>"
ARG POSTGRES_VER=9.6
ARG TZ=Australia/Melbourne
ARG DEBIAN_FRONTEND=noninteractive
# Ensure Timezone is correct
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
@peacefixation
peacefixation / middleware.go
Last active March 27, 2019 01:34
HTTP middleware
import "github.com/julienschmidt/httprouter"
// usage
// router.POST("/path", BasicAuth(someRequestHandler))
// BasicAuth middleware for incoming requests that must include basic authentication
func BasicAuth(handler httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
log.Printf("Authorizing request from %s", r.RemoteAddr)
@peacefixation
peacefixation / subnet.go
Last active March 27, 2019 01:33
Subnet contains IP
// SubnetContainsIP check if an IP address is part of the subnet given by the CIDR (ie. '192.168.0.0/24')
func SubnetContainsIP(CIDR, addr string) (bool, error) {
_, ipNet, err := net.ParseCIDR(CIDR)
if err != nil {
return false, fmt.Errorf("Error parsing CIDR '%s'", CIDR)
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return false, fmt.Errorf("Error parsing address '%s'. %v", addr, err)
@peacefixation
peacefixation / json-marshal.go
Last active March 26, 2019 02:21
Implement MarshalJSON and UnmarshalJSON on a custom type
import (
"fmt"
"strings"
"time"
)
const timeFormat = "2006-01-02 15:04:05"
// JSONTime a wrapper for time.Time that unmarshalls with a custom date format
type JSONTime struct {