-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexmples_test.go
153 lines (135 loc) · 3.37 KB
/
exmples_test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gear_test
import (
"fmt"
"log"
"net/http"
"github.com/mkch/gear"
)
func ExampleWrap() {
var handler http.Handler = gear.Wrap(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
}))
http.Handle("/", handler)
}
func ExampleWrapFunc() {
var handler http.Handler = gear.WrapFunc(
func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
})
http.Handle("/", handler)
}
func ExampleListenAndServe() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
})
gear.ListenAndServe(":8080", nil)
}
func ExampleListenAndServeTLS() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
})
gear.ListenAndServeTLS(":8080", "certfile", "keyfile", nil)
}
func ExampleWrapServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
})
var server = gear.WrapServer(&http.Server{})
server.ListenAndServe()
}
func ExampleNewTestServer() {
var server = gear.NewTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var g = gear.G(r)
// Use g here.
_ = g
}))
defer server.Close()
}
func ExampleMiddlewareFunc() {
var logMiddleware = gear.MiddlewareFuncWitName(func(g *gear.Gear, next func(*gear.Gear)) {
// Pre-processing.
log.Printf("Before request: Path=%v", g.R.URL.Path)
// Call the real handler.
next(g)
// Post-processing.
log.Printf("After request: Path=%v", g.R.URL.Path)
}, "logger")
gear.ListenAndServe(":80", nil, logMiddleware)
}
func ExamplePanicRecovery() {
gear.ListenAndServe(":80", nil, gear.PanicRecovery(true))
}
func ExamplePathInterceptor() {
var handler = gear.MiddlewareFunc(func(g *gear.Gear, next func(*gear.Gear)) {
// Do admin authentication.
var authOK bool
if !authOK {
http.Error(g.W, "", http.StatusUnauthorized)
g.Stop()
}
})
// "/admin" and all paths starts with "/admin/" will be intercepted by handler.
gear.ListenAndServe(":80", nil, gear.NewPathInterceptor("/admin", handler))
}
func adminAuth() bool { return false }
func op1() {}
func ExampleGroup() {
gear.NewGroup("/admin", nil, gear.MiddlewareFunc(func(g *gear.Gear, next func(*gear.Gear)) {
// Handle admin authentication
if !adminAuth() {
http.Error(g.W, "", http.StatusUnauthorized)
return
}
// OK, go ahead.
next(g)
})).Handle("op1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// The request path will be /admin/op1
op1() // Do the operation.
}))
}
func ExampleLogIfErr() {
var (
g *gear.Gear
v any
) // From somewhere else.
gear.LogIfErr(g.JSON(v))
}
func ExampleLogIfErrT() {
gear.LogIfErrT(fmt.Println("msg"))
}
func ExampleLogger() {
// logger logs HTTP method, host, URL(by default) and User-Agent header for request.
logger := gear.Logger(&gear.LoggerOptions{
HeaderKeys: []string{"User-Agent"},
})
gear.ListenAndServe(":http", nil, logger)
}
func ExampleGear_DecodeBody() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
type User struct {
Name string `map:"user"`
ID int `map:"id"`
}
var user User
gear.G(r).DecodeBody(&user)
// If the client posts a body:
//
// {
// "user": "USER1",
// "id": 100,
// }
//
// user equals User{"USER1", 100}
})
}