-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
51 lines (44 loc) · 1.1 KB
/
context.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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"time"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/chronos-tachyon/roxy/lib/mainutil"
)
type RequestContext struct {
Context context.Context
Logger zerolog.Logger
Subsystem string
LocalAddr net.Addr
RemoteAddr net.Addr
XID xid.ID
Impl *Impl
Metrics *Metrics
Request *http.Request
Body WrappedReader
Writer WrappedWriter
StartTime time.Time
EndTime time.Time
FrontendKey string
FrontendConfig *FrontendConfig
}
func (rc *RequestContext) RoxyFrontend() string {
return fmt.Sprintf("%s; %v", rc.FrontendKey, rc.FrontendConfig.Client.Target)
}
func GetRequestContext(ctx context.Context) *RequestContext {
return ctx.Value(mainutil.RequestContextKey).(*RequestContext)
}
func WithRequestContext(ctx context.Context, rc *RequestContext) context.Context {
if ctx == nil {
panic(errors.New("ctx is nil"))
}
if rc == nil {
panic(errors.New("rc is nil"))
}
return context.WithValue(ctx, mainutil.RequestContextKey, rc)
}