index
This commit is contained in:
30
080-http-server-static-select/http-server-static-select.go
Normal file
30
080-http-server-static-select/http-server-static-select.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// ## HTTP Server Static Select
|
||||
|
||||
package main
|
||||
|
||||
import "code.google.com/p/gorilla/mux"
|
||||
import "net/http"
|
||||
import "fmt"
|
||||
|
||||
func hello(res http.ResponseWriter, req *http.Request) {
|
||||
res.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintln(res, "Hello")
|
||||
}
|
||||
|
||||
func static(res http.ResponseWriter, req *http.Request) {
|
||||
http.ServeFile(res, req, "./01-hello.go")
|
||||
}
|
||||
|
||||
func notFound(res http.ResponseWriter, req *http.Request) {
|
||||
res.Header().Set("Content-Type", "text/plain")
|
||||
res.WriteHeader(404)
|
||||
fmt.Fprintln(res, "Not found: /" + mux.Vars(req)["path"])
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", hello)
|
||||
r.HandleFunc("/01-hello.go", static)
|
||||
r.HandleFunc("/{path:.*}", notFound)
|
||||
http.ListenAndServe(":5000", r)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
$ go get code.google.com/p/gorilla/mux
|
||||
$ go run xx-http-server-static-select.go
|
||||
|
||||
$ curl -i http://127.0.0.1:5000/
|
||||
$ curl -i http://127.0.0.1:5000/favicon.ico
|
||||
$ curl -i http://127.0.0.1:5000/wat
|
||||
$ curl -i http://127.0.0.1:5000/wat/wat
|
||||
Reference in New Issue
Block a user