-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BUX-641): featch SharedConfig & expose PublicConfig
- Loading branch information
1 parent
8375025
commit 0ca2580
Showing
7 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package config | ||
|
||
import ( | ||
"slices" | ||
"sync" | ||
|
||
backendconfig "github.com/bitcoin-sv/spv-wallet-web-backend/config" | ||
"github.com/bitcoin-sv/spv-wallet-web-backend/domain/users" | ||
"github.com/bitcoin-sv/spv-wallet/models" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type ConfigService struct { | ||
adminWalletClient users.AdminWalletClient | ||
log *zerolog.Logger | ||
|
||
sharedConfig *models.SharedConfig | ||
publicConfig *PublicConfig | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewConfigService(adminWalletClient users.AdminWalletClient, log *zerolog.Logger) *ConfigService { | ||
return &ConfigService{ | ||
adminWalletClient: adminWalletClient, | ||
log: log, | ||
sharedConfig: nil, | ||
publicConfig: nil, | ||
} | ||
} | ||
|
||
// GetSharedConfig returns shared config. | ||
// If shared config is not cached, it will be fetched from the spv-wallet. | ||
// SharedConfig should not be exposed to the public - use PublicConfig instead. | ||
func (s *ConfigService) GetSharedConfig() *models.SharedConfig { | ||
if s.sharedConfig != nil { | ||
return s.sharedConfig | ||
} | ||
|
||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
model, err := s.adminWalletClient.GetSharedConfig() | ||
if err != nil { | ||
s.log.Error().Err(err).Msg("Failed to get shared config") | ||
return nil | ||
} | ||
s.sharedConfig = model | ||
return s.sharedConfig | ||
} | ||
|
||
// GetPublicConfig returns public config. | ||
func (s *ConfigService) GetPublicConfig() *PublicConfig { | ||
if s.publicConfig != nil { | ||
return s.publicConfig | ||
} | ||
shared := s.GetSharedConfig() | ||
if shared == nil { | ||
return nil | ||
} | ||
|
||
s.publicConfig = s.makePublicConfig(shared) | ||
return s.publicConfig | ||
} | ||
|
||
func (s *ConfigService) makePublicConfig(shared *models.SharedConfig) *PublicConfig { | ||
configuredPaymailDomain := viper.GetString(backendconfig.EnvPaymailDomain) | ||
if !slices.Contains(shared.PaymilDomains, configuredPaymailDomain) { | ||
s.log.Warn().Str("configuredPaymailDomain", configuredPaymailDomain).Msg("Configured paymail domain is not in the list of paymail domains from SPV Wallet") | ||
} | ||
|
||
return &PublicConfig{ | ||
PaymilDomain: configuredPaymailDomain, | ||
ExperimentalFeatures: shared.ExperimentalFeatures, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package config | ||
|
||
import "github.com/bitcoin-sv/spv-wallet/models" | ||
|
||
type PublicConfig struct { | ||
PaymilDomain string `json:"paymail_domain"` | ||
ExperimentalFeatures models.ExperimentalConfig `json:"experimental_features"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/bitcoin-sv/spv-wallet-web-backend/domain" | ||
router "github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/endpoints/routes" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type handler struct { | ||
services *domain.Services | ||
} | ||
|
||
func NewHandler(s *domain.Services) router.RootEndpoints { | ||
handler := &handler{ | ||
services: s, | ||
} | ||
prefix := "/api/v1" | ||
|
||
rootEndpoints := router.RootEndpointsFunc(func(router *gin.RouterGroup) { | ||
router.GET(prefix+"/config", handler.getPublicConfig) | ||
}) | ||
|
||
return rootEndpoints | ||
} | ||
|
||
// getConfig returns config fields exposed to clients. | ||
// | ||
// @Summary Get config returns config fields exposed to clients | ||
// @Tags sharedconfig | ||
// @Produce json | ||
// @Success 200 {object} PublicConfig | ||
// @Router /api/v1/config [get] | ||
func (h *handler) getPublicConfig(c *gin.Context) { | ||
pubConf := h.services.ConfigService.GetPublicConfig() | ||
if pubConf == nil { | ||
c.JSON(500, "Failed to get public config") | ||
return | ||
} | ||
c.JSON(200, pubConf) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters