mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-11-01 10:41:35 +00:00
fmt code
This commit is contained in:
108
ch7/ch7-07.md
108
ch7/ch7-07.md
@@ -3,11 +3,11 @@
|
||||
在第一章中,我們粗略的了解了怎麽用net/http包去實現網絡客戶端(§1.5)和服務器(§1.7)。在這個小節中,我們會對那些基於http.Handler接口的服務器API做更進一步的學習:
|
||||
|
||||
```go
|
||||
// net/http
|
||||
net/http
|
||||
package http
|
||||
|
||||
type Handler interface {
|
||||
ServeHTTP(w ResponseWriter, r *Request)
|
||||
ServeHTTP(w ResponseWriter, r *Request)
|
||||
}
|
||||
|
||||
func ListenAndServe(address string, h Handler) error
|
||||
@@ -18,10 +18,10 @@ ListenAndServe函數需要一個例如“localhost:8000”的服務器地址,
|
||||
想象一個電子商務網站,爲了銷售它的數據庫將它物品的價格映射成美元。下面這個程序可能是能想到的最簡單的實現了。它將庫存清單模型化爲一個命名爲database的map類型,我們給這個類型一個ServeHttp方法,這樣它可以滿足http.Handler接口。這個handler會遍歷整個map併輸出物品信息。
|
||||
|
||||
```go
|
||||
// gopl.io/ch7/http1
|
||||
gopl.io/ch7/http1
|
||||
func main() {
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", db))
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", db))
|
||||
}
|
||||
|
||||
type dollars float32
|
||||
@@ -31,9 +31,9 @@ func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
|
||||
type database map[string]dollars
|
||||
|
||||
func (db database) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -56,26 +56,26 @@ socks: $5.00
|
||||
目前爲止,這個服務器不考慮URL隻能爲每個請求列出它全部的庫存清單。更眞實的服務器會定義多個不同的URL,每一個都會觸發一個不同的行爲。讓我們使用/list來調用已經存在的這個行爲併且增加另一個/price調用表明單個貨品的價格,像這樣/price?item=socks來指定一個請求參數。
|
||||
|
||||
```go
|
||||
// gopl.io/ch7/http2
|
||||
gopl.io/ch7/http2
|
||||
func (db database) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
switch req.URL.Path {
|
||||
case "/list":
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
case "/price":
|
||||
item := req.URL.Query().Get("item")
|
||||
price, ok := db[item]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such item: %q\n", item)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s\n", price)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such page: %s\n", req.URL)
|
||||
}
|
||||
switch req.URL.Path {
|
||||
case "/list":
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
case "/price":
|
||||
item := req.URL.Query().Get("item")
|
||||
price, ok := db[item]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such item: %q\n", item)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s\n", price)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such page: %s\n", req.URL)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -115,32 +115,32 @@ no such page: /help
|
||||
在下面的程序中,我們創建一個ServeMux併且使用它將URL和相應處理/list和/price操作的handler聯繫起來,這些操作邏輯都已經被分到不同的方法中。然後我門在調用ListenAndServe函數中使用ServeMux最爲主要的handler。
|
||||
|
||||
```go
|
||||
// gopl.io/ch7/http3
|
||||
gopl.io/ch7/http3
|
||||
func main() {
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/list", http.HandlerFunc(db.list))
|
||||
mux.Handle("/price", http.HandlerFunc(db.price))
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", mux))
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/list", http.HandlerFunc(db.list))
|
||||
mux.Handle("/price", http.HandlerFunc(db.price))
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", mux))
|
||||
}
|
||||
|
||||
type database map[string]dollars
|
||||
|
||||
func (db database) list(w http.ResponseWriter, req *http.Request) {
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
for item, price := range db {
|
||||
fmt.Fprintf(w, "%s: %s\n", item, price)
|
||||
}
|
||||
}
|
||||
|
||||
func (db database) price(w http.ResponseWriter, req *http.Request) {
|
||||
item := req.URL.Query().Get("item")
|
||||
price, ok := db[item]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such item: %q\n", item)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s\n", price)
|
||||
item := req.URL.Query().Get("item")
|
||||
price, ok := db[item]
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound) // 404
|
||||
fmt.Fprintf(w, "no such item: %q\n", item)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "%s\n", price)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -155,13 +155,13 @@ func(w http.ResponseWriter, req *http.Request)
|
||||
語句http.HandlerFunc(db.list)是一個轉換而非一個函數調用,因爲http.HandlerFunc是一個類型。它有如下的定義:
|
||||
|
||||
```go
|
||||
// net/http
|
||||
net/http
|
||||
package http
|
||||
|
||||
type HandlerFunc func(w ResponseWriter, r *Request)
|
||||
|
||||
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
|
||||
f(w, r)
|
||||
f(w, r)
|
||||
}
|
||||
```
|
||||
|
||||
@@ -170,7 +170,7 @@ HandlerFunc顯示了在Go語言接口機製中一些不同尋常的特點。這
|
||||
因爲handler通過這種方式註冊非常普遍,ServeMux有一個方便的HandleFunc方法,它幫我們簡化handler註冊代碼成這樣:
|
||||
|
||||
```go
|
||||
// gopl.io/ch7/http3a
|
||||
gopl.io/ch7/http3a
|
||||
mux.HandleFunc("/list", db.list)
|
||||
mux.HandleFunc("/price", db.price)
|
||||
```
|
||||
@@ -182,17 +182,17 @@ mux.HandleFunc("/price", db.price)
|
||||
然後服務器的主函數可以簡化成:
|
||||
|
||||
```go
|
||||
// gopl.io/ch7/http4
|
||||
gopl.io/ch7/http4
|
||||
func main() {
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
http.HandleFunc("/list", db.list)
|
||||
http.HandleFunc("/price", db.price)
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", nil))
|
||||
db := database{"shoes": 50, "socks": 5}
|
||||
http.HandleFunc("/list", db.list)
|
||||
http.HandleFunc("/price", db.price)
|
||||
log.Fatal(http.ListenAndServe("localhost:8000", nil))
|
||||
}
|
||||
```
|
||||
|
||||
最後,一個重要的提示:就像我們在1.7節中提到的,web服務器在一個新的協程中調用每一個handler,所以當handler獲取其它協程或者這個handler本身的其它請求也可以訪問的變量時一定要使用預防措施比如鎖機製。我們後面的兩章中講到併發相關的知識。
|
||||
|
||||
練習 7.11:增加額外的handler讓客服端可以創建,讀取,更新和刪除數據庫記録。例如,一個形如/update?item=socks&price=6的請求會更新庫存清單里一個貨品的價格併且當這個貨品不存在或價格無效時返迴一個錯誤值。(註意:這個脩改會引入變量同時更新的問題)
|
||||
**練習 7.11:** 增加額外的handler讓客服端可以創建,讀取,更新和刪除數據庫記録。例如,一個形如/update?item=socks&price=6的請求會更新庫存清單里一個貨品的價格併且當這個貨品不存在或價格無效時返迴一個錯誤值。(註意:這個脩改會引入變量同時更新的問題)
|
||||
|
||||
練習 7.12:脩改/list的handler讓它把輸出打印成一個HTML的表格而不是文本。html/template包(§4.6)可能會對你有幫助。
|
||||
**練習 7.12:** 脩改/list的handler讓它把輸出打印成一個HTML的表格而不是文本。html/template包(§4.6)可能會對你有幫助。
|
||||
|
||||
Reference in New Issue
Block a user