Instantly share code, notes, and snippets.

David Pennington Xeoncross

View GitHub Profile
@Xeoncross
Xeoncross / list_directory.go
Created Feb 8, 2019 — forked from manigandand/list_directory.go
List files in a directory using golang
View list_directory.go
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
@Xeoncross
Xeoncross / trimreader.go
Created Feb 1, 2019
just some examples of Golang io.Reader which trim whitespace
View trimreader.go
type TrimReader2 struct{ io.Reader }
var trailingws = regexp.MustCompile(` *\r?\n`)
func (tr TrimReader2) Read(bs []byte) (int, error) {
// Perform the requested read on the given reader.
n, err := tr.Reader.Read(bs)
if err != nil {
return n, err
@Xeoncross
Xeoncross / http_throttle.go
Created Jan 2, 2019
Golang HTTP concurrency limiter / throttle for limiting the maximum number of concurrent http handlers running at one time.
View http_throttle.go
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
@Xeoncross
Xeoncross / file_descriptors.go
Last active Jan 2, 2019
Counting file descriptors used by a process or Go application
View file_descriptors.go
package main
func countOpenFiles() int {
out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v | wc -l", os.Getpid())).Output()
if err != nil {
// log.Println("count files", err)
return 0
}
n, err := strconv.Atoi(strings.TrimSpace(string(out)))
@Xeoncross
Xeoncross / requests_per_minute.go
Last active Dec 31, 2018
Simple example of counting requests and submitting to datadog, statsd, or some other logging system.
View requests_per_minute.go
package main
import (
"fmt"
"net/http"
"sync/atomic"
"time"
)
var requestsPerMinute uint64
@Xeoncross
Xeoncross / enc.go
Created Dec 30, 2018 — forked from hbakhtiyor/enc.go
AES GCM Encryption/Decryption with Chunking Example in Golang, using OpenSSL
View enc.go
package main
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"io"
"os"
@Xeoncross
Xeoncross / shared_editor.md
Created Dec 29, 2018
Shared, encrypted, synced live WebRTC document editor
View shared_editor.md

Hyperpad

Shared, encrypted, synced live WebRTC document editor

You have to run a signalhub server (or some other WebRTC signaling server you modify hyperpad to use). Then just serve up hyperpad using any HTTP server.

# 1. Download and run hyperpad
$ git clone git@github.com:noffle/hyperpad
$ cd hyperpad
@Xeoncross
Xeoncross / mime_wrapper.go
Last active Dec 19, 2018
Basic io.Writer wrapper that appends a newline every 76 characters. Used for MIME multipart email creation. Example based on code by https://github.com/zerkms
View mime_wrapper.go
package main
import (
"encoding/base64"
"io"
"os"
"strings"
)
// MimeWrapWriter adds a newline pair (CRLF) every 76 bytes
@Xeoncross
Xeoncross / ReaderFunc.go
Created Dec 18, 2018 — forked from dolmen/ReaderFunc.go
Proposal to add ReaderFunc to package io
View ReaderFunc.go
/*
https://play.golang.org/p/iWDGHj_-X_
This programs generates 32 random bytes formatted à la "hexdump -C".
It does it in just 2 lines of code using the power of the standard library
and a trick to convert a func Read([]byte) (int, error) into an io.Reader.
Inspiration: see HandlerFunc in net/http