Skip to content

Instantly share code, notes, and snippets.

View kentquirk's full-sized avatar

Kent Quirk kentquirk

View GitHub Profile
@kentquirk
kentquirk / checktoken.go
Created October 9, 2015 23:03
Check token Golang middleware
// ChkTok is middleware that validates that the required
// security token is present.
func ChkTok(handler http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
sessionid := req.Header.Get(AUTHTOKEN)
if sessionid == "" {
util.WriteNewWebError(rw, http.StatusForbidden, "U-107", "x-anet-token is required.")
return
}
handler(rw, req)
@kentquirk
kentquirk / composehandlers.go
Created October 19, 2015 21:44
compose handlers in go
// CountRequests is middleware for standard handler functions
func (vc *VascoClient) CountRequests(handler http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// we use a recorder as our response writer so we can inspect it
recorder := httptest.NewRecorder()
// now call the original handler with our recorder
handler(recorder, req)
// switch on the result
code := recorder.Code
@kentquirk
kentquirk / index.html
Last active October 24, 2015 02:07
test of bl.ocks.org
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart div {
font: 10px sans-serif;
background-color: green;
text-align: right;
padding: 3px;
margin: 1px;
@kentquirk
kentquirk / decodejwt.go
Created November 6, 2015 22:26
Go demonstration of decoding jwt
package main
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"encoding/json"
@kentquirk
kentquirk / genericsimple.js
Last active September 10, 2016 16:22
Most generic JS
var output = {}
for (var i=0; i<input.length; i++) {
if (output[input[i].owner] == undefined) {
output[input[i].owner] = [input[i].pet]
} else {
output[input[i].owner].push(input[i].pet)
}
}
@kentquirk
kentquirk / moreidiomatic.js
Last active September 10, 2016 17:00
moreidiomatic.js
var output = {}
input.forEach(function(element) {
if (output.hasOwnProperty(element.owner)) {
output[element.owner].push(element.pet)
} else {
output[element.owner] = [element.pet]
}
});
@kentquirk
kentquirk / es6.js
Created September 10, 2016 17:05
ES6-ready
let output = {}
for (const element of input) {
if (output.hasOwnProperty(element.owner)) {
output[element.owner].push(element.pet)
} else {
output[element.owner] = [element.pet]
}
}
@kentquirk
kentquirk / petowner_simple.py
Last active September 10, 2016 17:50
Simple python
output = {}
for element in input:
if output.has_key(element["owner"]):
output[element["owner"]].append(element["pet"])
else:
output[element["owner"]] = [element["pet"]]
@kentquirk
kentquirk / petowner_except.py
Created September 10, 2016 17:21
Try using exceptions
output = dict()
for element in input:
try:
output[element["owner"]].append(element["pet"])
except KeyError:
output[element["owner"]] = [element["pet"]]
@kentquirk
kentquirk / petowner_noif.py
Created September 10, 2016 17:54
No explicit conditional
output = dict()
for element in input:
item = output.get(element["owner"], [])
item.append(element["pet"])
output[element["owner"]] = item