Skip to content

Instantly share code, notes, and snippets.

View dux's full-sized avatar

Dino Reić dux

  • Trifolium
  • London, Zagreb, Berlin
View GitHub Profile
@dux
dux / other-links.txt
Last active June 20, 2024 21:21
some links
@dux
dux / custom_element.js
Last active February 1, 2022 02:11
Creates custom DOM element and passes props. Bare bones custom nodes
// https://gist.github.com/dux/89c59570228d745128aafef98164de1f
// micro custom dom elements, no shadow dom
// exposes props: root, props and state
window.CustomElement = {
// we need to find custom node in exact name
// CustomElement.find(domNode, 'foo-bar')
find: (node, uid) => {
while(node) {
@dux
dux / Caddyfile
Last active February 15, 2022 17:22
linux services systemd
# https://caddy.community/t/serving-tens-of-thousands-of-domains-over-https-with-caddy/11179
{
admin off
# on_demand_tls {
# ask http://localhost:3000
# interval 2m
# burst 5
# }
@dux
dux / haversine.sql
Created January 21, 2022 17:26 — forked from carlzulauf/haversine.sql
PostgreSQL function for haversine distance calculation, in miles
-- Haversine Formula based geodistance in miles (constant is diameter of Earth in miles)
-- Based on a similar PostgreSQL function found here: https://gist.github.com/831833
-- Updated to use distance formulas found here: http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe
CREATE OR REPLACE FUNCTION public.geodistance(alat double precision, alng double precision, blat double precision, blng double precision)
RETURNS double precision AS
$BODY$
SELECT asin(
sqrt(
sin(radians($3-$1)/2)^2 +
sin(radians($4-$2)/2)^2 *
@dux
dux / _problem.md
Last active January 6, 2023 18:18
Ruby Sequel switch databases on the fly
  1. I wanted to keep global user data in one database and org data in separate database
  2. I want to use Sequel models in the same way as I was using a single databases
  3. I need simple interface to define that model data resides in Organization and not Global DB

Only possible solution I found is by patching Sequel native methods. If anyone knows better way to do it, like injecting DB connection just before dataset executes a query, please comment and explain.

I tried to use "sharding" and "arbitrary_servers", but that is not what I want. I have 1000nds of orgs, and 1000nds of DBs. I need connections to be fuly dynamic and I want to define request context once when request starts in Thread.current.

@dux
dux / _
Last active January 6, 2023 18:20
Capistrano replacement via common rake tasks
you will need
* hash_wia gem https://github.com/dux/hash_wia
* https://github.com/amazing-print/amazing_print
# to fully deploy application
rake remote:deploy
@dux
dux / paginate.rb
Last active January 6, 2023 18:21
Trivial and fast (does not execute count or any other unneeded requests) Ruby / Rails / Sinatra / Lux pagination
# @list = Paginate.set User, size: 40, name: :upage
# Paginate.render @list
# using: https://github.com/dux/html-tag/
module Paginate
extend self
def set rset, opts={}
opts[:size] ||= 20
opts[:name] ||= :page
@dux
dux / myapp.service
Created January 18, 2021 13:39
systemd sample script to start puma/rails app
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=deploy
@dux
dux / asmodeus.rb
Created July 4, 2019 10:46
check if process pid is running and exit
class Asmodeus
attr_accessor :pid_file
def info text
puts 'Asmodeus: %s' % text
end
def pid_check no_check=false
return unless @pid_file
@dux
dux / _usage.js
Last active May 20, 2019 21:13
DOM create customElement or polyfil for older browsers. passes node + attributes
// for svelte js, or any othe widget provider
let bindSvelteToDOM = function(name, klass) {
DOMCustomElement.define(name, function(node, opts) {
node.classList.add('mounted')
new klass({ target: node, props: opts })
})
}
import SvelteCheckbox from './svelte/checkbox.svelte'
bindSvelteToDOM('s-checkbox', SvelteCheckbox)