Skip to content

Instantly share code, notes, and snippets.

@reagent
reagent / .gitignore
Last active July 9, 2024 04:41
TypeSpec Playground
*
@reagent
reagent / cert.rake
Last active June 5, 2024 17:31
Automate SSL Certificate Generation for Your Rails Applications
namespace :cert do
desc 'Generate certificate and keys for the provided hostname'
task :generate, [:hostname] => [:environment] do |_t, args|
hostname = args.fetch(:hostname) { raise 'Missing `hostname`, invoke as cert:generate[<hostname>]' }
cert_path = Rails.root.join('config', 'certs')
FileUtils.mkdir_p(cert_path)
root_key = cert_path.join('root-ca.key')
root_certificate = cert_path.join('root-ca.crt')
@reagent
reagent / vcr_strip_credentials_from_params.rb
Created August 8, 2019 17:38
Example of stripping credentials / sensitive data from GET params when using VCR (5.0.x)
class URLSanitizer
def self.equal?(first, second)
new(first) == new(second)
end
def self.sanitize(url)
new(url).to_s
end
def initialize(url)
@reagent
reagent / migrator.rb
Created November 21, 2017 17:31
DIY migrations in Ruby
require 'pg'
require 'uri'
class Migrator
class Migration
def initialize(connection, path)
@connection, @path = connection, path
end
@reagent
reagent / 00_README.md
Last active August 21, 2024 05:12
Custom HTTP Routing in Go

Custom HTTP Routing in Go

Basic Routing

Responding to requests via simple route matching is built in to Go's net/http standard library package. Just register the path prefixes and callbacks you want invoked and then call the ListenAndServe to have the default request handler invoked on each request. For example:

package main

import (

Installing libcurl with Updated OpenSSL Support

If your Linux distribution is so out of date that you can no longer install updated patches, you'll typically need to either reach for an unsupported package or install the software via source. Additionally, I didn't want to interfere with installed software in fear of rendering the system unusable.

Become Root and Grab Tarballs

You're going to be installing stuff that requires root access, so it's easier to just be able to do it when you want without having to worry about using sudo and permissions. Be careful you don't delete anything you can't recover

$ sudo su - 
#!/usr/bin/env ruby
url = ARGV.pop
if url.to_s.strip.length == 0
puts "Error: Please supply a URL"
exit 1
end
uncompressed_bytes = `curl "#{url}" --silent --write-out "%{size_download}\n" --output /dev/null`
@reagent
reagent / downgrade.sh
Last active October 30, 2019 16:35
Downgrade Apache + PHP on Ubuntu 14.04
cat <<EOF >> /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu precise main restricted universe
deb http://archive.ubuntu.com/ubuntu precise-updates main restricted universe
deb http://security.ubuntu.com/ubuntu precise-security main restricted universe multiverse
EOF
apt-get update
apt-get purge \
apache2 \
@reagent
reagent / whitespace.sh
Last active August 15, 2019 03:27
Convert whitespace from tabs to 2 spaces
#!/bin/bash
set -e # fail on nonzero status
cd $1
files=`find . \
-not \( -path ./vendor -prune \) \
-not \( -path ./tmp -prune \) \
-name '*.html' -o -name '*.js' -o -name '*.coffee' -o \