-
Notifications
You must be signed in to change notification settings - Fork 24
/
jsonrpc2.go
62 lines (52 loc) · 1.55 KB
/
jsonrpc2.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
package apix
import (
"context"
"net"
"github.com/powerman/rpc-codec/jsonrpc2"
"github.com/powerman/structlog"
"github.com/sebest/xff"
"github.com/powerman/go-monolith-example/internal/dom"
"github.com/powerman/go-monolith-example/pkg/def"
"github.com/powerman/go-monolith-example/pkg/reflectx"
)
// JSONRPC2Ctx describe JSON-RPC 2.0 Ctx param used by all API methods.
type JSONRPC2Ctx struct {
AccessToken string
jsonrpc2.Ctx
}
// NewContext returns a new context.Context that carries values describing
// this request without any deadline, plus some of the values.
func (c *JSONRPC2Ctx) NewContext(
authn Authn,
service string,
) (
ctx Ctx,
log *structlog.Logger,
methodName string,
auth dom.Auth,
err error,
) {
ctx = c.Context()
remoteIP := "" // non-HTTP RPC call (like in tests)
if r := jsonrpc2.HTTPRequestFromContext(ctx); r != nil {
remoteIP, _, _ = net.SplitHostPort(xff.GetRemoteAddr(r))
}
ctx = context.WithValue(ctx, contextKeyRemoteIP, remoteIP)
methodName = reflectx.CallerMethodName(1)
ctx = context.WithValue(ctx, contextKeyMethodName, methodName)
if c.AccessToken != "" {
accessToken := AccessToken(c.AccessToken)
ctx = context.WithValue(ctx, contextKeyAccessToken, accessToken)
auth, err = authn.Authenticate(ctx, accessToken)
ctx = context.WithValue(ctx, contextKeyAuth, auth)
}
log = structlog.New(
structlog.KeyApp, service,
def.LogRemoteIP, remoteIP,
def.LogFunc, methodName,
def.LogUserName, auth.UserName,
)
ctx = structlog.NewContext(ctx, log)
c.SetContext(ctx)
return ctx, log, methodName, auth, err
}