-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
523 additions
and
15 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
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,34 @@ | ||
package apollo | ||
|
||
import ( | ||
"github.com/libgox/addr" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
c, err := NewClient(&Config{ | ||
AppID: "SampleApp", | ||
Cluster: "default", | ||
NamespaceNames: []string{"application", "application2"}, | ||
Address: addr.Address{ | ||
Host: "localhost", | ||
Port: 8080, | ||
}, | ||
Secret: "", | ||
TLSConfig: nil, | ||
Logger: nil, | ||
}) | ||
if err == nil { | ||
value := c.GetStringValue("application", "timeout") | ||
value2 := c.GetStringValue("application2", "timeout") | ||
c.SubscribeEvent(&ClientTest{}) | ||
t.Log(value, ",", value2) | ||
} | ||
time.Sleep(100 * time.Second) | ||
} | ||
|
||
type ClientTest struct{} | ||
|
||
func (c *ClientTest) OnChange(event *ChangeEvent) { | ||
} |
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,46 @@ | ||
package apollo | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"github.com/libgox/addr" | ||
"net/url" | ||
) | ||
|
||
type Config struct { | ||
AppID string | ||
Cluster string | ||
NamespaceNames []string | ||
Address addr.Address | ||
Secret string | ||
// TlsConfig configuration information for tls. | ||
TLSConfig *tls.Config | ||
Logger Logger | ||
} | ||
|
||
func (c *Config) GetNotifyURLSuffix(notifications string) string { | ||
return fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s¬ifications=%s", | ||
c.GetUrlPrefix(), | ||
url.QueryEscape(c.AppID), | ||
url.QueryEscape(c.Cluster), | ||
url.QueryEscape(notifications)) | ||
} | ||
|
||
func (c *Config) GetSyncURI(namespace string) string { | ||
return fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=&ip=%s", | ||
c.GetUrlPrefix(), | ||
url.QueryEscape(c.AppID), | ||
url.QueryEscape(c.Cluster), | ||
url.QueryEscape(namespace), | ||
GetLocalIP()) | ||
} | ||
|
||
func (c *Config) GetUrlPrefix() string { | ||
var urlPrefix string | ||
if c.TLSConfig != nil { | ||
urlPrefix = "https://" + c.Address.Addr() | ||
} else { | ||
urlPrefix = "http://" + c.Address.Addr() | ||
} | ||
return urlPrefix | ||
} |
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,53 @@ | ||
package apollo | ||
|
||
type ChangeType int | ||
|
||
const ( | ||
ADD ChangeType = iota | ||
|
||
MODIFY | ||
|
||
DELETE | ||
) | ||
|
||
type Listener interface { | ||
OnChange(event *ChangeEvent) | ||
} | ||
|
||
type Change struct { | ||
Key string | ||
OldValue string | ||
NewValue string | ||
ChangeType ChangeType | ||
} | ||
|
||
type ChangeEvent struct { | ||
Namespace string | ||
NotificationID int | ||
Changes map[string]*Change | ||
} | ||
|
||
func onDelete(key, value string) *Change { | ||
return &Change{ | ||
Key: key, | ||
ChangeType: DELETE, | ||
OldValue: value, | ||
} | ||
} | ||
|
||
func onModify(key, oldValue, newValue string) *Change { | ||
return &Change{ | ||
Key: key, | ||
ChangeType: MODIFY, | ||
OldValue: oldValue, | ||
NewValue: newValue, | ||
} | ||
} | ||
|
||
func onAdd(key, value string) *Change { | ||
return &Change{ | ||
Key: key, | ||
ChangeType: ADD, | ||
NewValue: value, | ||
} | ||
} |
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,56 @@ | ||
package apollo | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/exp/slog" | ||
) | ||
|
||
type Logger interface { | ||
Info(format string, args ...interface{}) | ||
|
||
Error(format string, args ...interface{}) | ||
|
||
Warn(format string, args ...interface{}) | ||
|
||
Infof(format string, args ...interface{}) | ||
|
||
Errorf(format string, args ...interface{}) | ||
|
||
Warnf(format string, args ...interface{}) | ||
} | ||
|
||
type defaultLogger struct { | ||
Logger *slog.Logger | ||
} | ||
|
||
var log Logger = &defaultLogger{ | ||
Logger: slog.Default(), | ||
} | ||
|
||
func SetLogger(logger Logger) { | ||
log = logger | ||
} | ||
|
||
func (d *defaultLogger) Info(format string, args ...interface{}) { | ||
d.Logger.Info(format, args...) | ||
} | ||
|
||
func (d *defaultLogger) Error(format string, args ...interface{}) { | ||
d.Logger.Error(format, args...) | ||
} | ||
|
||
func (d *defaultLogger) Warn(format string, args ...interface{}) { | ||
d.Logger.Warn(format, args...) | ||
} | ||
|
||
func (d *defaultLogger) Infof(format string, args ...interface{}) { | ||
d.Logger.Info(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (d *defaultLogger) Errorf(format string, args ...interface{}) { | ||
d.Logger.Error(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (d *defaultLogger) Warnf(format string, args ...interface{}) { | ||
d.Logger.Warn(fmt.Sprintf(format, args...)) | ||
} |
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,49 @@ | ||
package apollo | ||
|
||
import ( | ||
"encoding/json" | ||
"sync" | ||
) | ||
|
||
const defaultNotificationID int = -1 | ||
|
||
type notificationsMgr struct { | ||
notifications sync.Map | ||
} | ||
|
||
type notification struct { | ||
NamespaceName string `json:"namespaceName"` | ||
NotificationID int `json:"notificationId"` | ||
} | ||
|
||
func newNotificationManager(namespaceNames []string) *notificationsMgr { | ||
n := ¬ificationsMgr{ | ||
notifications: sync.Map{}, | ||
} | ||
for _, namespaceName := range namespaceNames { | ||
n.notifications.Store(namespaceName, defaultNotificationID) | ||
} | ||
return n | ||
} | ||
|
||
func (n *notificationsMgr) String() string { | ||
var notifications []*notification | ||
n.notifications.Range(func(key, value interface{}) bool { | ||
k, _ := key.(string) | ||
v, _ := value.(int) | ||
notifications = append(notifications, ¬ification{ | ||
NamespaceName: k, | ||
NotificationID: v, | ||
}) | ||
return true | ||
}) | ||
res, err := json.Marshal(¬ifications) | ||
if err != nil { | ||
return "" | ||
} | ||
return string(res) | ||
} | ||
|
||
func (n *notificationsMgr) Store(namespaceName string, notificationID int) { | ||
n.notifications.Store(namespaceName, notificationID) | ||
} |
Oops, something went wrong.