Skip to content

Instantly share code, notes, and snippets.

View 4lun's full-sized avatar

Alun Davey 4lun

View GitHub Profile
@4lun
4lun / helpers.php
Created April 22, 2024 12:00
Laravel Route macros/helpers for serving multiple domains
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
// Usage:
//
// Route::domain('domain.com', function () {
// Route::get('/', [DomainController::class, 'index']);
// });
@4lun
4lun / apache_app.conf
Last active April 18, 2024 16:32
Get access logs on DigitalOcean App Platform (with PHP via heroku-php-apache2) logging the forwarded Client IP by default on the "Runtime Logs" tab
# Update run_command in app spec to pass config, e.g. `run_command: heroku-php-apache2 -C apache_app.conf public/`
DirectoryIndex index.php index.html index.htm
# mod_remoteip should be loaded by default, but needs configuration
RemoteIPHeader DO-Connecting-IP
# Trust any private IPv4 ranges
RemoteIPTrustedProxy 10.0.0.0/8
RemoteIPTrustedProxy 172.16.0.0/12
@4lun
4lun / reddit-unsubscribe-all-subreddits.js
Last active March 1, 2024 07:31
Unsubscribe from all subreddits
// Visit https://www.reddit.com/subreddits/ and run the following in console (browser dev tools)
// Wait until all the buttons have visibly toggled, refresh page to confirm.
$('.fancy-toggle-button .remove').each(function(i, elem) { setTimeout(function(){ $(elem).trigger('click'); }, i*500) });
@4lun
4lun / git-clone-different-ssh-key.sh
Created February 20, 2024 10:32
Clone a git repo using an alternative ssh key
GIT_SSH_COMMAND="ssh -i ~/.ssh/PRIVATE_KEY" git clone git@github.com:ORG/REPO.git
@4lun
4lun / gist:9a9bd9c3d195d22005216d71ec661b55
Created January 19, 2024 15:15
Nginx server block config lines for piping access_log and error_log to syslog (useful for then getting relayed to papertrail). Source: https://chabik.com/nginx-logging-to-syslog/
access_log syslog:server=unix:/dev/log,tag=nginx,nohostname,severity=info combined;
error_log syslog:server=unix:/dev/log,tag=nginx,nohostname,severity=error;
@4lun
4lun / toBlueprint.js
Created November 30, 2023 10:20
Generate a quick and dirty type "blueprint" of a JS object/array/variable
function toBlueprint(obj) {
if (!obj || typeof obj !== 'object') {
return obj;
}
if(Array.isArray(obj)) {
return obj.map(toBlueprint) // nit: won't unify common props
}
return Object.keys(obj).reduce((acc, key) => {
let blueprint = typeof obj[key];
if (blueprint === "object" && blueprint) {
@4lun
4lun / t480s-fix-trackpad.bashrc
Created June 14, 2023 09:28
Trackpad sometimes stops working on T480s under Ubuntu, this restarts the driver which "fixes" it (until it stops again) without a restart
alias fix_trackpad="sudo modprobe -r elan_i2c && sudo modprobe elan_i2c"
@4lun
4lun / useEffectDebug.ts
Created March 21, 2023 12:20
Simple useEffect debug hook for figuring out which dependencies have changed in React
import { DependencyList, EffectCallback, useEffect, useRef } from 'react';
const useEffectDebug = (
effect: EffectCallback,
deps: DependencyList,
label?: string,
) => {
const previous = useRef<DependencyList>();
useEffect(() => {
const changedDepIndexes: number[] = [];
@4lun
4lun / class-names.css
Last active March 2, 2022 10:17
Ideas for CSS class names
.box,
.sector,
.section,
.quadrant,
.crate,
.container,
.page,
.content,
.body,
.head,
@4lun
4lun / str.slug.js
Created December 10, 2014 16:13
Slug function in JS matching the Laravel 4 implementation (Str::slug) - Note: does not include the transliteration of a UTF-8 value to ASCII
function slug(title, separator) {
if(typeof separator == 'undefined') separator = '-';
// Convert all dashes/underscores into separator
var flip = separator == '-' ? '_' : '-';
title = title.replace(flip, separator);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
title = title.toLowerCase()
.replace(new RegExp('[^a-z0-9' + separator + '\\s]', 'g'), '');