-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.go
More file actions
80 lines (75 loc) · 2.19 KB
/
header.go
File metadata and controls
80 lines (75 loc) · 2.19 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
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
package dghttp
import (
"net/http"
"strconv"
dgcoll "github.com/darwinOrg/go-common/collection"
"github.com/darwinOrg/go-common/constants"
dgctx "github.com/darwinOrg/go-common/context"
dgsys "github.com/darwinOrg/go-common/sys"
)
func FillHeadersWithDgContext(ctx *dgctx.DgContext, header http.Header) {
profile := dgsys.GetProfile()
if profile != "" {
header[constants.Profile] = []string{profile}
}
if ctx.TraceId != "" {
header[constants.TraceId] = []string{ctx.TraceId}
}
if ctx.UserId > 0 {
header[constants.UID] = []string{strconv.FormatInt(ctx.UserId, 10)}
}
if ctx.OpId > 0 {
header[constants.OpId] = []string{strconv.FormatInt(ctx.OpId, 10)}
}
if ctx.Roles != "" {
header[constants.Roles] = []string{ctx.Roles}
}
if ctx.BizTypes > 0 {
header[constants.BizTypes] = []string{strconv.Itoa(ctx.BizTypes)}
}
if ctx.Platform != "" {
header[constants.Platform] = []string{ctx.Platform}
}
if ctx.Token != "" {
header[constants.Token] = []string{ctx.Token}
}
if ctx.ShareToken != "" {
header[constants.ShareToken] = []string{ctx.ShareToken}
}
if ctx.RemoteIp != "" {
header[constants.RemoteIp] = []string{ctx.RemoteIp}
}
if ctx.CompanyId != 0 {
header[constants.CompanyId] = []string{strconv.FormatInt(ctx.CompanyId, 10)}
}
if ctx.Product > 0 {
header[constants.Product] = []string{strconv.Itoa(ctx.Product)}
}
if len(ctx.Products) > 0 {
header[constants.Products] = []string{dgcoll.JoinInts(ctx.Products, ",")}
}
if len(ctx.DepartmentIds) > 0 {
header[constants.DepartmentIds] = []string{dgcoll.JoinInts(ctx.DepartmentIds, ",")}
}
if ctx.Source != "" {
header[constants.Source] = []string{ctx.Source}
}
if ctx.Since != 0 {
header[constants.Since] = []string{strconv.FormatInt(ctx.Since, 10)}
}
if ctx.OutUserId != "" {
header[constants.OutUserId] = []string{ctx.OutUserId}
}
}
func WriteHeaders(request *http.Request, headers map[string]string) {
if headers != nil && len(headers) > 0 {
for k, v := range headers {
request.Header[k] = []string{v}
}
}
}
func WriteSseHeaders(request *http.Request) {
request.Header.Set("Accept", "text/event-stream")
request.Header.Set("Cache-Control", "no-cache")
request.Header.Set("Connection", "keep-alive")
}