-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
51 lines (45 loc) · 1.06 KB
/
example_test.go
File metadata and controls
51 lines (45 loc) · 1.06 KB
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
package middleware_test
import (
"crypto/sha1"
"fmt"
"net/http"
"time"
"github.com/qingtao/aksk/v2/core"
"github.com/qingtao/aksk/v2/middleware"
)
func getSecretKey(ak string) (string, error) {
// TODO: get secret_key
return "", nil
}
func handleError(w http.ResponseWriter, err error) {
if err == nil {
return
}
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "%s", err)
}
func hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello world!`))
}
func ExampleNew() {
config := middleware.Config{
// Key 获取密钥, 必须非nil
KeyGetter: getSecretKey,
// 不验证请求Body
SkipBody: false,
// 错误处理函数
ErrorHandler: handleError,
}
opts := []core.Option{
// 编码格式
core.WithEncoder(&core.Base64Encoder{}),
// 哈希算法
core.WithHash(sha1.New),
// 签名的时间戳有效时间范围
core.WithAcceptableSkew(60 * time.Second),
}
middleware := middleware.New(config, opts...)
// 包装http.HandlerFunc
http.Handle("/hello", middleware.HandleFunc(hello))
http.ListenAndServe(":8080", nil)
}