mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
add chapter 15.5 (#681)
This commit is contained in:
56
eBook/examples/chapter_15/robust_webserver.go
Normal file
56
eBook/examples/chapter_15/robust_webserver.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// robust_webserver.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
const form = `<html><body><form action="#" method="post" name="bar">
|
||||
<input type="text" name="in"/>
|
||||
<input type="submit" value="Submit"/>
|
||||
</form></html></body>`
|
||||
|
||||
type HandleFnc func(http.ResponseWriter, *http.Request)
|
||||
|
||||
/* handle a simple get request */
|
||||
func SimpleServer(w http.ResponseWriter, request *http.Request) {
|
||||
io.WriteString(w, "<h1>hello, world</h1>")
|
||||
}
|
||||
|
||||
/* handle a form, both the GET which displays the form
|
||||
and the POST which processes it.*/
|
||||
func FormServer(w http.ResponseWriter, request *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
switch request.Method {
|
||||
case "GET":
|
||||
/* display the form to the user */
|
||||
io.WriteString(w, form)
|
||||
case "POST":
|
||||
/* handle the form data, note that ParseForm must
|
||||
be called before we can extract form data*/
|
||||
//request.ParseForm();
|
||||
//io.WriteString(w, request.Form["in"][0])
|
||||
io.WriteString(w, request.FormValue("in"))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/test1", logPanics(SimpleServer))
|
||||
http.HandleFunc("/test2", logPanics(FormServer))
|
||||
if err := http.ListenAndServe(":8088", nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func logPanics(function HandleFnc) HandleFnc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
defer func() {
|
||||
if x := recover(); x != nil {
|
||||
log.Printf("[%v] caught panic: %v", request.RemoteAddr, x)
|
||||
}
|
||||
}()
|
||||
function(writer, request)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user