master
Evan Chen 2021-12-30 21:39:00 +08:00
parent 79ded3059f
commit bf27136a4d
6 changed files with 121 additions and 32 deletions

2
db.go
View File

@ -41,7 +41,7 @@ func migrateDB() {
if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil { if err := DB.SetupJoinTable(&User{}, "Products", &Order{}); err != nil {
panic(err) 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) panic(err)
} }
} }

36
deploy.sh Normal file
View File

@ -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

View File

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -39,6 +40,10 @@ func init() {
} }
func main() { func main() {
tz, err := time.LoadLocation("Asia/Taipei")
if err == nil {
time.Local = tz
}
flag.Parse() flag.Parse()
setupLog() setupLog()
setupServer() setupServer()

View File

@ -14,7 +14,7 @@ type User struct {
Email string Email string
LastLogin time.Time LastLogin time.Time
Products Product `gorm:"many2many:orders;"` Products []Product `gorm:"many2many:orders;"`
ProfileID uint `gorm:"default:NULL"` ProfileID uint `gorm:"default:NULL"`
Profile Profile Profile Profile
@ -54,6 +54,16 @@ type Order struct {
UpdatedAt time.Time UpdatedAt time.Time
} }
type Node struct {
ID uint `gorm:"primaryKey"`
Name string
UserID string
CreatedAt time.Time
UpdatedAt time.Time
}
// BeforeCreate set UID // BeforeCreate set UID
func (u *User) BeforeCreate(tx *gorm.DB) (err error) { func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
if u.ID == "" { if u.ID == "" {

View File

@ -45,16 +45,44 @@ func Follow(event *linebot.Event) {
usr := &User{ usr := &User{
ID: uid, ID: uid,
} }
Hit("加入", uid)
reply := event.ReplyToken
row := DB.Exec("select id from users where id = ?", uid).RowsAffected row := DB.Exec("select id from users where id = ?", uid).RowsAffected
if row == 0 { if row == 0 {
// 新用戶 // 新用戶
if err := DB.Create(usr).Error; err != nil { if err := DB.Create(usr).Error; err != nil {
log.Error().Err(err).Msg("Follow error.") 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 { } else {
// 舊用戶 // 舊用戶
log.Debug().Str("user", uid).Msg("user unblocked.") 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 // Welcome and ask for

View File

@ -12,40 +12,50 @@ import (
func setupLog() { func setupLog() {
// log.Logger = log.With().Caller().Logger() // log.Logger = log.With().Caller().Logger()
log.Logger = log.Output(zerolog.ConsoleWriter{ if flagDev {
Out: os.Stdout, zerolog.SetGlobalLevel(-1)
TimeFormat: "2006/01/02 15:04:05", log.Logger = log.Output(zerolog.ConsoleWriter{
FormatCaller: func(i interface{}) string { Out: os.Stdout,
var c string TimeFormat: "2006/01/02 15:04:05",
if cc, ok := i.(string); ok { FormatCaller: func(i interface{}) string {
c = cc var c string
} if cc, ok := i.(string); ok {
if len(c) > 0 { c = cc
// shorten caller to mod/file:line }
segs := strings.Split(c, ":") if len(c) > 0 {
file := segs[len(segs)-2] // shorten caller to mod/file:line
short := file segs := strings.Split(c, ":")
ptr := 0 file := segs[len(segs)-2]
for i := len(file) - 1; i > 0; i-- { short := file
if file[i] == '/' { ptr := 0
if ptr == 0 { for i := len(file) - 1; i > 0; i-- {
short = file[i+1:] 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 ptr = i
} else {
short = fmt.Sprintf("%v/%v", file[i+1:ptr], short)
break
} }
} }
if file[i] == '@' { return color.GreenString("%v:%v >", short, segs[len(segs)-1])
ptr = i
}
} }
return color.GreenString("%v:%v >", short, segs[len(segs)-1]) return c
} },
return c })
}, }
})
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.DurationFieldInteger = true zerolog.DurationFieldInteger = true
zerolog.SetGlobalLevel(-1) }
func Hit(name, uid string) {
n := &Node{
Name: name,
UserID: uid,
}
DB.Create(n)
} }