Skip to content

Instantly share code, notes, and snippets.

View michaeltelford's full-sized avatar

Michael Telford michaeltelford

View GitHub Profile
@michaeltelford
michaeltelford / docker-compose.yml
Last active February 25, 2025 09:06
Docker compose example for HTTP API and Postgres (with correct port mapping/forwarding)
services:
postgres:
container_name: postgres
# Custom postgres image contains the migration SQL needed to build the schema
image: custom-postgres:latest
environment:
PGUSER: foobar
POSTGRES_PASSWORD: foobar
POSTGRES_USER: foobar
POSTGRES_DB: foobar
@michaeltelford
michaeltelford / tmux.txt
Last active February 10, 2025 18:47
Linux tmux cheatsheet
# Linux tmux is used as a session manager to persist processes started from a SSH connection (which are killed
# by default on disconnect).
# Start a tmux session, then a daemon process and exit the session (which keeps the daemon running):
# ssh -i <foobar>.pem <ip-address>:22
tmux
# start a process with <cmd &> to start a daemon
Ctrl+B and then D # to exit the session
@michaeltelford
michaeltelford / build.sh
Created December 17, 2024 11:25
Go build with OS and ARCH params
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ...
@michaeltelford
michaeltelford / main.go
Last active December 19, 2024 15:36
Linked List Combine & Sort (Golang)
package main
import (
"fmt"
"sort"
)
type LinkedNode struct {
Val int
Next *LinkedNode
@michaeltelford
michaeltelford / config.ru
Last active November 14, 2024 09:45
Rack middleware for handling CORS requests
require 'rack/protection'
require_relative './cors_handler'
# require_relative './api'
# Add any required ENV vars to this array
def env_vars_present?
[
"RACK_ENV",
"PORT",
"CLIENT_ORIGIN"
@michaeltelford
michaeltelford / event_tracker.py
Last active November 7, 2024 16:41
Python EventTracker class for recording events (like HTTP requests) with a TTL to determine when a threshold has been passed
# Records events for a given key e.g. ShareID in the form of:
#
# {
# key1: [timestamp1, timestamp2],
# key2: [timestamp3, timestamp4]
# }
#
# Then tells you when a given threshold/limit has been exceeded.
# Starts a background thread to evict events when their TTL expires.
@michaeltelford
michaeltelford / caBundle.sh
Last active January 21, 2025 10:42
HTTPS (TLS) CA and Server Cert Generation
#!/bin/bash
WEBHOOK_YAML_FILE="./path_to_webhook_yaml"
WEBHOOK_KEY="TODO_SET_CA_BUNDLE"
echo "Generating base64 encoded myCA.pem and setting in webhook YAML"
# Check $WEBHOOK_YAML_FILE file exists
if [ ! -e $WEBHOOK_YAML_FILE ]; then
echo "Unable to find ${WEBHOOK_YAML_FILE} file, has it been moved or renamed?"
@michaeltelford
michaeltelford / fizz_buzz.cr
Created December 20, 2022 10:50
FizzBuzz solution written in Crystal
def multiple_of?(multiple : Int32, i : Int32) : Bool
(i % multiple) == 0
end
def get_fizz_buzz_str(i : Int32) : String
if multiple_of?(3, i) && multiple_of?(5, i)
"FizzBuzz"
elsif multiple_of?(3, i)
"Fizz"
elsif multiple_of?(5, i)
@michaeltelford
michaeltelford / main.go
Created September 30, 2022 12:39
Goroutine with timeout (using a context and channel)
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx := context.Background()
@michaeltelford
michaeltelford / extract.rb
Last active July 23, 2025 17:02
Ruby script using Wgit to extract the meaningful content from a webpage (without the crap e.g. cookie banners)
require "wgit"
# Remove the default extractors since we won't be using them.
Wgit::Document.remove_extractors
# The default name of the output file containing the clean HTML.
def default_file_name
"webpage.html"
end