@@ -17,11 +17,13 @@ package core
1717import (
1818 "context"
1919 "fmt"
20+ "net/http/httptest"
2021 "regexp"
2122 "strconv"
2223 "strings"
2324
2425 "github.com/l3montree-dev/devguard/internal/database/models"
26+ "github.com/l3montree-dev/devguard/internal/echohttp"
2527 "github.com/l3montree-dev/devguard/internal/utils"
2628
2729 "github.com/ory/client-go"
@@ -592,3 +594,61 @@ func GetBadgeSVG(label string, values []BadgeValues) string {
592594
593595 return sb .String ()
594596}
597+
598+ func GoroutineSafeContext (c Context ) Context {
599+ // create a new context - with only the values
600+ ctx := echohttp .E .NewContext (nil , httptest .NewRecorder ())
601+
602+ // copy all values from the original context that might be needed in goroutines
603+ if thirdParty , ok := c .Get ("thirdPartyIntegration" ).(IntegrationAggregate ); ok {
604+ ctx .Set ("thirdPartyIntegration" , thirdParty )
605+ }
606+
607+ if session , ok := c .Get ("session" ).(AuthSession ); ok {
608+ ctx .Set ("session" , session )
609+ }
610+
611+ if org , ok := c .Get ("organization" ).(models.Org ); ok {
612+ ctx .Set ("organization" , org )
613+ }
614+
615+ if project , ok := c .Get ("project" ).(models.Project ); ok {
616+ ctx .Set ("project" , project )
617+ }
618+
619+ if asset , ok := c .Get ("asset" ).(models.Asset ); ok {
620+ ctx .Set ("asset" , asset )
621+ }
622+
623+ if assetVersion , ok := c .Get ("assetVersion" ).(models.AssetVersion ); ok {
624+ ctx .Set ("assetVersion" , assetVersion )
625+ }
626+
627+ if rbac , ok := c .Get ("rbac" ).(AccessControl ); ok {
628+ ctx .Set ("rbac" , rbac )
629+ }
630+
631+ if authClient , ok := c .Get ("authAdminClient" ).(AdminClient ); ok {
632+ ctx .Set ("authAdminClient" , authClient )
633+ }
634+
635+ // Copy string values that might be needed
636+ if orgSlug , ok := c .Get ("orgSlug" ).(string ); ok {
637+ ctx .Set ("orgSlug" , orgSlug )
638+ }
639+
640+ if projectSlug , ok := c .Get ("projectSlug" ).(string ); ok {
641+ ctx .Set ("projectSlug" , projectSlug )
642+ }
643+
644+ if assetSlug , ok := c .Get ("assetSlug" ).(string ); ok {
645+ ctx .Set ("assetSlug" , assetSlug )
646+ }
647+
648+ // Copy public request flag
649+ if c .Get ("publicRequest" ) != nil {
650+ ctx .Set ("publicRequest" , true )
651+ }
652+
653+ return ctx
654+ }
0 commit comments