-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (48 loc) · 1.79 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
package main
import (
"fmt"
"github.com/blang/methodr"
"net/http"
)
var (
getHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Requested method: %s\n", r.Method)
})
postHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Post me something")
})
notFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
)
const listen = ":9000"
func main() {
// Setup 1: Restrict route to single method
// POST/PUT/... will result in StatusMethodNotAllowed
// Note: HEAD will use GET unless HEAD handler was set.
http.Handle("/foo1", methodr.GET(getHandler))
// Setup 2: Restrict route with custom default handler
// POST/PUT/... will result in StatusNotFound
http.Handle("/foo2", methodr.GET(getHandler).DEFAULT(notFoundHandler))
//NOTE: If you're not happy with the global default handler, you can set it:
// methodr.DefaultHandler = myBadRequestHandler
// Setup 3: Route depending on method
// Use chains to specify different routes, order is nonrelevant.
// PATCH/PUT/... will result in StatusMethodNotAllowed
http.Handle("/foo3", methodr.GET(getHandler).POST(postHandler))
// Equivalent to: http.Handle("/foo3", methodr.POST(postHandler).GET(getHandler))
// Setup 4: Setup more complicated routes
mux := &methodr.Mux{
Get: getHandler,
Post: postHandler,
Patch: postHandler,
Default: notFoundHandler,
}
http.Handle("/foo4", mux)
// Equivalent to:
// http.Handle("/foo4", methodr.GET(getHandler).POST(postHandler).PATCH(postHandler).DEFAULT(notFoundHandler))
fmt.Printf("HTTP Server listening on %s, exit with ctrl+c\n", listen)
if err := http.ListenAndServe(listen, nil); err != nil {
fmt.Printf("HTTP Server exited with: %s\n", err)
}
}