12/04/2026 18:16น.

Ep.21 Go กับ HTTP Middleware ควบคุมทุก Request อย่างมือโปร!
#Go
#Golang
#ภาษา Go
#HTTP
#Middleware
#การจัดการ Request
#การตรวจสอบสิทธิ์
#การบันทึก Log
#การพัฒนาเว็บ
#การพัฒนาโปรแกรม
#การศึกษาการเขียนโปรแกรม
#การเขียนโปรแกรม
#การเขียนโปรแกรม Go
#การเขียนโปรแกรมสำหรับมือใหม่
#ฝึกเขียนโปรแกรม
#ภาษาโปรแกรม
#โปรแกรม
#โปรแกรมเมอร์
#Superdev School
Go กับ HTTP Middleware ควบคุมทุก Request อย่างมือโปร!
Middleware คืออะไร?
Middleware คือ ส่วนที่ทำหน้าที่จัดการ Request หรือ Response ก่อนที่มันจะถึง Handler หลักของเรา เช่น การตรวจสอบสิทธิ์ (Authentication), การบันทึก Log, หรือการจัดการ Header
ตัวอย่างการทำงานของ Middleware :
- รับ Request จากผู้ใช้
- ตรวจสอบหรือปรับแต่ง Request (เช่น ตรวจสอบ Token)
- ส่งต่อไปยัง Handler หลัก
- หลังจาก Handler ทำงานเสร็จ Middleware อาจปรับแต่ง Response ก่อนส่งกลับผู้ใช้
การสร้าง Middleware แบบง่ายๆ ใน Go
ตัวอย่างการสร้าง Middleware :
ในตัวอย่างนี้ :
loggingMiddleware ทำหน้าที่บันทึก Log ของทุก Request ที่เข้ามา
Middleware จะทำงานก่อน helloHandler
package main
import (
"fmt"
"net/http"
)
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Request: %s %s\n", r.Method, r.URL.Path)
next.ServeHTTP(w, r) // ส่ง Request ไปยัง Handler ถัดไป
})
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
}
func main() {
mux := http.NewServeMux()
mux.Handle("/", loggingMiddleware(http.HandlerFunc(helloHandler)))
fmt.Println("Server started at :8080")
http.ListenAndServe(":8080", mux)
}
Middleware สำหรับการตรวจสอบสิทธิ์ (Authentication Middleware)
Middleware สามารถใช้ตรวจสอบ Token เพื่อยืนยันว่าผู้ใช้มีสิทธิ์ในการเข้าถึงหรือไม่
ในตัวอย่างนี้ :
authMiddleware ตรวจสอบว่ามี Authorization Header หรือไม่ และมีค่าเป็น valid-token หรือไม่
หากไม่ผ่าน จะส่งข้อความ "Unauthorized" พร้อมสถานะ 401
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token != "valid-token" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r) // ส่งต่อไปยัง Handler ถ้าสิทธิ์ถูกต้อง
})
}
func main() {
mux := http.NewServeMux()
mux.Handle("/", authMiddleware(http.HandlerFunc(helloHandler)))
fmt.Println("Server started at :8080")
http.ListenAndServe(":8080", mux)
}
Middleware หลายตัวทำงานร่วมกัน
เราสามารถใช้ Middleware หลายตัวต่อกัน (Chaining) เพื่อเพิ่มการควบคุม Request และ Response ได้
ตัวอย่าง Middleware หลายตัว :
ในตัวอย่างนี้ :
ใช้ chainMiddleware เพื่อเชื่อมต่อ Middleware หลายตัว
เรียงลำดับการทำงานของ Middleware จากบนลงล่าง
func chainMiddleware(h http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler {
for _, middleware := range middlewares {
h = middleware(h)
}
return h
}
func main() {
mux := http.NewServeMux()
mux.Handle("/", chainMiddleware(
http.HandlerFunc(helloHandler),
loggingMiddleware,
authMiddleware,
))
fmt.Println("Server started at :8080")
http.ListenAndServe(":8080", mux)
}
กรณีการใช้งาน Middleware ที่พบบ่อย
- การตรวจสอบสิทธิ์ (Authentication)
- การบันทึก Log (Logging)
- การจัดการ CORS (Cross-Origin Resource Sharing)
- การบีบอัดข้อมูล (Compression)
- การจัดการ Error (Error Handling)
สรุปง่ายๆ
- Middleware ช่วยจัดการ Request/Response ก่อนและหลังถึง Handler หลัก
- ใช้ Middleware ได้หลายตัวพร้อมกัน (Chaining) เพื่อจัดการหลายขั้นตอน
- Middleware เหมาะสำหรับการจัดการที่ต้องทำซ้ำๆ เช่น การตรวจสอบสิทธิ์และการบันทึก Log