Skip to content

Instantly share code, notes, and snippets.

View julik's full-sized avatar
💭
🎺

Julik Tarkhanov julik

💭
🎺
View GitHub Profile
# This class encapsulates a unit of work done for a particular tenant, connected to that tenant's database.
# ActiveRecord makes it _very_ hard to do in a simple manner and clever stuff is required, but it is knowable.
#
# What this class provides is a "misuse" of the database "roles" of ActiveRecord to have a role per tenant.
# If all the tenants are predefined, it can be done roughly so:
#
# ActiveRecord::Base.legacy_connection_handling = false if ActiveRecord::Base.respond_to?(:legacy_connection_handling)
# $databases.each_pair do |n, db_path|
# config_hash = {
# "adapter" => 'sqlite3',
@julik
julik / styled_templates.rb
Last active April 22, 2025 09:29
styled_templates.rb
# <%= style_for_this_template do %>
# width: 128px;
# .caption {
# font-size: 12px;
# text-align: center;
# }
# <% end %>
# <div class="<%= css_class_for_this_template %>">
# <div class="title">Hello!</div>
# </div>
require 'bundler/inline'
require "logger"
gemfile do
source 'https://rubygems.org'
gem "activerecord", "~> 6", require: "active_record"
gem "sqlite3", "~> 1.1" # This has to be of a version activerecord supports, so can't be 2.x
end
16.times do |n|
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'premailer'
gem 'nokogiri'
gem 'base64'
gem 'mail'
<!DOCTYPE html>
<html>
<head>
<style>
.outlinerounded {
width: 1px; height: 1px; overflow: hidden; border: 1px solid gray;
}
</style>
</head>
<body>
@julik
julik / rae.rb
Created February 13, 2025 01:04
AES encryption with random access to plaintext (playground)
require "openssl"
require "stringio"
class PassthruScheme
READ_OR_WRITE_CHUNK_SIZE = 1 << 16
def streaming_encrypt(from_plaintext_io, into_ciphertext_io)
while (chunk = from_plaintext_io.read(READ_OR_WRITE_CHUNK_SIZE))
into_ciphertext_io.write(chunk)
end
@julik
julik / substreams.rb
Created February 7, 2025 16:32
AES-CFB with substream-based blocks
# frozen_string_literal: true
require "test_helper"
class BlockwiseEncryptionTest < ActiveSupport::TestCase
class NonRandomAccessible
def initialize(encryption_key)
@algo = "aes-256-cfb"
@iv_and_key_length = 16 + 32
@key_derivation_salt = "my desire"
@julik
julik / deploy.sh
Created April 30, 2024 08:31
The Deployinator™©
#!/usr/bin/env bash
set -e
# This script will build a Docker image from the current state of the working tree.
# It will tag it with the short Git sha, and then push it to Quay.io under our company
# account. Afterwards the image can be picked up from other hosts to deploy.
export DEPLOY_HOST=<someapp>.<my-domain>
export REV=`git log -1 --pretty=%h`
class StreamingController < ApplicationController
class Writer
def initialize(&blk)
@blk = blk
end
def write(stuff)
@blk.call(stuff)
stuff.bytesize
end
@julik
julik / pecobox.rb
Created February 9, 2024 14:59
A circuit breaker using Pecorino leaky buckets
# frozen_string_literal: true
# Pecobox is a Circuitbox-like class which uses Pecorino for
# measurement error rates. It is less resilient than Circuitbox
# because your error stats are stored in the database, but guess what:
# if your database is down your product is already down, so there is
# nothing to circuit-protect. And having a shared data store for
# the circuits allows us to track across machines and processes, so
# we do not have to have every worker hammer at an already failing
# resource right after start