|
|
|
@ -10,6 +10,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
"jochum.dev/jo-micro/auth2"
|
|
|
|
|
"jochum.dev/jo-micro/components"
|
|
|
|
|
"jochum.dev/jo-micro/settings/cmd/microsettingsd/config"
|
|
|
|
|
"jochum.dev/jo-micro/settings/proto/settingspb"
|
|
|
|
@ -50,15 +51,24 @@ func MustReg(cReg *components.Registry) *Handler {
|
|
|
|
|
return cReg.Must(Name).(*Handler)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) sClient() (settingspb.SettingsService, error) {
|
|
|
|
|
func (h *Handler) client(ctx context.Context) (settingspb.SettingsService, context.Context, error) {
|
|
|
|
|
// Wait until the service is here
|
|
|
|
|
_, err := utils.ServiceRetryGet(h.cReg.Service(), config.Name, 10)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
return nil, ctx, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
service := settingspb.NewSettingsService(config.Name, h.cReg.Service().Client())
|
|
|
|
|
return service, nil
|
|
|
|
|
|
|
|
|
|
// Optional Service Account
|
|
|
|
|
if err := auth2.RegHasClientAuth(h.cReg); err == nil {
|
|
|
|
|
ctx, err = auth2.ClientAuthMustReg(h.cReg).Plugin().ServiceContext(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, ctx, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return service, ctx, nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -97,7 +107,7 @@ func (c *Handler) Initialized() bool {
|
|
|
|
|
|
|
|
|
|
func (h *Handler) Init(cReg *components.Registry, cli *cli.Context) error {
|
|
|
|
|
if h.initialized {
|
|
|
|
|
return nil
|
|
|
|
|
return errors.InternalServerError("ALREADY_INITIALIZED", "already initialized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.cReg = cReg
|
|
|
|
@ -111,15 +121,15 @@ func (h *Handler) Stop() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) Health(context context.Context) error {
|
|
|
|
|
if !c.Initialized() {
|
|
|
|
|
return errors.InternalServerError("NOT_INITIALIZED", "Not initialized")
|
|
|
|
|
func (h *Handler) Health(context context.Context) error {
|
|
|
|
|
if !h.Initialized() {
|
|
|
|
|
return errors.InternalServerError("NOT_INITIALIZED", "not initialized")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*Setting, error) {
|
|
|
|
|
func (h *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*Setting, error) {
|
|
|
|
|
// Build the request
|
|
|
|
|
req := &settingspb.GetRequest{}
|
|
|
|
|
cacheKey := ""
|
|
|
|
@ -147,14 +157,14 @@ func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check cache and return from cache
|
|
|
|
|
c.cacheGetLock.RLock()
|
|
|
|
|
if result, ok := c.cacheGet[cacheKey]; ok {
|
|
|
|
|
c.cacheGetLock.RUnlock()
|
|
|
|
|
h.cacheGetLock.RLock()
|
|
|
|
|
if result, ok := h.cacheGet[cacheKey]; ok {
|
|
|
|
|
h.cacheGetLock.RUnlock()
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
c.cacheGetLock.RUnlock()
|
|
|
|
|
h.cacheGetLock.RUnlock()
|
|
|
|
|
|
|
|
|
|
client, err := c.sClient()
|
|
|
|
|
client, ctx, err := h.client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -170,14 +180,14 @@ func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the result in cache
|
|
|
|
|
c.cacheGetLock.Lock()
|
|
|
|
|
c.cacheGet[cacheKey] = cResult
|
|
|
|
|
c.cacheGetLock.Unlock()
|
|
|
|
|
h.cacheGetLock.Lock()
|
|
|
|
|
h.cacheGet[cacheKey] = cResult
|
|
|
|
|
h.cacheGetLock.Unlock()
|
|
|
|
|
|
|
|
|
|
return cResult, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) ([]*Setting, error) {
|
|
|
|
|
func (h *Handler) List(ctx context.Context, id, ownerId, service, name string) ([]*Setting, error) {
|
|
|
|
|
// Build the request
|
|
|
|
|
req := &settingspb.ListRequest{}
|
|
|
|
|
cacheKey := ""
|
|
|
|
@ -205,15 +215,15 @@ func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check cache and return from cache
|
|
|
|
|
c.cacheListLock.RLock()
|
|
|
|
|
if result, ok := c.cacheList[cacheKey]; ok {
|
|
|
|
|
c.cacheListLock.RUnlock()
|
|
|
|
|
h.cacheListLock.RLock()
|
|
|
|
|
if result, ok := h.cacheList[cacheKey]; ok {
|
|
|
|
|
h.cacheListLock.RUnlock()
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
c.cacheListLock.RUnlock()
|
|
|
|
|
h.cacheListLock.RUnlock()
|
|
|
|
|
|
|
|
|
|
// Fetch
|
|
|
|
|
client, err := c.sClient()
|
|
|
|
|
client, ctx, err := h.client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -232,16 +242,16 @@ func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) (
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the result in cache
|
|
|
|
|
c.cacheListLock.Lock()
|
|
|
|
|
c.cacheList[cacheKey] = cResult
|
|
|
|
|
c.cacheListLock.Unlock()
|
|
|
|
|
h.cacheListLock.Lock()
|
|
|
|
|
h.cacheList[cacheKey] = cResult
|
|
|
|
|
h.cacheListLock.Unlock()
|
|
|
|
|
|
|
|
|
|
return cResult, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, error) {
|
|
|
|
|
func (h *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, error) {
|
|
|
|
|
// Create
|
|
|
|
|
client, err := c.sClient()
|
|
|
|
|
client, ctx, err := h.client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -262,9 +272,9 @@ func (c *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, erro
|
|
|
|
|
return serviceToClient(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, error) {
|
|
|
|
|
func (h *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, error) {
|
|
|
|
|
// Update
|
|
|
|
|
client, err := c.sClient()
|
|
|
|
|
client, ctx, err := h.client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -281,9 +291,9 @@ func (c *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, erro
|
|
|
|
|
return serviceToClient(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Handler) Upsert(ctx context.Context, req *UpsertRequest) (*Setting, error) {
|
|
|
|
|
func (h *Handler) Upsert(ctx context.Context, req *UpsertRequest) (*Setting, error) {
|
|
|
|
|
// Upsert
|
|
|
|
|
client, err := c.sClient()
|
|
|
|
|
client, ctx, err := h.client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|