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 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 (
|
||||
"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()
|
||||
|
|
12
model.go
12
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 == "" {
|
||||
|
|
|
@ -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
|
||||
|
|
70
setup_log.go
70
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue