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
@elithrar
elithrar / gocraftweb.go
Last active December 13, 2018 16:39
HTTP Request Contexts in Go — Examples — http://elithrar.github.io/article/map-string-interface/
package main
import (
"fmt"
"log"
"net/http"
"github.com/gocraft/web"
)
@elithrar
elithrar / templates.go
Last active October 16, 2018 15:31
renderTemplate — write to a buffer first — http://elithrar.github.io/article/custom-handlers-avoiding-globals/
import (
"fmt"
"html/template"
"net/http"
"github.com/oxtoacart/bpool"
)
var bufpool *bpool.BufferPool
package main
import (
"fmt"
"log"
"net/http"
"html/template"
"github.com/gorilla/sessions"
@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:

@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 / 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 / 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 / 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 / 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 / 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 {