update
parent
79ded3059f
commit
bf27136a4d
2
db.go
2
db.go
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
5
main.go
5
main.go
|
@ -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()
|
||||||
|
|
12
model.go
12
model.go
|
@ -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 == "" {
|
||||||
|
|
|
@ -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
|
||||||
|
|
12
setup_log.go
12
setup_log.go
|
@ -12,6 +12,8 @@ import (
|
||||||
|
|
||||||
func setupLog() {
|
func setupLog() {
|
||||||
// log.Logger = log.With().Caller().Logger()
|
// log.Logger = log.With().Caller().Logger()
|
||||||
|
if flagDev {
|
||||||
|
zerolog.SetGlobalLevel(-1)
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{
|
log.Logger = log.Output(zerolog.ConsoleWriter{
|
||||||
Out: os.Stdout,
|
Out: os.Stdout,
|
||||||
TimeFormat: "2006/01/02 15:04:05",
|
TimeFormat: "2006/01/02 15:04:05",
|
||||||
|
@ -45,7 +47,15 @@ func setupLog() {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue