Skip to content

Commit

Permalink
feat(BRIDGE-119): added feature flag relevant API calls; added Featur…
Browse files Browse the repository at this point in the history
…e Flag route to fake server
  • Loading branch information
ElectroNafta committed Aug 19, 2024
1 parent 218840f commit 149e501
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
19 changes: 19 additions & 0 deletions client_features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package proton

import (
"context"

"github.com/go-resty/resty/v2"
)

func (c *Client) GetFeatures(ctx context.Context) (FeatureFlagResult, error) {
res := FeatureFlagResult{}

if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get("/feature/v2/frontend")
}); err != nil {
return FeatureFlagResult{}, err
}

return res, nil
}
26 changes: 26 additions & 0 deletions manager_features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package proton

import (
"context"
)

type FeatureFlagResult struct {
Code int `json:"Code"`
Toggles []FeatureToggle `json:"toggles"`
}

type FeatureToggle struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
}

func (m *Manager) GetFeatures(ctx context.Context) (FeatureFlagResult, error) {
responseData := FeatureFlagResult{}

_, err := m.r(ctx).SetResult(&responseData).Get("/feature/v2/frontend")
if err != nil {
return FeatureFlagResult{}, err
}

return responseData, nil
}
21 changes: 21 additions & 0 deletions server/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,27 @@ func (b *Backend) GenerateContactID(userID string) (string, error) {
})
}

func (b *Backend) GetFeatureFlags() []proton.FeatureToggle {
return readBackendRet(b, func(b *unsafeBackend) []proton.FeatureToggle {
return b.featureFlags
})
}

func (b *Backend) PushFeatureFlag(flagName string) {
writeBackend(b, func(b *unsafeBackend) {
b.featureFlags = append(b.featureFlags, proton.FeatureToggle{
Name: flagName,
Enabled: true,
})
})
}

func (b *Backend) DeleteFeatureFlags() {
writeBackend(b, func(b *unsafeBackend) {
b.featureFlags = []proton.FeatureToggle{}
})
}

func buildEvent(
updates []update,
addresses map[string]*address,
Expand Down
2 changes: 2 additions & 0 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type unsafeBackend struct {
enableDedup bool

csTicket []string

featureFlags []proton.FeatureToggle
}

func readBackendRet[T any](b *Backend, f func(b *unsafeBackend) T) T {
Expand Down
17 changes: 17 additions & 0 deletions server/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package server

import (
"net/http"

"github.com/gin-gonic/gin"
)

func (s *Server) handleGetFeatures() gin.HandlerFunc {
return func(c *gin.Context) {
ff := s.b.GetFeatureFlags()
c.JSON(http.StatusOK, gin.H{
"Code": 1000,
"Toggles": ff,
})
}
}
3 changes: 3 additions & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func initRouter(s *Server) {
s.applyRateLimit(),
)

// Feature flag route. Needs to be updated when user specific feature flags are implemented
s.r.GET("/feature/v2/frontend", s.handleGetFeatures())

if core := s.r.Group("/core/v4"); core != nil {
// Domains routes don't need authentication.
if domains := core.Group("/domains"); domains != nil {
Expand Down
8 changes: 8 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,11 @@ func (s *Server) Close() {
s.proxyTransport.CloseIdleConnections()
s.s.Close()
}

func (s *Server) PushFeatureFlag(flagName string) {
s.b.PushFeatureFlag(flagName)
}

func (s *Server) DeleteFeatureFlags() {
s.b.DeleteFeatureFlags()
}

0 comments on commit 149e501

Please sign in to comment.