Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
karl-gustav / fullURL.go
Created September 25, 2022 20:42
Get full URL from http request in go
func fullURL(r *http.Request, overridePath ...string) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
if len(overridePath) != 0 {
return fmt.Sprintf("%s://%s%s", scheme, r.Host, overridePath[0])
}
return fmt.Sprintf("%s://%s%s?%s#%s", scheme, r.Host, r.URL.Path, r.URL.RawQuery, r.URL.Fragment)
@karl-gustav
karl-gustav / stderr
Last active February 8, 2024 11:42
Listening for macOS wakeup events
JIT session error: Symbols not found: [ _OBJC_CLASS_$_NSWorkspace, _NSWorkspaceDidWakeNotification ]
Failed to materialize symbols: { (main, { __swift_FORCE_LOAD_$_swiftObjectiveC_$_watch_for_lock_unlock, _got.$sSY8rawValuexSg03RawB0Qz_tcfCTq, _$sSo18NSNotificationNameaSYSCMA, __swift_FORCE_LOAD_$_swiftos_$_watch_for_lock_unlock, _got.$s8RawValueSYTl, _got.$ss20_SwiftNewtypeWrapperPSYTb, _$s10Foundation12NotificationVIeghn_So14NSNotificationCIeyBhy_TR, _symbolic _____ 21watch_for_lock_unlock18ScreenLockObserverC, _got.$ss20_SwiftNewtypeWrapperMp, _$s21watch_for_lock_unlock18ScreenLockObserverCMn, _$sSo18NSNotificationNameaABSYSCWL, _$s21watch_for_lock_unlock18ScreenLockObserverCMm, _got.$s15_ObjectiveCTypes01_A11CBridgeablePTl, _associated conformance So18NSNotificationNameaSHSCSQ, _associated conformance So18NSNotificationNameas20_SwiftNewtypeWrapperSCSY, _$sSo18NSNotificationNameaABs35_HasCustomAnyHashableRepresentationSCWl, _$sSo18NSNotificationNameas20_SwiftNewtypeWrapperSCMc, _$sS2Ss21_ObjectiveCBridgeab
@karl-gustav
karl-gustav / Makefile
Last active September 29, 2023 08:21
Good example of Makefile for using google run containers with gopass
export DOCKER_BUILDKIT=1
GPC_PROJECT_ID=hkraft-iot
SERVICE_NAME=defibrilator-registry
CONTAINER_NAME=europe-west1-docker.pkg.dev/$(GPC_PROJECT_ID)/cloud-run/$(SERVICE_NAME)
.PHONY: *
run: build
docker run -it -p 8080:8080\
-e CLIENT_ID=$$(gopass hjertestarterregisteret.no/client-id)\
@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 / Dockerfile
Last active September 29, 2023 08:03
Dockerfile for building golang scratch container
FROM golang:1-alpine as builder
RUN apk update
RUN apk add --no-cache ca-certificates git
WORKDIR /app
# Fetch dependencies first; they are less susceptible to change on every build
# and will therefore be cached for speeding up the next build.
COPY go.* .
@karl-gustav
karl-gustav / git_setup.sh
Last active May 9, 2023 06:36
My git setup
git config --global init.defaultBranch main
git config --global alias.clone 'clone'
git config --global alias.br 'branch'
git config --global alias.st 'status'
git config --global alias.co 'checkout'
git config --global alias.ci 'commit'
git config --global alias.pr 'pull --rebase'
git config --global alias.pa 'pull --abort'
git config --global alias.rc 'rebase --continue'
git config --global alias.rs 'rebase --skip'
@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 / main.py
Created December 13, 2022 12:52
Minimal Python3 webserver (zero dependencies)
import http.server
import socketserver
from http import HTTPStatus
import os
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
@karl-gustav
karl-gustav / backup.sh
Created November 13, 2022 10:23
Backup script with aes-256-cbc encryption and automatically spits the output into 4GB chunks (for fat32 file systems)
#! /bin/bash
if ! [ $# -eq 1 ] ;then
echo "Ha mappen du vil ha backup av som argument"
exit 1
fi
echo "Navn på backupen:"
read name
echo "Passord:"
@karl-gustav
karl-gustav / encrypt_decrypt.go
Created September 25, 2022 20:32
AES encryption and decryption with password
// based on https://github.com/isfonzar/filecrypt/blob/master/filecrypt.go
func encryptAES(password, plaintext string) (string, error) {
key := []byte(password)
nonce := make([]byte, 12)
// Randomizing the nonce
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", fmt.Errorf("io.ReadFull(...): %w)", err)
}