Skip to content

Instantly share code, notes, and snippets.

@ivan3bx
ivan3bx / geoip_subnet.go
Created February 1, 2020 15:49
retrieve subnet for given IP
package main
import (
"net"
"fmt"
)
// Get the subnet for a given IP address
func geoIP() {
ip := net.ParseIP("128.174.5.59")

Keybase proof

I hereby claim:

  • I am ivan3bx on github.
  • I am i3x (https://keybase.io/i3x) on keybase.
  • I have a public key ASCJ22U-E5AunADD1zBDe2-VYE_myhHTeVOwiCch37cEAgo

To claim this, I am signing this object:

@ivan3bx
ivan3bx / soundex.rb
Created April 20, 2019 20:01
implementing the soundex algorithm for fun
require 'minitest/autorun'
require 'text'
# An exercise to implement the 'soundex' algorithm
#
# See https://en.wikipedia.org/wiki/Soundex for details
#
# Compare with 'text' gem impl: https://github.com/threedaymonk/text/blob/master/lib/text/soundex.rb
class Soundex
def process(input)
@ivan3bx
ivan3bx / upgrade_go.rb
Created February 27, 2019 14:26
simple script to unpack a go distribution & link it into /usr/local
#!/usr/bin/env ruby
require 'pathname'
# Upgrade go on linux from binary distribution file
# 1. Will create /usr/local/go-<major>.<minor>.<patch>
# 2. Will create a symbolic link from /usr/local/go -> this version
def run
file = ARGV[0]
@ivan3bx
ivan3bx / graceful_httpserver.go
Created September 20, 2017 05:32
Example of handling graceful shutdowns with gin-gonic
// runServer will start the HTTPServer, handling graceful
// shutdown on receives SIGTERM / SIGINT, returning nil.
//
// - returns error if the server can not start
// - returns error if the server panics in a way that isn't
// otherwise handled by gin
// - no errors otherwise
func runServer(addr string, r *gin.Engine) error {
s := &http.Server{
Addr: addr,
@ivan3bx
ivan3bx / throttling.go
Last active September 15, 2017 05:34
simple throttling of work in go
package main
import (
"fmt"
"time"
)
func main() {
// queue of size 10
queue := make(chan string, 10)
@ivan3bx
ivan3bx / non-vendor-packages.sh
Created July 15, 2017 23:20
Replacement expression for 'glide novendor' that returns list of packages suitable as arguments to `go test`
#
# select unique directories with *.go files
# and appends "/..." ("foo/bar.go" → "./foo/...")
#
find . -type f -name \*go -maxdepth 2 -exec dirname {} \; | uniq | sed 's/\([a-z]\)$/\1\/.../'
@ivan3bx
ivan3bx / custom_marshalling.go
Created May 6, 2016 18:20
Using type alias in custom JSON marshalling
package main
import (
"encoding/json"
"fmt"
"os"
)
type Foo struct {
One string
@ivan3bx
ivan3bx / api_router_swift_2.2.swift
Created April 13, 2016 07:03
Using enums to define an API router where query params are defined in a trailing closure (updated for latest Swift)
// http://eliasbagley.github.io/json/api/alamofire/2015/09/25/making-network-requests-with-alamofire.html
enum APIRouter: URLRequestConvertible
{
static let BASE_URL = "http://foo"
static let API_KEY = "1231254"
case User(Int, closure: () -> [String:AnyObject])
case Likes(Int, Int)
var URLRequest: NSMutableURLRequest
@ivan3bx
ivan3bx / lesseract-test.js
Created March 16, 2016 14:52
testing out node-tesseract
var tesseract = require('node-tesseract');
var options = {
l: 'eng',
psm: 1
};
tesseract.process('/home/tesseract/IMG_4918.jpg', options, function(err, text) {
if (err) {
console.error(err);