Skip to content

Instantly share code, notes, and snippets.

View peter's full-sized avatar

Peter Marklund peter

View GitHub Profile
@peter
peter / terraform-util.js
Created December 17, 2020 11:30
De-duplicating Terraform code by generating it from JavaScript
const fs = require('fs')
module.exports = {
writeTerraform,
generateMain,
}
// Templates are just terraform (.tf files) with the following kinds of interpolations:
// 1. Variables embedded in a string: "terraform-states-<id>"
// 2. Variables occupying an entire string: "<apiId>".
@peter
peter / traverse-object-generator.js
Last active December 11, 2020 12:10
Traverse an object in Node.js/Javascript with a generator function
function isObject (value) {
return value != null && typeof value === 'object' && value.constructor === Object
}
//////////////////////////////////////////
// GENERATOR
//////////////////////////////////////////
function * traverseObj (value, path = []) {
@peter
peter / check-linux-version.sh
Created December 10, 2020 09:21
Check Linux Version
#!/bin/bash
uname -a
if test -f "/etc/os-release"; then
cat /etc/os-release
fi
if command -v lsb_release; then
lsb_release -a
fi
cat /proc/version
-- Order by one indexed column (FAST)
newsdesk_production=# explain analyze select * from pressreleases order by published_at DESC limit 100;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=0.00..249.91 rows=100 width=1207) (actual time=26.070..716.453 rows=100 loops=1)
-> Index Scan Backward using pressreleases_published_at_index on pressreleases (cost=0.00..964766.62 rows=386042 width=1207) (actual time=26.067..716.343 rows=100 loops=1)
Total runtime: 716.709 ms
(3 rows)
- Order by two separately indexed columns (SLOW)
@peter
peter / about_scoring_project.rb
Created September 8, 2011 14:23
Solution to Ruby Koans Scoring Project (about_scoring_project.rb)
def counts_from_dice(dice)
dice.inject({}) do |hash, value|
hash[value] ||= 0
hash[value] += 1
hash
end
end
def score(dice)
counts_from_dice(dice).inject(0) do |sum, (number, count)|
@peter
peter / gist:700194
Created November 15, 2010 09:21
Rails Migration With Tests
class ImportLegacyDevices < ActiveRecord::Migration
def self.up
return unless Rails.env.production?
legacy_devices.each do |device_id, issue|
if device = Device.find_by_hardware_id(device_id)
unless InclusiveIssue.exists?(:issue_id => issue.id, :device_id => device.id)
InclusiveIssue.create!(
:issue => issue,
:device => device,
@peter
peter / creating-edgerails-app.sh
Created June 30, 2012 21:03
Creating and Deploying an EdgeRails (Rails 4) Application to Heroku
# 0. Make sure you have Ruby 1.9.3 installed, and optionally RVM and PostgreSQL
# 0.2 If you are on the Mac, make sure you have a c compiler by installing XCode Command Line Tools or gcc4.2 with homebrew
# https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers
# 0.5 Make sure you have bundler version ~> 1.2 as Rails depends on it
gem install bundler
# 1. Get edge Rails source (master branch)
git clone https://github.com/rails/rails.git
@peter
peter / cors-proxy.js
Last active August 24, 2020 12:33
Local Node http proxy with CORS headers
// The use case is that you are developing a web app locally that makes Ajax requests to some external
// API that doesn't have sufficiently permissive CORS headers so your requests fail. The solution is to point the app
// to this local proxy which will allow CORS.
const http = require('http')
const axios = require('axios')
const HOSTNAME = '127.0.0.1'
const PROXY_BASE_URL = process.env.PROXY_BASE_URL;
const PORT = process.env.PORT || 9999
@peter
peter / koa-naive-basic-auth.ts
Last active January 29, 2020 09:36
Naive Basic Auth Middleware for Koa
import crypto from 'crypto';
export function basicAuthMiddleware({ name, pass }: any): Function {
const auth = `Basic ${Buffer.from([name, pass].join(':')).toString(
'base64'
)}`;
return async (ctx: any, next: any): Promise<any> => {
const header = ctx.request.get('Authorization');
const headerMatches =
header &&
@peter
peter / koa-naive-basic-auth-first-draft.ts
Created January 29, 2020 09:36
Naive Basic Auth Middleware for Koa - First Draft
/* eslint-disable no-plusplus */
import crypto from 'crypto';
function parse(header: string | null): string | null {
const match = (header || '').match(/^Basic (\S{3,300})$/);
return match && Buffer.from(match[1], 'base64').toString();
}
function isEqual(s1: string | null, s2: string | null): boolean {
return (