Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
Open
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
2 changes: 2 additions & 0 deletions cmd/lrc/nodecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type GlobalConfig struct {
Market MarketOptions
MarketCap MarketCapOptions
UserManager UserManagerOptions
AWSService AwsServiceOption
}

type JsonrpcOptions struct {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions config/relay.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 4 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,3 +47,7 @@ func Initialize(logOpts config.LogOptions) *zap.Logger {

return logger
}

func InitSnsClient(awsOption config.AwsServiceOption) {
snsClient = NewSnsClient(awsOption)
}
55 changes: 55 additions & 0 deletions log/sns_client.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}