Skip to content

Instantly share code, notes, and snippets.

View elithrar's full-sized avatar
🌐
Helping make the Internet better.

Matt Silverlock elithrar

🌐
Helping make the Internet better.
View GitHub Profile
require 'bundler/setup'
require 'sinatra/base'
require 'rack/contrib'
# The project root directory
$root = ::File.dirname(__FILE__)
class SinatraStaticServer < Sinatra::Base
get(/.+/) do
@elithrar
elithrar / use.go
Last active December 29, 2015 02:29
use.go — the little HTTP middleware helper that could.
// use provides a cleaner interface for chaining middleware for single routes.
// Middleware functions are simple HTTP handlers (w http.ResponseWriter, r *http.Request)
//
// r.HandleFunc("/login", use(loginHandler, rateLimit, csrf))
// r.HandleFunc("/form", use(formHandler, csrf))
// r.HandleFunc("/about", aboutHandler)
func use(h http.HandlerFunc, middleware ...func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
for _, m := range middleware {
h = m(h)
}
@elithrar
elithrar / stripe-config.js
Created November 27, 2013 00:40
Example Stripe Checkout - Custom Integration for #stripe
// Disable the payment/submit button until everything has loaded
// (if Stripe fails to load, we can't progress anyway)
$(document).ready(function() {
$("#payment-button").prop('disabled', false)
})
var handler = StripeCheckout.configure("customButtonA", {
key: '<yourpublishablekey',
token: function(token, args){
var $input = $('<input type=hidden name=stripeToken />').val(token.id);
@elithrar
elithrar / ratelimit.go
Last active February 17, 2016 06:14
Quick and dirty HTTP request rate limiter (will eventually wrap this into a package!)
package main
import (
"errors"
"github.com/garyburd/redigo/redis"
"net/http"
"time"
)
type RateStore struct {
@elithrar
elithrar / wale_postgres_recovery.md
Last active May 3, 2021 15:38
WAL-E + Postgres 9.x (single server + DB) Setup and Recovery

A quick "how to" on what you need to do to both setup AND recover a single-server PostgreSQL database using WAL-E

  • WAL-E: https://github.com/wal-e/wal-e
  • Assuming Ubuntu 12.04 LTS ("Precise")
  • We'll be using S3. Make sure you have an IAM in a group with GetObject, ListBucket and PutObject on the bucket you want to use (and that it's not public).

Setup:

  1. These packages:
@elithrar
elithrar / authserver.go
Last active June 30, 2021 07:12
HTTP Basic Auth example in Go (based on http://stackoverflow.com/a/21937924/556573 + bespoke middleware implementation)
package main
import (
"encoding/base64"
"github.com/gorilla/mux"
"net/http"
"strings"
)
func main() {
@elithrar
elithrar / supervisord.conf
Last active September 4, 2021 20:08
Example supervisord.conf for a Go application #golang
[unix_http_server]
file=/var/run/supervisor.sock
chmod=0770
chown=root:supervisor
[supervisord]
pidfile=/var/run/supervisord.pid
nodaemon=false
logfile=/var/log/supervisord/supervisord.log
loglevel=error
@elithrar
elithrar / use.go
Last active January 5, 2023 11:19
go/use: Little middleware chains that could. Inspired by comments here: https://github.com/gorilla/mux/pull/36
r := mux.NewRouter()
// Single handler
r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging)
// All handlers
http.Handle("/", recovery(r))
// Sub-routers
apiMiddleware := []func(http.Handler) http.Handler{logging, apiAuth, json}
@elithrar
elithrar / helpers.go
Created June 5, 2014 11:50
GenerateShortGUID() - generate a 8-byte, URL-safe ID for slightly saner URLs with a very low collision chance (http://goo.gl/VWAFOo) and avoid http://example.com/thing/550e8400-e29b-41d4-a716-446655440000 ugliness. Leverages Go's existing cryptographically secure random number generator: http://elithrar.github.io/article/generating-secure-random…
// GenerateRandomBytes generates a crytographically secure random byte string using crypto/rand.
func GenerateRandomBytes(s int) ([]byte, error) {
b := make([]byte, s)
n, err := rand.Read(b)
if n != len(b) || err != nil {
return nil, fmt.Errorf("Unable to successfully read from the system CSPRNG (%v)", err)
}
return b, nil
}
@elithrar
elithrar / keybase.md
Created June 12, 2014 21:43
keybase.md

Keybase proof

I hereby claim:

  • I am elithrar on github.
  • I am silverlock (https://keybase.io/silverlock) on keybase.
  • I have a public key whose fingerprint is 73E6 AFB3 9131 E24C A140 F660 D6E8 9C78 40EB 8D6B

To claim this, I am signing this object: