backend
Technical notes on web development, DevOps, and AI integration.
157 articles
- 19:30backend
The Status Contract Lives in the Query, Not in a Parameter
Public organization detail now goes through GetActiveByID with the status clause inside the query: a missing row and a non-AKTIF row both answer 404.
TL;DR: Organization 17 is inactive. Public requests return 404. Admin requests return the full record.
#go#mysql#rest-api - 19:14backend
Open Routes, Guarded Routes: Where the Permission Seed Goes
Admin routes return 403 for every role except superadmin because the permission rows were never seeded. The guard is only the enforcer.
TL;DR: Admin accounts got 403 errors due to missing permission rows. The permission middleware checked and denied access. Adding permission rows fixed the issue.
#go#security#mysql - 16:10backend
One Transaction or Half-Written Data: BeginTxx in sqlx
Org and member writes ran as separate statements; a mid-way failure left half-written data. One BeginTxx closes the gap.
TL;DR: Kartara had a design flaw. The organisation was written separately from its members. Using transactions fixed this issue.
#go#sqlx#mysql - 16:02backend
400 or 404: the Right Error Status from a Go Service Layer
A Create with an unknown category_slug returns 400, not 404: sentinel errors separate bad input values from missing resources.
TL;DR: HTTP 400 covers unusable requests. HTTP 404 describes absent resources. Error handling uses sentinels.
#go#error-handling#validation - 14:22backend
Go Repository Pattern: Nullable Model, One List
Database model and JSON DTO never share a struct: nullable columns scan into sql.NullX, responses use pointers, one List serves admin and public.
TL;DR: Database columns are stored in one struct. A separate struct handles JSON responses. Null types and escaping prevent silent bugs.
#go#mysql#sqlx - 10:46database
One Table for Many Organisation Types
A single organisations table with nullable per-type columns is cheaper to maintain than one table per type, if you accept three consequences deliberately.
TL;DR: A public service portal needed to support a new institution type. The decision was made to use a single generic table with nullable columns. This approach is known as Single Table Inheritance.
#mysql#database-schema#migration - 22:12backend
Throttling a Public Feedback Endpoint: Two Buckets and One Cap
A public feedback endpoint needs defenses on two axes: a body size cap before JSON decoding and two per-account rate limit buckets mounted after authentication.
TL;DR: A security review flagged a feedback endpoint accepting unlimited payload sizes and submission frequency. Fixes: cap the body at 32 KiB before JSON decoding, mount two per-user rate limit buckets (one per 10 minutes, five per day) after auth middleware, and return 429 with Retry-After. Also normalize nil query results to empty arrays so clients get the promised JSON contract.
#go#security#rate-limit - 20:49backend
One Upload Limit, Four Layers
An 8 MiB upload cap is not one setting. FastAPI never rejects big files; the real gates are the browser, nginx, and Next.js.
TL;DR: An 8.5 MiB upload stalled silently, and debugging showed the FastAPI backend wasn't the culprit. Instead, size limits hide across the whole stack: browser Blob.size, nginx's client_max_body_size, and Next.js's bodySizeLimit, which counts multipart overhead rather than just the file. Consistent enforcement means aligning one limit constant across all four layers.
#file-upload#nginx#fastapi - 04:28backend
Seed Migration and Status Mapping for Government Facility Data Import
Creating new categories through idempotent seed migrations, then importing facility data with status-based publish mapping for existing and planned facilities.
TL;DR: A missing Sarana Budaya category blocked importing eight cultural facilities, so the team added it via an idempotent seed migration using ON DUPLICATE KEY UPDATE. Existing facilities import as published, planned ones as drafts with admin notes. The importer ships with dry-run mode, a development-only guard, and slug-based deduplication for safe, repeatable runs.
#go#mysql#data-import