Skip to content

Instantly share code, notes, and snippets.

View garraflavatra's full-sized avatar

Romein van Buren garraflavatra

View GitHub Profile
@garraflavatra
garraflavatra / prefetch.js
Last active October 22, 2024 15:10
Speed up webpage loading up to 10x by prefetching links hovered upon.
/*! prefetch.js v1.0.1 - Speed up webpage loading by prefetching links hovered upon.
(c) Romein van Buren - 2024 - MIT license */
document.addEventListener('DOMContentLoaded', () => {
for (const link of document.querySelectorAll('a:not([data-noprefetch])')) {
const url = new URL(link.href, 'https://'+window.location.hostname);
if (url.hostname !== window.location.hostname ||
window.location.pathname === url.pathname) continue;
const callback = () => {
@garraflavatra
garraflavatra / show_urls.py
Created June 8, 2024 17:45
Django management command that prints list of all registered URL patterns. Adapted from https://github.com/django-extensions/django-extensions, but stand-alone.
import functools
import json
import re
from django.conf import settings
from django.contrib.admindocs.views import simplify_regex
from django.core.exceptions import ViewDoesNotExist
from django.core.management.base import BaseCommand, CommandError
from django.utils import translation
from django.urls import URLPattern, URLResolver # type: ignore
<script>
const server = 'https://tile.openstreetmap.org';
const lon = 4.76114 * Math.PI / 180;
const lat = 51.57947 * Math.PI / 180;
const z = 18;
const n = 2**z;
// docs: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
@garraflavatra
garraflavatra / django-compose-rollout.sh
Created March 31, 2024 16:20
Zero-downtime deployment script for Docker Compose, Django, and Nginx.
#!/bin/sh
#
# Zero-downtime deployment script for Docker Compose and Nginx (+ Django)
# Inspired by https://www.tines.com/blog/simple-zero-downtime-deploys-with-nginx-and-docker-compose
#
# (c) Romein van Buren 2024
# SPDX-License-Identifier: WTFPL
#
reload_nginx() {
@garraflavatra
garraflavatra / nginx.gzip.conf
Created December 2, 2023 14:42
Nginx gzip configuration
http {
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/java>
}
@garraflavatra
garraflavatra / find-777.sh
Last active December 9, 2023 20:36
Find all 777 directories on Linux systems
# Find all 777 files:
sudo find / -type d -perm 777
# Find all world-writable files:
sudo find / -xdev -type d \( -perm -0002 -a ! -perm -1000 \) -print
# Find all files without owner:
sudo find / -xdev \( -nouser -o -nogroup \) -print
@garraflavatra
garraflavatra / nginx-php-mariadb-installer-debian12.bash
Last active September 20, 2024 00:29
Install nginx, PHP, FPM, MariaDB & Certbot on Debian 12
# Update apt repositories
sudo apt update -y
sudo apt upgrade -y
# Install nginx & PHP
sudo apt install -y nginx php8.2
sudo apt-get install -y php8.2-{fpm,cgi,mysql,curl,xsl,gd,common,xml,zip,xsl,soap,bcmath,mbstring,gettext,imagick,sqlite3,intl}
sudo nano /etc/nginx/sites-available/default
# Add the following / update the file to contain it:
@garraflavatra
garraflavatra / git-fork-update.bash
Last active June 17, 2023 10:38
Bash script that keeps all Git repositories within a specified directory up to date.
#!/usr/bin/bash
#
# This script keeps all Git repositories within a specified directory up to date.
#
# Consider the following example. The linux and curl repositories will be updated
# when you run this in the gitforks directory.
#
# gitforks/
# linux/.git/ linux Git repository
@garraflavatra
garraflavatra / gitlines.sh
Created December 29, 2021 10:19
Git: how many lines did a user change? Modified https://gist.github.com/Xeoncross/4020489#gistcomment-3989215
git log --shortstat --author "Romein van Buren" \
| egrep "file[s] changed" \
| sed 's/changed, \([0-9]\+ deletions\)/changed, 0 insertions(+), \1/g' \
| awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed:", files, "- lines inserted:", inserted, "- lines deleted:", deleted}'
@garraflavatra
garraflavatra / meta.svelte
Last active October 23, 2023 10:22
Meta tags component for SvelteKit
<script>
import { page } from '$app/stores';
export let type = 'website'; // or article, or music.album etc. See https://ogp.me/#types
export let title = '';
export let description = '';
export let image = '/img/social/preview.jpg';
export let path = $page.url.pathname;