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 / 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)
@dux
dux / rollup.config.js
Last active May 23, 2019 10:02
multi file rollup config with coffeescript, svelte, react and livereload
import babel from 'rollup-plugin-babel'
import svelte from 'rollup-plugin-svelte'
import { scss } from '@kazzkiq/svelte-preprocess-scss';
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import { terser } from 'rollup-plugin-terser'
import coffee from 'rollup-plugin-coffee-script'
import livereload from 'rollup-plugin-livereload'
const production = !process.env.ROLLUP_WATCH;