Skip to content

Instantly share code, notes, and snippets.

View Integralist's full-sized avatar
🎯
Making an impact

Mark McDonnell Integralist

🎯
Making an impact
View GitHub Profile
@Integralist
Integralist / main.go
Last active July 4, 2025 09:33
Base62 encoding and decoding #go #uuid #serialization
// https://go.dev/play/p/IwFfNFlIq_x
package main
import (
"fmt"
"log"
"github.com/dromara/dongle"
"github.com/google/uuid"
)
@Integralist
Integralist / API ETags.md
Last active June 25, 2025 10:33
Using ETags in your API #etag #api

I like to include ETags (RFC 7232) in an API as a way to distinguish between “Which resource am I targeting?” (e.g. id) and “Which version of the resource am I changing?” (e.g. etag).

The id never changes while the etag changes every time the resource is modified.

The problem I'm trying to solve is both "race conditions" and "lost updates".

For example, a customer has two employees (or automated systems) both looking to make modifications to API-based data. They get the same data at the same time, then attempt to update that data, but the changes they each attempt to make overlaps with each other (maybe user A changes the name field in the data, while user B deletes the name field). This is a problem because the outcome is non-deterministic and user A's changes might be applied last which means the name field is put back instead of deleted.

Supporting ETags requires the API user to provide an If-Match request header with

@Integralist
Integralist / dependabot.yaml
Created June 16, 2025 15:16
Dependabot #dependencies
# Example configuration that's quite detailed in its approach.
# .github/dependabot.yaml
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
day: monday
interval: weekly
@Integralist
Integralist / main.tf
Created June 13, 2025 10:08
Simple Fastly Terraform Subscription #tls #fastly #iac
terraform {
required_providers {
fastly = {
source = "fastly/fastly"
version = "7.0.0"
}
}
required_version = ">= 1.0"
}
@Integralist
Integralist / README.md
Last active May 28, 2025 14:43
Homebrew: custom version install #homebrew #macOS

You can't install specific package versions using Homebrew.

If you have a version installed you can pin it:

brew pin <package>

That will prevent a brew upgrade from updating the package.

@Integralist
Integralist / 1. README.md
Last active May 29, 2025 13:41
Go: httpx.WriteJSON #go #http #json #api

Here is some problematic code...

func WriteJSON(l *slog.Logger, w http.ResponseWriter, r *http.Request, code int, v any) {
	ctx := r.Context()
	w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(code)

	if err := json.NewEncoder(w).Encode(v); err != nil {
 l.LogAttrs(ctx, slog.LevelError, "encode_json_response", slog.Any("err", err))
@Integralist
Integralist / main.go
Last active May 29, 2025 13:41
Go: 1.23 iter.Seq/iter.Seq2 iterators #go #iterator
// This code demonstrates how iterators work in Go.
// This particular example is contrived, but I wanted something simple enough to demonstrate the point.
package main
import (
"fmt"
"iter"
"strings"
)
@Integralist
Integralist / README.md
Last active July 9, 2025 08:51
Go: JSON omitempty vs omitzero #go #json

Guidelines:

Tip

The super quick summary is: use omitzero
Unless you need to identify an empty map/slice/interface, then use omitempty.
If you need to identify if value was deliberately set to the zero type, use a pointer.
If you have specific zero requirements define custom type with IsZero method.

  • to filter out a nil map, use omitzero
  • to filter out a nil map and an empty map, use omitempty
@Integralist
Integralist / example.go
Last active May 30, 2025 08:42
Go: HTTP handler Write error after WriteHeader #go #http #middleware
func exampleHandler(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
// The net/http server itself has a recovery mechanism that specifically looks for http.ErrAbortHandler.
// When it catches this particular panic, it knows to abort the current request handler and close the connection.
if rec == http.ErrAbortHandler {
log.Println("Handler aborted with http.ErrAbortHandler. Connection will be closed by server.")
} else {
log.Printf("Unhandled panic: %v.", rec)
}
@Integralist
Integralist / TLS, Certificate, and ACME Glossary.md
Last active May 29, 2025 13:40
TLS, Certificate, and ACME Glossary #TLS #ACME

TLS, Certificate, and ACME Glossary

This glossary defines common terms related to Transport Layer Security (TLS), digital certificates, Certificate Authorities (CAs), and the Automatic Certificate Management Environment (ACME) protocol. It's intended to help users understand the concepts involved in securing services and using APIs that manage certificate issuance, particularly focusing on Subject Alternative Names (SANs).