Skip to content

Instantly share code, notes, and snippets.

func (app *application) rateLimit(next http.Handler) http.Handler {
if !app.config.limiter.enabled {
return next
}
type client struct {
limiter *rate.Limiter
lastSeen time.Time
}
.
├── cmd
│   ├── cli
│   └── web
├── internal
│   ├── database
│   ├── request
│   ├── response
│   ├── templatefuncs
│   ├── validator
@alexedwards
alexedwards / cache.go
Last active March 17, 2025 22:35
Generic in-memory cache implementation in Go
package cache
import (
"sync"
"time"
)
// Cache is a basic in-memory key-value cache implementation.
type Cache[K comparable, V any] struct {
items map[K]V // The map storing key-value pairs.
@alexedwards
alexedwards / go-update
Created November 22, 2023 14:53
Bash script for updating Go
#!/bin/bash
# Check if the version argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <go_version>"
exit 1
fi
# Version number provided as an argument
version="$1"
@alexedwards
alexedwards / go-update.sh
Last active November 2, 2023 08:59
Bash script to update Go on linux-amd64 (generated using Chat GPT)
#!/bin/bash
# Check if the version argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <go_version>"
exit 1
fi
# Version number provided as an argument
version="$1"
@alexedwards
alexedwards / Makefile
Created May 2, 2023 16:04
Makefile targets for working with sqlite
## db/connect: create to the local database
.PHONY: db/connect
db/connect:
sqlite3 db.sqlite
## db/migrations/new name=$1: create a new migration
.PHONY: db/migrations/new
db/migrations/new:
go run -tags 'sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest create -seq -ext=.sql -dir=./resources/migrations ${name}
@alexedwards
alexedwards / Makefile
Last active March 15, 2025 21:40
Boilerplate Makefile for Go projects
# Change these variables as necessary.
main_package_path = ./cmd/example
binary_name = example
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
name: Audit and deploy
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
name: Audit
on:
push:
branches:
- main
pull_request:
branches:
- main