From bf27136a4dc09e31d734829cb71f72c9f41a46d4 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 30 Dec 2021 21:39:00 +0800 Subject: [PATCH] update --- db.go | 2 +- deploy.sh | 36 ++++++++++++++++++++++++++ main.go | 5 ++++ model.go | 12 ++++++++- setup_line.go | 28 +++++++++++++++++++++ setup_log.go | 70 +++++++++++++++++++++++++++++---------------------- 6 files changed, 121 insertions(+), 32 deletions(-) create mode 100644 deploy.sh diff --git a/db.go b/db.go index 8fa2acb..af63cb2 100644 --- a/db.go +++ b/db.go @@ -41,7 +41,7 @@ func migrateDB() { if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil { panic(err) } - if err := DB.AutoMigrate(&User{}, &Profile{}, &Product{}, &Order{}); err != nil { + if err := DB.AutoMigrate(&Node{}, &User{}, &Profile{}, &Product{}, &Order{}); err != nil { panic(err) } } diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 0000000..10f88a3 --- /dev/null +++ b/deploy.sh @@ -0,0 +1,36 @@ +VERSION=$(git describe --tags --abbrev=0) +if [ $? -ne 0 ]; then VERSION=$DRONE_TAG; fi +[ -z "$VERSION" ] && VERSION=v0.0.0 +BUILD=$(git rev-parse --short HEAD) +if [ $? -ne 0 ]; then BUILD=${DRONE_COMMIT:0:7}; fi + +PROJ=talent +DIST=dist + +LDFLAGS="-ldflags \"-X main.Version=${VERSION} -X main.Build=${BUILD} -w -s -extldflags '-static'\"" +GOFLAGS="-a -tags netgo" +FAILURES="" + +PLATFORMS="linux/s390x" + + +for PLATFORM in $PLATFORMS; do + GOOS=${PLATFORM%/*} + GOARCH=${PLATFORM#*/} + BIN_FILENAME="${PROJ}" + if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi + CMD="CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build ${GOFLAGS} ${LDFLAGS} -o ${DIST}/${BIN_FILENAME} *.go" + echo "${CMD}" + eval $CMD || FAILURES="${FAILURES} ${PLATFORM}" + ssh root@bot.ework.tw rm talent + scp ${DIST}/${BIN_FILENAME} root@bot.ework.tw:~ + ssh root@bot.ework.tw chmod +x talent + ssh root@bot.ework.tw screen -X quit + ssh root@bot.ework.tw screen -d -m "./talent -dev -db-name talentdev > talent.log" +done + +if [[ "${FAILURES}" != "" ]]; then + echo "" + echo "${SCRIPT_NAME} failed on: ${FAILURES}" + exit 1 +fi \ No newline at end of file diff --git a/main.go b/main.go index 034289e..8bfc27f 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "time" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" @@ -39,6 +40,10 @@ func init() { } func main() { + tz, err := time.LoadLocation("Asia/Taipei") + if err == nil { + time.Local = tz + } flag.Parse() setupLog() setupServer() diff --git a/model.go b/model.go index ebc485e..953c98c 100644 --- a/model.go +++ b/model.go @@ -14,7 +14,7 @@ type User struct { Email string LastLogin time.Time - Products Product `gorm:"many2many:orders;"` + Products []Product `gorm:"many2many:orders;"` ProfileID uint `gorm:"default:NULL"` Profile Profile @@ -54,6 +54,16 @@ type Order struct { UpdatedAt time.Time } +type Node struct { + ID uint `gorm:"primaryKey"` + + Name string + UserID string + + CreatedAt time.Time + UpdatedAt time.Time +} + // BeforeCreate set UID func (u *User) BeforeCreate(tx *gorm.DB) (err error) { if u.ID == "" { diff --git a/setup_line.go b/setup_line.go index 8f78801..150016b 100644 --- a/setup_line.go +++ b/setup_line.go @@ -45,16 +45,44 @@ func Follow(event *linebot.Event) { usr := &User{ ID: uid, } + Hit("加入", uid) + + reply := event.ReplyToken row := DB.Exec("select id from users where id = ?", uid).RowsAffected if row == 0 { // 新用戶 if err := DB.Create(usr).Error; err != nil { log.Error().Err(err).Msg("Follow error.") + return } + + // 舊用戶 + log.Debug().Str("user", uid).Msg("New user.") + msg := linebot.NewTextMessage("你好!") + if reply != "" { + if _, err := bot.ReplyMessage(reply, msg).Do(); err != nil { + log.Error().Err(err).Caller().Msg("reply error") + } + } else { + if _, err := bot.PushMessage(uid, msg).Do(); err != nil { + log.Error().Err(err).Caller().Msg("push error") + } + } + } else { // 舊用戶 log.Debug().Str("user", uid).Msg("user unblocked.") + msg := linebot.NewTextMessage("歡迎回來!") + if reply != "" { + if _, err := bot.ReplyMessage(reply, msg).Do(); err != nil { + log.Error().Err(err).Caller().Msg("reply error") + } + } else { + if _, err := bot.PushMessage(uid, msg).Do(); err != nil { + log.Error().Err(err).Caller().Msg("push error") + } + } } // Welcome and ask for diff --git a/setup_log.go b/setup_log.go index b5f2751..7f54127 100644 --- a/setup_log.go +++ b/setup_log.go @@ -12,40 +12,50 @@ import ( func setupLog() { // log.Logger = log.With().Caller().Logger() - log.Logger = log.Output(zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: "2006/01/02 15:04:05", - FormatCaller: func(i interface{}) string { - var c string - if cc, ok := i.(string); ok { - c = cc - } - if len(c) > 0 { - // shorten caller to mod/file:line - segs := strings.Split(c, ":") - file := segs[len(segs)-2] - short := file - ptr := 0 - for i := len(file) - 1; i > 0; i-- { - if file[i] == '/' { - if ptr == 0 { - short = file[i+1:] + if flagDev { + zerolog.SetGlobalLevel(-1) + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: "2006/01/02 15:04:05", + FormatCaller: func(i interface{}) string { + var c string + if cc, ok := i.(string); ok { + c = cc + } + if len(c) > 0 { + // shorten caller to mod/file:line + segs := strings.Split(c, ":") + file := segs[len(segs)-2] + short := file + ptr := 0 + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + if ptr == 0 { + short = file[i+1:] + ptr = i + } else { + short = fmt.Sprintf("%v/%v", file[i+1:ptr], short) + break + } + } + if file[i] == '@' { ptr = i - } else { - short = fmt.Sprintf("%v/%v", file[i+1:ptr], short) - break } } - if file[i] == '@' { - ptr = i - } + return color.GreenString("%v:%v >", short, segs[len(segs)-1]) } - return color.GreenString("%v:%v >", short, segs[len(segs)-1]) - } - return c - }, - }) + return c + }, + }) + } zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.DurationFieldInteger = true - zerolog.SetGlobalLevel(-1) +} + +func Hit(name, uid string) { + n := &Node{ + Name: name, + UserID: uid, + } + DB.Create(n) }