-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (39 loc) · 1.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"html"
"log"
"net/http"
"github.com/gorilla/mux"
"encoding/json"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/",HandleIndex)
router.HandleFunc("/Domain", HandleDomainIndex)
router.HandleFunc("/Domain/{id}",HandleDomainShow)
fmt.Println("Main tasks:")
log.Fatal(http.ListenAndServe(":80",router))
}
func HandleIndex(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Router test: Hello, %q \n", html.EscapeString(r.URL.Path))
fmt.Fprintf(w,"Router test: Hello, %q", html.EscapeString(r.URL.Path))
}
func HandleDomainIndex(w http.ResponseWriter, r *http.Request) {
userInfo := User {
Name:"zhanghui",
Age:18,
}
fmt.Printf("Router test: %q \n", userInfo)
json.NewEncoder(w).Encode(userInfo)
}
func HandleDomainShow(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
fmt.Printf("Router test: %q \n", id)
fmt.Fprintf(w,"Domain show:", id)
}