Skip to content

Instantly share code, notes, and snippets.

View titpetric's full-sized avatar

Tit Petric titpetric

View GitHub Profile
@titpetric
titpetric / AnimalDependencies.php
Created July 5, 2013 11:36
Dependency Injection with Traits (PHP 5.4+)
<?php
namespace AnimalDependencies;
trait Dog {
private $dog = false;
function setDog(\Animals\Dog $dog) {
$this->dog = $dog;
}
function getDog() {
@titpetric
titpetric / logtail.sh
Last active December 19, 2015 16:38
Incremental reading of files that get rotated. If files are being written to it ignores the bytes written since the first `stat` call, avoiding double reads. Fairly untested so far, not even sure if it recognizes that the log rotation was performed. Mileage may vary, feel free to submit fixes.
#!/bin/bash
function usage {
echo Usage: $0 logfile.log logfile.log.1 [name]
exit
}
if [ -z $1 ]; then
usage
fi
if [ -z $2 ]; then
#!/bin/bash
function docker_run_shell {
NAME=$1
DOCKERFILE=$2
# docker needs a few libraries, but not all - essential libs here
BIND_LIBS=`ldd /usr/bin/docker | grep /lib/ | awk '{print $3}' | egrep '(apparmor|libseccomp|libdevmap|libsystemd-journal|libcgmanager.so.0|libnih.so.1|libnih-dbus.so.1|libdbus-1.so.3|libgcrypt.so.11)'`
ARGS=""
for LIB in $BIND_LIBS; do
ARGS="$ARGS -v $LIB:$LIB:ro"
@titpetric
titpetric / gpio.go
Created July 19, 2016 14:45
Using GPIO pins with GoLang
package main
import "os"
import "io/ioutil"
import "time"
type GPIO struct{}
func (r GPIO) Pin(name string) GPIO_Pin {
pin := GPIO_Pin{name}
@titpetric
titpetric / purge.lua
Created November 2, 2016 11:00
Delete NGINX cached items from a PURGE request
-- Tit Petric, Monotek d.o.o., Thu 27 Oct 2016 10:43:38 AM CEST
--
-- Delete nginx cached assets with a PURGE request against an endpoint
--
local md5 = require 'md5'
function file_exists(name)
local f = io.open(name, "r")
if f~=nil then io.close(f) return true else return false end
@titpetric
titpetric / proxy-proc-net.sh
Created November 12, 2016 11:36
Fake copy of a part of /proc/net fs, with about 200ms of latency
#!/bin/bash
OUTPUT="/dev/shm/fakenet/";
if [ ! -d "$OUTPUT" ]; then
mkdir -p $OUTPUT/{rpc,stat,ip_vs}
fi
SOURCES="/proc/net/dev
/proc/net/ip_vs_stats
/proc/net/ip_vs/stats
@titpetric
titpetric / purge-multi.lua
Created January 8, 2017 18:34
Delete NGINX cached items with a PURGE with wildcard support
-- Tit Petric, Monotek d.o.o., Tue 03 Jan 2017 06:54:56 PM CET
--
-- Delete nginx cached assets with a PURGE request against an endpoint
-- supports extended regular expression PURGE requests (/upload/.*)
--
function file_exists(name)
local f = io.open(name, "r")
if f~=nil then io.close(f) return true else return false end
end
@titpetric
titpetric / client.lua
Created March 10, 2017 10:37
LUA FFI bridge to Go shared Lib
local ffi = require("ffi")
local awesome = ffi.load("./awesome.so")
ffi.cdef([[
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef double GoFloat64;
@titpetric
titpetric / graphql-mini.php
Created April 11, 2017 08:47
Transform GraphQL fields into JSON to use them for filtering native PHP arrays
<?php
/** A poor mans GraphQL fields parser
*
* Try to convert graphQL fields to JSON and then just use json_decode to produce an array.
*/
class Fields
{
/** Parse GraphQL fields into an array */
public static function parse($query)
@titpetric
titpetric / setup_ssmtp.bash
Created April 13, 2017 21:27
setting up ssmtp config from environment variables (docker start-up script)
#!/bin/bash
#
# You can set up sSMTP by setting the following ENV variables:
#
# SSMTP_TO - This is the address alarms will be delivered to.
# SSMTP_SERVER - This is your SMTP server. Defaults to smtp.gmail.com.
# SSMTP_PORT - This is the SMTP server port. Defaults to 587.
# SSMTP_USER - This is your username for the SMTP server.
# SSMTP_PASS - This is your password for the SMTP server. Use an app password if using Gmail.
# SSMTP_TLS - Use TLS for the connection. Defaults to YES.