-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing.go
52 lines (42 loc) · 1.37 KB
/
routing.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
47
48
49
50
51
52
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
// URL Parameter
r.HandleFunc("/books/{title}/page/{page}", func(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
title := vars["title"]
page := vars["page"]
fmt.Fprintf(rw, "request [book=%s, page=%s]", title, page)
})
// methods
// GET POST PUT DELETE ...
r.HandleFunc("/books", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "get request [book=%s]", mux.Vars(r)["title"])
}).Methods("GET")
// hostnames & subdomains
r.HandleFunc("/host", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprint(rw, "host www.jaeyeonling.com")
}).Host("www.jaeyeonling.com")
// schemes
// TODO: this code not working (404 not found)
r.HandleFunc("/secure", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprint(rw, "https schemes secure")
}).Schemes("https")
r.HandleFunc("/insecure", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprint(rw, "http schemes insecure")
}).Schemes("http")
// path prefixes & subrouters
router := r.PathPrefix("/route").Subrouter()
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprint(rw, "default routing")
})
router.HandleFunc("/hello", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprint(rw, "hello routing")
})
http.ListenAndServe(":80", r)
}