Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/BRIDGE-119 - Feature flag support #151

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading