goext/ginext/appContext.go

60 lines
1.2 KiB
Go
Raw Normal View History

2023-07-18 15:12:06 +02:00
package ginext
import (
"context"
"github.com/gin-gonic/gin"
"time"
)
type AppContext struct {
inner context.Context
cancelFunc context.CancelFunc
cancelled bool
2023-07-18 15:59:12 +02:00
GinContext *gin.Context
2023-07-18 15:12:06 +02:00
}
func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext {
for key, value := range g.Keys {
2023-07-27 09:58:10 +02:00
innerCtx = context.WithValue(innerCtx, key, value)
}
2023-07-18 15:12:06 +02:00
return &AppContext{
inner: innerCtx,
cancelFunc: cancelFn,
cancelled: false,
2023-07-18 15:59:12 +02:00
GinContext: g,
2023-07-18 15:12:06 +02:00
}
}
func (ac *AppContext) Deadline() (deadline time.Time, ok bool) {
return ac.inner.Deadline()
}
func (ac *AppContext) Done() <-chan struct{} {
return ac.inner.Done()
}
func (ac *AppContext) Err() error {
return ac.inner.Err()
}
func (ac *AppContext) Value(key any) any {
return ac.inner.Value(key)
}
func (ac *AppContext) Set(key, value any) {
2023-07-27 09:58:10 +02:00
ac.inner = context.WithValue(ac.inner, key, value)
}
2023-07-18 15:12:06 +02:00
func (ac *AppContext) Cancel() {
ac.cancelled = true
ac.cancelFunc()
}
func (ac *AppContext) RequestURI() string {
2023-07-18 15:59:12 +02:00
if ac.GinContext != nil && ac.GinContext.Request != nil {
return ac.GinContext.Request.Method + " :: " + ac.GinContext.Request.RequestURI
2023-07-18 15:12:06 +02:00
} else {
return ""
}
}