Skip to content

Instantly share code, notes, and snippets.

@elhardoum
elhardoum / redis-cli-with-docker.md
Last active April 13, 2023 05:26
redis-cli without installing redis

Use redis-cli interactive shell without installing redis on your host machine, using docker.

Install

The following assumes you have docker engine or desktop up and running.

Add the following line to your bash profile (~/.bash_profile or ~/.bashrc)

@elhardoum
elhardoum / redis_hot_topics.php
Created February 12, 2023 17:56
Twitter-like Trending Topics App Backend - PHP Example
<?php
// @see https://medium.com/@elhardoum/2a442e04bb2f
function get_redis() : \Redis {
static $redis;
if ( null === $redis ) {
$redis = new \Redis();
$redis->connect('0.0.0.0', 6379);
@elhardoum
elhardoum / manifest.json
Last active June 19, 2022 18:13
Blank new tab to prevent chrome from slowing down whenever you open a new tab and type stuff into the address bar, taking ages of latency to reflect keyboard strokes
{
"name": "New Tab",
"version": "1.0",
"description": "Load a simple and blank new tab for google chrome.",
"manifest_version": 3,
"chrome_url_overrides": {
"newtab": "./tab.html"
}
}
@elhardoum
elhardoum / Program.cs
Created September 30, 2021 05:18
C# - Create a set of classes designed to help libraries track which books they own and whether they are available or checked out
using System;
using System.Collections.Generic;
using System.Linq;
namespace LearningActivity1
{
public class Book
{
public string Name { get; set; }
public string Author { get; set; }
@elhardoum
elhardoum / nodebalancer-automated-setup.js
Created January 20, 2021 19:10
Automatically add servers to your Linode nodebalancer. It's painful to do it manually when you have dozens of servers to add
/**
* Please change your nodebalancer configuration in `payload` line 6, and clusters configurations
* in lines 13 and 19
*
* This must be run within cloud.linode.com/ dashboard, ideally via DevTools > Console (or snippets)
*/
const payload = { "protocol": "tcp", "proxy_protocol": "none", "algorithm": "source", "stickiness": "table", "check": "none", "check_interval": 5, "check_timeout": 3, "check_attempts": 2, "port": 443, "check_passive": true, "cipher_suite": "recommended", "nodes": [] }
, ips = [] // a list of your IP addresses
, promises = []
@elhardoum
elhardoum / linode-cpu-stats.js
Last active January 20, 2021 04:24
fetch Linode servers last CPU percentage usage with JavaScript since the official CLI doesn't support this yet.
(async () =>
{
/*
* Remember to login to linode dashboard, and from there open the devtools > console and run this code.
*/
console.log('Fetching linodes...')
const linodes = await fetch('https://cloud.linode.com/api/v4/linode/instances/?page_size=100', {
headers: {
authorization: localStorage.getItem('authentication/token'),
@elhardoum
elhardoum / game.js
Created December 4, 2020 15:26
Simple Connect 4 Game with JavaScript
const game_elem = document.getElementById('game')
, game = JSON.parse(JSON.stringify(new Array(game_elem.childElementCount-1).fill(
new Array(game_elem.children[0].childElementCount).fill(null)
)))
const colors = { 1: 'red', 2: 'green' }
, color_style = document.getElementById('bg-styles')
, status = document.getElementById('status')
let current_player = 1
@elhardoum
elhardoum / upload.js
Last active August 12, 2020 14:25
Handle file uploads in node.js without express
// call fileUploadRequestHandler( req, res ) from http.createServer callback context
// change ./files references to your files directory name
async function fileUploadRequestHandler( req, res )
{
const fs = require(fs), path = require('path')
// update the file name/ext here to a random or dynamic one (e.g supplied from querystring)
const filename = 'example.txt'
<?php
// temporarily disable post_title matching for wp query search
add_filter('posts_where_paged', $disablePostTitleSearch=function( string $sql )
{
return str_replace('.post_title LIKE ', '.post_content LIKE ', $sql);
});
// now query posts
$posts = query_posts([
@elhardoum
elhardoum / test.sh
Last active May 20, 2020 16:02
Testing your load balancer servers for downtime, ignores SSL errors assuming you're testing the core of your web service
#!/usr/bin/env sh
host="www.example.com"
ips=(
93.184.216.34
)
for ip in "${ips[@]}"; do
curl -H "Host: $host" "https://$ip/" -kIs |grep 200\ OK >/dev/null && (
echo "\033[0;32m[✓] $ip\t(passed)"