diff --git a/cmd/lrc/nodecmd.go b/cmd/lrc/nodecmd.go index 0c8cc0ba..48fcfa0b 100644 --- a/cmd/lrc/nodecmd.go +++ b/cmd/lrc/nodecmd.go @@ -47,6 +47,8 @@ func startNode(ctx *cli.Context) error { } }() + log.InitSnsClient(globalConfig.AWSService) + var n *node.Node signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, os.Interrupt) diff --git a/config/config.go b/config/config.go index 20836d23..a9dfa401 100644 --- a/config/config.go +++ b/config/config.go @@ -87,6 +87,7 @@ type GlobalConfig struct { Market MarketOptions MarketCap MarketCapOptions UserManager UserManagerOptions + AWSService AwsServiceOption } type JsonrpcOptions struct { @@ -254,6 +255,10 @@ type UserManagerOptions struct { WhiteListCacheCleanTime int64 } +type AwsServiceOption struct { + SNSTopicArn string +} + func Validator(cv reflect.Value) (bool, error) { for i := 0; i < cv.NumField(); i++ { cvt := cv.Type().Field(i) diff --git a/config/relay.toml b/config/relay.toml index 9eced036..109c1814 100644 --- a/config/relay.toml +++ b/config/relay.toml @@ -134,3 +134,7 @@ name = "Loopring corporation" white_list_open = false white_list_cache_expire_time = 8640000 white_list_cache_clean_time = 0 + + +[aws_service] + sns_topic_arn = "arn:aws:sns:ap-northeast-1:639504797543:RelayNotification" diff --git a/log/log.go b/log/log.go index a748e28e..230b67cd 100644 --- a/log/log.go +++ b/log/log.go @@ -101,3 +101,7 @@ func Panicw(msg string, keysAndValues ...interface{}) { func Fatalw(msg string, keysAndValues ...interface{}) { sugaredLogger.Fatalw(msg, keysAndValues...) } + +func PublishSns(subject string, message string) { + snsClient.PublishSns(subject, message) +} diff --git a/log/logger.go b/log/logger.go index ade9b3d4..1f6a6032 100644 --- a/log/logger.go +++ b/log/logger.go @@ -26,6 +26,7 @@ import ( //todo: I'm not sure whether zap support Rotating var logger *zap.Logger var sugaredLogger *zap.SugaredLogger +var snsClient *SnsClient func Initialize(logOpts config.LogOptions) *zap.Logger { var err error @@ -46,3 +47,7 @@ func Initialize(logOpts config.LogOptions) *zap.Logger { return logger } + +func InitSnsClient(awsOption config.AwsServiceOption) { + snsClient = NewSnsClient(awsOption) +} diff --git a/log/sns_client.go b/log/sns_client.go new file mode 100644 index 00000000..98dc4d0f --- /dev/null +++ b/log/sns_client.go @@ -0,0 +1,55 @@ +package log + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/sns" + "github.com/Loopring/relay/config" +) +import ( + "fmt" + "time" +) + +type SnsClient struct { + innerClient *sns.SNS + topicArn string + valid bool +} + +const region = "ap-northeast-1" + +func NewSnsClient(options config.AwsServiceOption) *SnsClient { + if len(options.SNSTopicArn) == 0 { + Errorf("Sns TopicArn not set, will not init sns client") + return &SnsClient{nil, nil, false} + } + //NOTE: use default config ~/.asw/credentials + sess, err := session.NewSession(&aws.Config{ + Region: aws.String(region), + Credentials: credentials.NewSharedCredentials("", ""), + }) + if err != nil { + Errorf("new aws session failed \n", err.Error()) + return &SnsClient{nil, options.SNSTopicArn, false} + } else { + return &SnsClient{sns.New(sess), options.SNSTopicArn,true} + } +} + +func (client *SnsClient) PublishSns(subject string, message string) { + if !client.valid { + Error("SnsClient invalid, will not send message") + return + } else { + input := &sns.PublishInput{} + input.SetTopicArn(client.topicArn) + input.SetSubject(subject) + input.SetMessage(fmt.Sprintf("%s|%s",time.Now().Format("15:04:05"), message)) + _, err := client.innerClient.Publish(input) + if err != nil { + Errorf("Failed send sns message with error : %s\nSubject: %s\n, Message %s\n", err.Error(), subject, message) + } + } +} \ No newline at end of file