Skip to content

Instantly share code, notes, and snippets.

View gnilchee's full-sized avatar

Greg Nilchee gnilchee

  • Evernote
  • Woodinville, WA
View GitHub Profile
@gnilchee
gnilchee / state_to_abbreviation.json
Created May 4, 2020 04:25
convert state name to abbreviation
[
{
"name": "Alabama",
"abbreviation": "AL"
},
{
"name": "Alaska",
"abbreviation": "AK"
},
{
@gnilchee
gnilchee / multiple_promises.js
Last active April 29, 2020 08:02
Using node-fetch and Promises. Comparing using a function vs within the promise to manipulate the data
// import node-fetch module
const fetch = require("node-fetch");
// return user-agent from request
const userAgent = fetch("https://httpbin.org/user-agent")
.then((response) => response.json())
.then((response) => response);
// return origin IP from request
const originIP = fetch("https://httpbin.org/ip")
@gnilchee
gnilchee / get_urls.py
Created March 22, 2020 08:40
Use asyncio and aiohttp to grab status codes from 20 sites
import asyncio
import aiohttp
my_urls = [
'https://www.google.com/',
'https://www.youtube.com/',
'https://www.facebook.com/',
'https://www.wikipedia.org/',
'https://www.yahoo.com/',
'https://www.reddit.com/',
@gnilchee
gnilchee / haproxy.cfg
Created March 10, 2020 01:32
HAProxy config supporting an active/active setup with shared table used for rate limiting
# tested with HAProxy 2.0 LTS on Debian 9
global
stats socket /tmp/haproxy_admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
defaults
mode http
maxconn 500
@gnilchee
gnilchee / foreach_tester.php
Created September 16, 2018 07:41
foreach example
<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'mango',
'fruit5' => 'peach');
echo $array['fruit3'];
echo "\n";
@gnilchee
gnilchee / memcache_tester.php
Created September 16, 2018 07:38
php and memcache example
<?php
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);
$result = $mem->get("username");
if ($result) {
echo $result;
} else {
echo "No matching key found. I'll add that now!\n";
@gnilchee
gnilchee / nginx.conf
Created September 9, 2018 04:19
example nginx.conf with json logging (nginx 1.11.8+)
log_format json_combined escape=json
'{'
'"time_local":"$time_local", '
'"remote_addr":"$remote_addr", '
'"client_addr":"$proxy_add_x_forwarded_for", '
'"request_type": "$request_method", '
'"request":"$request", '
'"request_uri": "$uri", '
'"query_string": "$query_string", '
'"status": "$status", '
@gnilchee
gnilchee / es_6.3.0_stats_shards_20180618.json
Created June 19, 2018 01:40
GET /_stats?level=shards (ES v6.3.0)
{
"_all": {
"primaries": {
"completion": {
"size_in_bytes": 0
},
"docs": {
"count": 1989994,
"deleted": 0
},
@gnilchee
gnilchee / git_describe_tags.py
Last active January 21, 2018 03:20
Get current tag of branch and increment patch version
import git
git_path = '/path/to/git_dir/'
repo = git.Repo(git_path)
cur_version = repo.git.describe()
major, minor, patch = cur_version.split('.')
updated_patch = int(patch)+1
new_tag = '.'.join((major, minor, str(updated_patch)))
@gnilchee
gnilchee / ssh_command.py
Last active January 31, 2018 07:08
Simple example executing commands via ssh with Paramiko
#!/usr/bin/env python
import paramiko
SSH_HOST='host.example.com'
SSH_USER='admin_user'
SSH_KEY='/home/user/.ssh/id_rsa'
def do_ssh_command(command):
try:
with paramiko.SSHClient() as client: