Skip to content

Instantly share code, notes, and snippets.

@marcelmeyer
marcelmeyer / audit.api_request().sql
Last active March 14, 2018 17:07
Allows for some logging and debugging of PostgREST requests. All the x-* headers set by the reverse proxy.
create or replace function audit.api_request() returns void
language plpgsql
as $$
declare
_request_id text := coalesce(current_setting('request.header.x-request-id', true),'');
_role text := coalesce(current_setting('request.jwt.claim.role', true),'');
_user_id text := coalesce(current_setting('request.jwt.claim.id', true),'');
_resource text := coalesce(current_setting('request.header.x-path', true),'');
_verb text := coalesce(current_setting('request.header.x-verb', true),'');
-module(key_grouper).
%% You have to make sure that the journal bucket is set to allow siblings ({"props" : { "allow_mult":true}}.
%% Fetch the grouped keys using the Accept: multi/mixed header.
-define (JOURNAL, <<"test">>).
-export([
store_per_month/1,
store_per_hour/1,
store_per_10min/1,
store_per_min/1]).
-export([log/1]).
location /resize {
alias /tmp/nginx/resize;
set $width 150;
set $height 100;
set $dimens "";
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) {
set $width $1;
set $height $2;
set $image_path $3;
@marcelmeyer
marcelmeyer / watch
Created October 10, 2012 11:04 — forked from exogen/watch
Generic file watcher script.
#!/bin/bash
#
# Author: Brian Beck <exogen@gmail.com>
# Usage: watch PATH COMMAND...
#
# This script watches PATH and runs COMMAND whenever PATH or a descendent
# of PATH is modified. COMMAND is everything after the first argument.
#
# If PATH is "-", then the list of paths comes from standard input.
#
@marcelmeyer
marcelmeyer / game.erl
Created January 30, 2012 15:34
Rail Cars!
-module (game).
-compile([export_all]).
go() ->
test(lists:seq(250,999)).
test([N|T]) ->
case match(parse(N), parse(4*N)) of
true -> io:format("~p~n", [N]);
false -> no
@marcelmeyer
marcelmeyer / time.erl
Created April 12, 2011 22:43
We all have written code that outputs some string representation of a time duration, like "You were poked 2 minutes ago". That usually involves a lot of if statements. Here it is in Erlang using pattern matching.
-module(time).
-compile([export_all]).
time_to_words({0,0,_}) -> "now";
time_to_words({0,1,_}) -> "1 minute";
time_to_words({0,M,_}) -> integer_to_list(M) ++ " minutes";
time_to_words({1,0,_}) -> "1 hour";
time_to_words({H,0,_}) -> integer_to_list(H) ++ " hours";
time_to_words({H,1,_}) -> integer_to_list(H) ++ "h " ++ "1min";
time_to_words({H,M,_}) -> integer_to_list(H) ++ "h " ++ integer_to_list(M) ++ "mins".
@marcelmeyer
marcelmeyer / fizz.erl
Created April 3, 2011 04:13
Solves the 'fizzbuzz' game! As I generally don't like 'if' or 'case' statements (in any language), I tried pattern matching to solve this.
-module(fizz).
-compile(export_all).
play(Max)->
Numbers = lists:seq(1, Max, 1),
[eval(N) || N <- Numbers].
eval(Number) when Number rem 15 == 0 -> fizzbuzz;
eval(Number) when Number rem 5 == 0 -> buzz;
eval(Number) when Number rem 3 == 0 -> fizz;
@marcelmeyer
marcelmeyer / neural.erl
Created April 2, 2011 20:10
Spawns Erlang processes for each node in a record that represents a node in a tree, then each node finds its parent and forms a logical tree structure where nodes can send messages to their ancestry to resolve certain questions about logical inheritance.
-module(neural).
-record(state, {id, pid, parentId, ppid, children=[]}).
-compile([export_all]).
test(N) ->
Nodes = [{Id, Id - 1} || Id <- lists:seq(1,N,1)],
Pids = [spawn(?MODULE, start, [Id, ParentId]) || {Id, ParentId} <- Nodes],
timer:sleep(1000),
Started = length([Pid ! {connect, Pids} || Pid <- Pids]),
Started.