go:stdlib:http_server
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| go:stdlib:http_server [2025/12/29 23:29] – phong2018 | go:stdlib:http_server [2025/12/29 23:33] (current) – phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ===== HTTP Server ===== | + | ===== HTTP Server |
| ==== What is it? ==== | ==== What is it? ==== | ||
| An **HTTP server** is a program (service) that: | An **HTTP server** is a program (service) that: | ||
| - | * **listens** on a network | + | * **listens** on an address (host: |
| - | * **handles** incoming HTTP requests, then | + | * accepts incoming HTTP connections/ |
| - | * sends back HTTP responses. | + | |
| + | * writes an HTTP response (status + headers + body). | ||
| - | In Go, you commonly build HTTP servers using the standard library package `net/http`. | + | In Go, the standard library package `net/ |
| ==== What is it used for? ==== | ==== What is it used for? ==== | ||
| - | * Build web APIs (REST) | + | * Build REST APIs and microservices. |
| - | * Serve web pages, JSON, files. | + | * Serve web pages, JSON, files, webhooks. |
| - | * Provide health checks (e.g. `/health`) for monitoring | + | * Provide health checks (e.g. `/health`) for monitoring/load balancers. |
| - | ==== How it works (simple flow) ==== | + | ==== Core building blocks |
| - | | + | |
| - | | + | * `http.Handler` |
| - | - Server calls a **handler** | + | * `http.HandlerFunc` (function adapter) |
| - | - Handler | + | * **ResponseWriter**: writes status code, headers, and body. |
| + | * **Request**: | ||
| + | * **ServeMux (router)**: maps URL paths to handlers | ||
| + | * `http.DefaultServeMux` is the default global mux. | ||
| + | * `http.NewServeMux()` creates a dedicated mux. | ||
| - | ==== Minimal | + | ==== Minimal |
| <code go> | <code go> | ||
| package main | package main | ||
| - | import " | + | import |
| + | " | ||
| + | | ||
| + | ) | ||
| func main() { | func main() { | ||
| - | http.HandleFunc("/ | + | |
| + | |||
| + | mux.HandleFunc("/", | ||
| + | w.Write([]byte(" | ||
| + | }) | ||
| + | |||
| + | mux.HandleFunc("/ | ||
| w.WriteHeader(http.StatusOK) | w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte(" | w.Write([]byte(" | ||
| }) | }) | ||
| - | http.ListenAndServe(": | + | |
| + | log.Fatal(http.ListenAndServe(": | ||
| } | } | ||
| </ | </ | ||
| - | ==== Key concepts | + | ==== Common patterns |
| - | * **Handler**: code that processes a request and writes a response. | + | * **Default mux pattern** (quick demos): use `http.HandleFunc` + `ListenAndServe(..., |
| - | * **ResponseWriter**: used to write headers/ | + | * **Custom mux pattern** (recommended): `mux := http.NewServeMux()` and pass it to server |
| - | * **Request**: contains method, URL, headers, body, context. | + | * **Middleware pattern**: wrap handlers (logging, auth, recover, rate limit) |
| - | * **ServeMux | + | |
| + | ==== Production notes (important) ==== | ||
| + | For production, prefer `http.Server{...}` with timeouts and graceful shutdown. | ||
| + | Time-outs help protect against slow-client attacks and avoid hanging connections. | ||
| ==== Related pages ==== | ==== Related pages ==== | ||
| - | * [[go: | + | * [[go: |
| - | * [[go: | + | * [[go: |
| * [[go: | * [[go: | ||
| * [[go: | * [[go: | ||
| + | * [[go: | ||
| ==== Hard words (English) ==== | ==== Hard words (English) ==== | ||
| Line 52: | Line 71: | ||
| * **service** / | * **service** / | ||
| * **listen** / | * **listen** / | ||
| - | * **handle** /ˈhændəl/: xử lý | + | * **accept** /əkˈsept/: chấp nhận kết nối/ |
| - | * **request** /rɪˈkwest/: yêu cầu | + | * **handler** /ˈhændlər/: bộ xử lý request |
| - | * **response** /rɪˈspɑːns/: phản hồi | + | * **interface** /ˈɪntərfeɪs/: giao diện |
| - | * **status code** /ˈsteɪtəs koʊd/: mã trạng thái (200, 404, 500...) | + | * **adapter** /əˈdæptər/: bộ chuyển/đệm để khớp kiểu |
| - | * **header** /ˈhedər/: header (thông tin kèm theo) | + | |
| - | * **body** /ˈbɑːdi/: nội dung | + | |
| * **router** / | * **router** / | ||
| - | * **health check** /helθ tʃek/: kiểm tra tình trạng | + | * **multiplexer (mux)** /ˈmʌltɪˌpleksər/: |
| + | * **webhook** / | ||
| + | * **load balancer** /ˈloʊd ˌbælənser/: | ||
| + | * **middleware** / | ||
| + | * **graceful shutdown** / | ||
| + | * **timeout** / | ||
| + | * **hanging** / | ||
go/stdlib/http_server.txt · Last modified: by phong2018
