-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
68 lines (55 loc) · 1.99 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"log"
"net/http"
"github.com/kataras/muxie"
)
func main() {
mux := muxie.NewMux()
mux.PathCorrection = true
mux.HandleFunc("/users", listUsersWithoutMuxieMethods)
mux.Handle("/user/:id", muxie.Methods().
HandleFunc(http.MethodGet, getUser).
HandleFunc(http.MethodPost, saveUser).
HandleFunc(http.MethodDelete, deleteUser))
/*
muxie.Methods().
HandleFunc("POST, PUT", func(w http.ResponseWriter, r *http.Request) {[...]}
^ can accept many methods for the same handler
^ methods should be separated by comma, comma following by a space or just space
Equivalent to:
mux.HandleFunc("/save", func(w http.ResponseWriter, r *http.Request){
if r.Method != http.MethodPost && r.Method != http.MethodPut {
w.Header().Set("Allow", "POST, PUT")
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
[...]
})
*/
log.Println("Server started at http://localhost:8080\nGET: http://localhost:8080/users\nGET, POST, DELETE: http://localhost:8080/user/:id")
log.Fatal(http.ListenAndServe(":8080", mux))
}
// The `muxie.Methods()` is just a helper for this common matching.
//
// However, you definitely own your route handlers,
// therefore you can easly make these checks manually
// by matching the `r.Method`.
func listUsersWithoutMuxieMethods(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
fmt.Fprintf(w, "GET: List all users\n")
}
func getUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "GET: User details by user ID: %s\n", muxie.GetParam(w, "id"))
}
func saveUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "POST: save user with ID: %s\n", muxie.GetParam(w, "id"))
}
func deleteUser(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "DELETE: remove user with ID: %s\n", muxie.GetParam(w, "id"))
}