Skip to content

Instantly share code, notes, and snippets.

View tarampampam's full-sized avatar
🍻
go test && go build && go drink-beer

Pаramtamtām tarampampam

🍻
go test && go build && go drink-beer
View GitHub Profile
@tarampampam
tarampampam / fallback404.go
Last active June 7, 2026 10:31
SPA (Single Page App) file server in Go
package frontend
import (
"bytes"
_ "embed"
"io"
"io/fs"
"time"
)
@tarampampam
tarampampam / hasher.go
Last active June 7, 2026 10:31
Optimized MD5 hasher in Go (with auto-memory-free)
package pkg
import (
"crypto/md5" //nolint:gosec // MD5 is used for non-cryptographic hashing of filesystem paths
"encoding/hex"
"runtime"
"sync"
"weak"
)
@tarampampam
tarampampam / compress.go
Last active June 7, 2026 10:32
Compressor in Go
package pkg
import (
"compress/gzip"
"io"
)
// Compressor handles compression and decompression of data, and provides filename transformation for compressed
// files.
type Compressor interface {
@tarampampam
tarampampam / cache-idb.test.ts
Created March 13, 2026 12:47
A simple cache implementation in TypeScript that uses IndexedDB for storage if available, and falls back to an in-memory Map if not.
import { CacheIDB } from './cache-idb'
import { IDBFactory, IDBKeyRange } from 'fake-indexeddb'
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
const tests = () => {
let cache: CacheIDB<string>
beforeEach(() => {
vi.useFakeTimers({ toFake: ['Date', 'setInterval', 'clearInterval'] })
cache = new CacheIDB()
@tarampampam
tarampampam / middleware.go
Last active October 12, 2025 20:26
Golang HTTP middleware for dealing with Server-Timing header
// Package serverTiming implements a small Server-Timing middleware and helpers to measure named timings during HTTP
// request handling and emit them as the `Server-Timing` response header as specified by the Performance API.
package serverTiming
import (
"bufio"
"context"
"errors"
"fmt"
"maps"
@tarampampam
tarampampam / README.md
Last active September 22, 2025 16:08
Resolve public IP in Golang

Small, dependency-free Go helper to discover your machine’s public IPv4 quickly and reliably. It races three strategies under a short overall timeout and returns the first winner:

  • OpenDNS (myip.opendns.com A lookup via OpenDNS resolvers)
  • Google DNS (TXT on o-o.myaddr.l.google.com via Google authoritative NS)
  • HTTP (plain-text IP from several well-known endpoints)

Each strategy is isolated with short dial/read timeouts and early cancellation. You can supply your own resolvers and tweak the overall budget via functional options.

Usage example

@tarampampam
tarampampam / .gitlab-ci.yml
Created May 6, 2024 09:04
FFmpeg static binary build using docker
# GitLab CI help: <https://docs.gitlab.com/ee/ci/yaml/>
# GitLab environments: <https://docs.gitlab.com/ee/ci/variables/>
variables:
GITLAB_IMAGE: $CI_REGISTRY_IMAGE
GITHUB_IMAGE: ...
stages:
- build
@tarampampam
tarampampam / run.bash
Created December 15, 2023 08:31
Enable dark mode on linux, including Google Chrome
$ gsettings get org.gnome.desktop.interface color-scheme
# default
$ gsettings set org.gnome.desktop.interface color-scheme prefer-dark
@tarampampam
tarampampam / handlers.go
Last active January 6, 2023 10:25
Golang simple RPC client/server
package rpc
type (
API struct{}
LivenessArgs struct{}
LivenessReply struct{ Ok bool }
)
// NewAPI returns a new API instance.
@tarampampam
tarampampam / syncmap.go
Last active December 2, 2024 08:00
Golang SyncMap (sync.Map with generics, type-safe)
package syncmap
import "sync"
// SyncMap is like a Go sync.Map but type-safe using generics.
//
// The zero SyncMap is empty and ready for use. A SyncMap must not be copied after first use.
type SyncMap[K comparable, V any] struct {
mu sync.Mutex
m map[K]V