Skip to content

Instantly share code, notes, and snippets.

View peacefixation's full-sized avatar

Matt Jarvis peacefixation

  • Melbourne, Australia
View GitHub Profile
@peacefixation
peacefixation / gist:bfd3388f63ed04e94dac80a86954daaa
Created July 1, 2019 08:24
Controlling MS PowerPoint with JavaScript
https://docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/powerpoint-quickstart
https://medium.com/@ferrygunawan/controlling-powerpoint-slides-with-alexa-and-websocket-fd3c91c0901b
https://www.npmjs.com/package/slideshow
@peacefixation
peacefixation / mx.go
Created May 8, 2019 02:48
Check valid MX record
import (
"fmt"
"net"
"strings"
)
// isValidEmailMX looks up the MX records for the email address and to check if the domain is valid
func isValidEmailMX(emailAddress string) (bool, error) {
parts := strings.Split(emailAddress, "@")
if len(parts) != 2 {
@peacefixation
peacefixation / aesgcm.go
Created May 7, 2019 00:35
AES-GCM encryption
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
)
@peacefixation
peacefixation / Crypto.java
Last active May 1, 2019 09:55
Encrypt JPA entity fields with AES
package crypto;
import CryptoException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidAlgorithmParameterException;
@peacefixation
peacefixation / trace.go
Created April 10, 2019 03:05
Trace function name, line number
func trace() string {
pc := make([]uintptr, 15)
n := runtime.Callers(2, pc) // 2 -> the calling function's frame details
frames := runtime.CallersFrames(pc[:n])
if frame, ok := frames.Next(); ok {
// frame.File is the full file path
return fmt.Sprintf("%s:%d", frame.Function, frame.Line)
}
return ""
}
@peacefixation
peacefixation / update-join.sql
Created March 27, 2019 04:31
Postgres update join
-- given two tables with a foreign key
-- and the requirement to set a field in tableA to the value of the corresponding field in tableB
-- in this case setting tableA.someField to tableB.someOtherField where tableA.fk_id = tableB.id
-- tableA (
-- id
-- someField
-- fk_id
-- )
@peacefixation
peacefixation / json-marshal.go
Last active March 26, 2019 02:21
Implement MarshalJSON and UnmarshalJSON on a custom type
import (
"fmt"
"strings"
"time"
)
const timeFormat = "2006-01-02 15:04:05"
// JSONTime a wrapper for time.Time that unmarshalls with a custom date format
type JSONTime struct {
@peacefixation
peacefixation / subnet.go
Last active March 27, 2019 01:33
Subnet contains IP
// SubnetContainsIP check if an IP address is part of the subnet given by the CIDR (ie. '192.168.0.0/24')
func SubnetContainsIP(CIDR, addr string) (bool, error) {
_, ipNet, err := net.ParseCIDR(CIDR)
if err != nil {
return false, fmt.Errorf("Error parsing CIDR '%s'", CIDR)
}
host, _, err := net.SplitHostPort(addr)
if err != nil {
return false, fmt.Errorf("Error parsing address '%s'. %v", addr, err)
@peacefixation
peacefixation / middleware.go
Last active March 27, 2019 01:34
HTTP middleware
import "github.com/julienschmidt/httprouter"
// usage
// router.POST("/path", BasicAuth(someRequestHandler))
// BasicAuth middleware for incoming requests that must include basic authentication
func BasicAuth(handler httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
log.Printf("Authorizing request from %s", r.RemoteAddr)
@peacefixation
peacefixation / Dockerfile
Created March 6, 2019 23:41
Debian Postgresql container
FROM debian:stretch
LABEL maintainer="First Last <[email protected]>"
ARG POSTGRES_VER=9.6
ARG TZ=Australia/Melbourne
ARG DEBIAN_FRONTEND=noninteractive
# Ensure Timezone is correct
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone