Skip to content

Commit

Permalink
add a v1 and v2 handler that uses different devcycle clients which fe…
Browse files Browse the repository at this point in the history
…tch from v1 and v2 cdn endpoints respectively
  • Loading branch information
elliotCamblor committed Aug 22, 2024
1 parent 508abab commit 7ab8287
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/devcyclehq/sdk-proxy
go 1.20

require (
github.com/devcyclehq/go-server-sdk/v2 v2.17.0
github.com/devcyclehq/go-server-sdk/v2 v2.18.0
github.com/gin-gonic/gin v1.10.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/kr/pretty v0.3.1
github.com/launchdarkly/eventsource v1.7.1
github.com/stretchr/testify v1.9.0
)

Expand All @@ -27,7 +28,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/launchdarkly/eventsource v1.7.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devcyclehq/go-server-sdk/v2 v2.16.1 h1:NZ0vHZhhrPOb2hbU/r5sIIDSG4tPc+TiO/ovJaP0Gk8=
github.com/devcyclehq/go-server-sdk/v2 v2.16.1/go.mod h1:DzKrJ4s2apfphFwB/Aq8YDf7brB+NDr6IxX0TNi2c24=
github.com/devcyclehq/go-server-sdk/v2 v2.17.0 h1:eBvoJesVPYvy7htJBjhIWRGbQq5fsmlcv4nvsrHqPDU=
github.com/devcyclehq/go-server-sdk/v2 v2.17.0/go.mod h1:DzKrJ4s2apfphFwB/Aq8YDf7brB+NDr6IxX0TNi2c24=
github.com/devcyclehq/go-server-sdk/v2 v2.18.0 h1:9STdu/bTnrjM1cFh3OJddO4nxAaAUqlOAO180x6SWpk=
github.com/devcyclehq/go-server-sdk/v2 v2.18.0/go.mod h1:DzKrJ4s2apfphFwB/Aq8YDf7brB+NDr6IxX0TNi2c24=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
3 changes: 1 addition & 2 deletions http_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ func BatchEvents() gin.HandlerFunc {
}
}

func GetConfig() gin.HandlerFunc {
func GetConfig(client *devcycle.Client) gin.HandlerFunc {
return func(c *gin.Context) {
instance := c.Value("instance").(*ProxyInstance)
client := c.Value("devcycle").(*devcycle.Client)

if c.Param("sdkKey") == "" || !strings.HasSuffix(c.Param("sdkKey"), ".json") {
c.AbortWithStatus(http.StatusForbidden)
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ func (i *ProxyInstance) BuildDevCycleOptions() *devcycle.Options {
EnableBetaRealtimeUpdates: i.SSEEnabled,
AdvancedOptions: devcycle.AdvancedOptions{
OverridePlatformData: &i.PlatformData,
OverrideConfigWithV1: false,
},
ClientEventHandler: i.sseEvents,
}
options.CheckDefaults()
return &options
}

func (i *ProxyInstance) BuildDevCycleV1Options() *devcycle.Options {
options := i.BuildDevCycleOptions()
options.AdvancedOptions.OverrideConfigWithV1 = true
return options
}

func (i *ProxyInstance) EventRebroadcaster() {
for event := range i.sseEvents {
if event.EventType == api.ClientEventType_RealtimeUpdates {
Expand Down
20 changes: 16 additions & 4 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ func NewBucketingProxyInstance(instance *ProxyInstance) (*ProxyInstance, error)
}

options := instance.BuildDevCycleOptions()
optionsV1 := instance.BuildDevCycleV1Options()
client, err := devcycle.NewClient(instance.SDKKey, options)
if err != nil {
return nil, fmt.Errorf("error creating DevCycle client: %v", err)
}

clientV1, err := devcycle.NewClient(instance.SDKKey, optionsV1)
if err != nil {
return nil, fmt.Errorf("error creating DevCycle V1 client: %v", err)
}
instance.dvcClient = client

r := newRouter(client, instance)
r := newRouter(client, clientV1, instance)

if instance.HTTPEnabled {
if instance.HTTPPort == 0 {
Expand Down Expand Up @@ -94,7 +103,7 @@ func sdkProxyMiddleware(instance *ProxyInstance) gin.HandlerFunc {
}
}

func newRouter(client *devcycle.Client, instance *ProxyInstance) *gin.Engine {
func newRouter(client *devcycle.Client, clientV1 *devcycle.Client, instance *ProxyInstance) *gin.Engine {
r := gin.New()

r.Use(gin.Logger())
Expand All @@ -116,9 +125,12 @@ func newRouter(client *devcycle.Client, instance *ProxyInstance) *gin.Engine {
}
configCDNv1 := r.Group("/config/v1")
{
configCDNv1.GET("/server/:sdkKey", GetConfig())
configCDNv1.GET("/server/:sdkKey", GetConfig(clientV1))
}
configCDNv2 := r.Group("/config/v2")
{
configCDNv2.GET("/server/:sdkKey", GetConfig(client))
}

r.GET("/event-stream", SSE())

return r
Expand Down

0 comments on commit 7ab8287

Please sign in to comment.