Talk is cheap, just show you the code, take a look at this image
running:
How does it work?
Any struct implmented ServeHTTP
is a http.Handler,method ServeHTTP
will be invoked when one request comes in, and it will decide which fucntion(or HandlerFunc) will be invoked for different router path;Here BeeApp.Handlers
is one http.Handler,the implemented code can be found in beego/router.go
Here is one example of ServeHTTP
implementaion
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
)
type Handler struct{}
var handlerFunc map[string]http.HandlerFunc
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("r.RequestURI: %+v\n", r.RequestURI)
if hf, ok := handlerFunc[r.RequestURI]; ok {
hf(w, r)
return
}
fmt.Fprintln(w, "Hello, client, no data were found")
}
func init() {
handlerFunc = make(map[string]http.HandlerFunc)
handlerFunc["/home"] = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client,welcome to home")
}
handlerFunc["/color/city"] = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client,color city")
}
}
func getCheck(url string) (ouput string) {
res, err := http.Get(url)
if err != nil {
panic(err.Error())
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
panic(err.Error())
}
return string(greeting)
}
func main() {
ts := httptest.NewServer(Handler{})
defer ts.Close()
listOfURI := []string{
"/home",
"/color/city",
}
for _, v := range listOfURI {
output := getCheck(ts.URL + v)
fmt.Printf("output: %+v\n", output)
}
}