-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.go
64 lines (56 loc) · 1.62 KB
/
net.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
package sparkle
import (
"context"
"fmt"
entitiesv1 "github.com/octofoxio/sparkle/pkg/entities/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"net"
"net/http"
)
var (
AuthorizationContextKey = "authorization"
UserRecordContextKey = "user-record"
)
func AppendUserProfileToContext(ctx context.Context, record *entitiesv1.UserRecord) context.Context {
return context.WithValue(ctx, UserRecordContextKey, record)
}
func GetUserProfileFromContext(ctx context.Context) (out *entitiesv1.UserRecord, ok bool) {
if ctx.Value(UserRecordContextKey) == nil {
return nil, false
}
if u, ok := ctx.Value(UserRecordContextKey).(*entitiesv1.UserRecord); ok {
return u, true
} else {
return nil, false
}
}
func AppendAccessTokenToOutgoingContext(ctx context.Context, accessToken string) context.Context {
return metadata.AppendToOutgoingContext(ctx, AuthorizationContextKey, accessToken)
}
func GetAccessTokenFromIncomingContext(ctx context.Context) (string, bool) {
md, isOk := metadata.FromIncomingContext(ctx)
if !isOk {
return "", false
}
data := md.Get(AuthorizationContextKey)
if len(data) == 0 {
return "", false
}
return data[0], true
}
func MustListenAndServeTCP(server *grpc.Server, port string) {
fmt.Printf("====== TCP service start at port %s ======\n", port)
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
panic(err)
}
_ = server.Serve(lis)
}
func MustListenAndServeHTTP(handler http.Handler, port string) {
fmt.Printf("====== HTTP service start at port %s ======\n", port)
err := http.ListenAndServe(fmt.Sprintf(":%s", port), handler)
if err != nil {
panic(err)
}
}