Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
rlipscombe / exwx.exs
Created November 26, 2017 16:08
Using wxWidgets from Elixir
#!/usr/bin/env elixir
defmodule Canvas do
@behaviour :wx_object
@title "Canvas Example"
@size {600, 600}
def start_link() do
:wx_object.start_link(__MODULE__, [], [])
@rlipscombe
rlipscombe / https-server.txt
Last active November 14, 2023 09:09
Ad-hoc HTTPS server
## On the server
# AWS EC2
SERVER_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
# Digital Ocean
SERVER_IP=$(curl http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
# Generate a self-signed server certificate
openssl genrsa -out server.key 4096
-module(kvl).
-export([from_list/1]).
from_list(KVs) ->
from_list(KVs, #{}).
from_list([K, V | KVs], Acc) ->
from_list(KVs, Acc#{K => V});
from_list([], Acc) ->
Acc.
@rlipscombe
rlipscombe / Makefile
Created September 14, 2023 13:44
Presentations with Marp, etc.
PRESENTATION := cat-feeder-architecture
all: $(PRESENTATION).html
SVGS = $(patsubst %.d2,%.svg,$(wildcard *.d2))
SVGS += $(patsubst %.mmd,%.svg,$(wildcard *.mmd))
# brew install marp-cli
MARP := marp
%.html: %.md $(SVGS)
@rlipscombe
rlipscombe / list_share.erl
Created August 10, 2023 08:41
Erlang code to share a list between N
% Share a list between N.
share(List, N) when N > 0 ->
L = (length(List) + N - 1) div N,
lists:reverse(share(List, N, L, [])).
share([], _N = 0, _L, Acc) ->
Acc;
share([], N, L, Acc) ->
share([], N - 1, L, [[] | Acc]);
share(List, N, L, Acc) when L =< length(List) ->
@rlipscombe
rlipscombe / jwt.io.hs256.sh
Created July 19, 2018 10:51
Generating HS256 JWT in bash (jq, openssl)
#!/usr/bin/env bash
# This script uses the same secret key and example as jwt.io,
# so that you can verify that it's correct.
secret_key="your-256-bit-secret"
base64url() {
# Don't wrap, make URL-safe, delete trailer.
base64 -w 0 | tr '+/' '-_' | tr -d '='
@rlipscombe
rlipscombe / x509.erl
Created November 17, 2022 17:40
Generate a certificate signing request in Erlang, EC key.
-module(x509).
-export([
create_private_key/0,
private_key_to_pem/1,
create_request/2,
request_to_pem/1
]).
-include_lib("public_key/include/public_key.hrl").
@rlipscombe
rlipscombe / mss.cpp
Last active August 28, 2021 14:36
Playing with TCP_MAXSEG
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
@rlipscombe
rlipscombe / to_dot.ex
Created August 5, 2021 18:31
Processes to graphviz
def to_dot(pid, path) do
# Starting with the given process, find all of the processes that it's linked to/from, and that it's monitoring/monitored; put them in the dot file.
f = File.open!(path, [:write])
IO.write(f, "digraph G {\n")
IO.write(f, "concentrate=true")
seen = MapSet.new()
to_dot(pid, f, seen)
IO.write(f, "}\n")
@rlipscombe
rlipscombe / rm-duplicate-tracknr.exs
Last active July 20, 2021 08:43
Elixir snippet to find MP3 files with the same track number in their filenames
re = ~r/^.*([0-9][0-9]) .*$/U
files = Path.wildcard("**/*.mp3") |> Enum.filter(&Regex.match?(re, &1))
groups = files |>
Enum.group_by(fn f ->
d = Path.dirname(f)
f = Path.basename(f)
[_, n] = Regex.run(re, f)
{d, n}
end)