mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-10 00:13:43 +00:00
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 192.
|
|
|
|
// Http2 is an e-commerce server with /list and /price endpoints.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
db := database{"shoes": 50, "socks": 5}
|
|
log.Fatal(http.ListenAndServe("localhost:8000", db))
|
|
}
|
|
|
|
type dollars float32
|
|
|
|
func (d dollars) String() string { return fmt.Sprintf("$%.2f", d) }
|
|
|
|
type database map[string]dollars
|
|
|
|
//!+handler
|
|
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)
|
|
}
|
|
}
|
|
|
|
//!-handler
|