บทที่ 15 · Part 4 — APIs and Boundaries

HTTP APIs, Routers and OpenAPI

เลือก ServeMux, Chi, Gin, Echo หรือ Fiber และวาง oapi-codegen เป็น contract boundary ที่ถูกต้อง

ในปี 2026 การเริ่ม Go API ไม่ได้มีคำถามแค่ว่า “ใช้ framework อะไรเร็วที่สุด” แต่ต้องถามว่า contract อยู่ที่ไหน, middleware ใช้ ecosystem ใด, validation ครบแค่ไหน และทีมยอมรับ migration cost เท่าไร Router ที่ benchmark ชนะไม่ได้ช่วยถ้า generated interface ไม่ตรง spec หรือ cancellation ไม่ถึง service

จบบทนี้คุณจะ

  • เลือก net/http, Chi, Gin, Echo หรือ Fiber จาก constraints ที่ตรวจสอบได้
  • ใช้ oapi-codegen แบบ spec-first โดยไม่เข้าใจผิดว่า strict server validate ทุกอย่างแล้ว
  • แยก generated transport types ออกจาก application model

Course Default สำหรับ HTTP API

คอร์สนี้เลือก net/http + Chi + oapi-codegen strict server + request-validation middleware เป็น default สำหรับ service ใหม่ที่ใช้ OpenAPI 3.0 เหตุผลคือ Chi เข้ากับ http.Handler โดยตรง, middleware จาก stdlib ecosystem ใช้ร่วมได้, API surface เล็ก และย้ายกลับ ServeMux ได้ง่ายกว่า framework ที่มี context/model ของตัวเอง

generated code เป็น transport boundary ไม่ควรไหลเข้า domain/application ทุกชั้น Handler แปลง CreateBatchRequest เป็น settlement.SubmitRequest, เรียก use case แล้วแปลง application error เป็น documented response การแยกนี้ทำให้ regenerate spec ไม่กระจาย field tags และ nullable wrapper ไปทั่วระบบ

เลือก Router หรือ Framework อย่างไร

ตัวเลือกเหมาะเมื่อCourse stance ปี 2026
Go 1.22+ http.ServeMuxroute ไม่ซับซ้อน ต้องการ dependency น้อยดีมากสำหรับ service เล็ก; รองรับ method และ wildcard แล้ว
Chiต้องการ route groups/middleware และคง net/http compatibilityRecommended default
Ginทีมมี ecosystem/knowledge เดิม ต้องการ batteries มากขึ้นใช้ได้และ mature; ยอมรับ framework context
Echo v5ทีมเลือก Echo และพร้อม migration จาก v4ใช้ได้; ตรวจ middleware compatibility และ timeline ของ v4
Fiber v3workload พิสูจน์ว่าต้องการ fasthttp model และทีมเข้าใจ semanticsไม่ใช่ default; benchmark กับ endpoint จริงก่อน

Fiber v3 ต้องใช้ Go 1.25+ และสร้างบน fasthttp แม้มี adapter กับ net/http แล้ว context values ของ Fiber ถูก reuse หลัง handler และ package ใช้ unsafe เพื่อ performance บางส่วน สิ่งเหล่านี้ไม่ใช่คำว่า “ไม่ดี” แต่เป็น programming model ที่ทีมต้องรับรู้ การเลือกเพียงจาก hello-world benchmark ละเลย JSON, DB, network latency, allocation ใน handler และ ecosystem compatibility

Gin และ Echo มี community ใหญ่และ productivity ที่ดีเมื่อองค์กรมี standard อยู่แล้ว การบังคับย้ายไป Chi โดยไม่มีปัญหาจริงไม่คุ้ม Course default ใช้เลือกโปรเจกต์ใหม่ ไม่ใช่ migration mandate

oapi-codegen ทำอะไร และไม่ทำอะไร

oapi-codegen สร้าง Go types, client และ server boilerplate จาก OpenAPI; strict server interface ทำให้ handler รับ typed request และต้องคืน response ตาม variant ที่ generate ช่วยลดการเขียน status/body ที่ผิด แต่คำว่า strict ไม่ได้หมายถึง validate incoming request ครบตาม schema

สำหรับ Chi/net/http ให้ใช้ nethttp-middleware จาก ecosystem ของโครงการเพื่อ validate request กับ spec อีกชั้น และกำหนด authentication/authorization เอง Security scheme ในเอกสารไม่ได้บังคับ auth อัตโนมัติ นอกจากนี้ response validation ไม่ได้ถูกแก้ครบด้วย middleware ปกติ จึงต้องมี contract tests และ typed strict responses

สำหรับ API ที่ไม่ต้องใช้ feature ของ OpenAPI 3.1 คอร์สยังเลือก 3.0 เป็น conservative default แต่ oapi-codegen v2.8.0 (กรกฎาคม 2026) เพิ่ม initial OpenAPI 3.1 support แล้ว พร้อม callbacks, webhooks และ nullability บางรูปแบบ ตัว generator รุ่นนี้ต้องใช้ Go 1.25 และ generated code บาง feature ต้องใช้ github.com/oapi-codegen/runtime v1.6.0+ คำว่า initial หมายความว่าทีมยังต้องทดลองกับ spec จริงและตรวจ unsupported schema ไม่ควรสมมติว่า JSON Schema 2020-12 ทุกส่วนรองรับครบ

Pin code generator เป็น tool dependency เพื่อให้ local กับ CI generate ตรงกัน เก็บ spec ไว้ใน source control, validate ก่อน generate และ review generated diff อย่าดึง remote spec ที่ไม่เชื่อถือเข้ามา generate ใน build โดยตรง เพราะ spec เป็น input ที่สามารถกระทบ source code ที่สร้างออกมาได้

ทางเลือก Code-First และ Generated Router

Huma เหมาะเมื่อทีมต้องการ code-first API และสร้าง OpenAPI 3.1 จาก typed operation โดยใช้ adapter กับ ServeMux/Chi ส่วน ogen เน้น generation ที่รวม routing, parsing, validation, serialization และ client/server มากขึ้น ทั้งคู่ควรประเมินจาก generated diff, unsupported schema, middleware integration, error model และ upgrade path ไม่ใช่จำนวน star เพียงอย่างเดียว

Production Toolbox

Default ใหม่: Chi + oapi-codegen strict + request validator ถ้า API เล็กและไม่ต้อง group route มากให้ใช้ ServeMux โดยตรง ถ้าองค์กรใช้ Gin/Echo อยู่ให้รักษาความสม่ำเสมอ Fiber ใช้เมื่อ profile และ compatibility test พิสูจน์ benefit Huma/ogen เป็นทางเลือกเมื่อ contract workflow ต่างจาก spec-first

Checklist ก่อนเปิด Endpoint

  • OpenAPI spec เป็น source of truth และ generator version ถูก pin
  • generated code แยกจาก application/domain model
  • request schema validation, auth และ authorization มี owner แยกชัด
  • timeout/body size/header limits ถูกตั้งที่ server และ middleware
  • error mapping ใช้ stable code ไม่รั่ว internal error
  • framework selection มี ecosystem, migration และ runtime semantics ใน decision
  • มี contract/integration test ของ documented success และ failure responses

อ่านเพิ่ม: Go 1.22 routing enhancements, Chi, Fiber, Huma และ ogen