Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
karl-gustav / json_decoding_encoding.go
Last active August 22, 2022 08:00
Encode/decode directly into golang http
var subscription Subscription
err := json.NewDecoder(r.Body).Decode(&subscription)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer r.Body.Close()
@karl-gustav
karl-gustav / json_decoding_web_request.go
Last active December 19, 2022 11:38
Decode directly from request in golang webserver
var subscription Subscription
err := json.NewDecoder(r.Body).Decode(&subscription)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer r.Body.Close()
@karl-gustav
karl-gustav / safe_script.bash
Last active January 3, 2020 14:36
safe bash scripts
#######COPY THIS#######
set -euo pipefail
#######################
# Exit immediately when a command fails.
set -e
# Fail if any step in pipe fails, not just the last one
set -o pipefail
@karl-gustav
karl-gustav / useful_commands.sh
Last active February 12, 2020 08:36
Useful commands
# Move pictures from iPhone 7 into separate file structure:
find . -iname '*.JPG' -or -iname '*.JPEG' |while read line; do jhead "$line" |grep "Camera model : iPhone 7" && (mkdir -p $(dirname "${line/./.\/KGR}") ;mv "$line" "${line/./.\/KGR}" ) ; done
@karl-gustav
karl-gustav / timed-ssh-tunnel
Last active October 3, 2020 20:30
SSH tunnel that auto closes after a given time
# Portforward localhost:8080 to port 8200 on the server
ssh -L 8080:localhost:8200 [email protected] sleep 600
@karl-gustav
karl-gustav / webserver.go
Last active September 29, 2023 08:21
go webserver
package main
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
@karl-gustav
karl-gustav / easee charger.py
Last active May 20, 2021 08:53
control easee charger over internet
# Source https://community.home-assistant.io/t/easee-ev-charging-station/186474/7
import requests, json, time
from pprint import pprint
class connect:
def __init__(self,
access_token=None,
):
@karl-gustav
karl-gustav / etc_caddy_Caddyfile
Created September 7, 2021 10:58
Caddyfile used before migrating to traefik
ha.lillesveiven.space {
reverse_proxy 127.0.0.1:8123
}
oc.lillesveiven.space {
reverse_proxy 127.0.0.1:9123
}
nr.lillesveiven.space {
reverse_proxy 127.0.0.1:1880
@karl-gustav
karl-gustav / .vimrc
Created October 5, 2021 06:11
my .vimrc file (mac)
filetype plugin indent on
syntax on
au FileType gitcommit set tw=72
" scrolling in (iTerm2)
:set mouse=a
" Spellchecking in vim
:setlocal spell spelllang=en_us
@karl-gustav
karl-gustav / archive.applescript
Created October 20, 2021 10:45
Script for archiving a message (used in keyboard maestro)
tell application "System Events"
click menu item "Archive" of menu "Message" of menu bar 1 of application process "Mail"
end tell