feat(internal/timermanger): timermanger
This commit is contained in:
@@ -26,3 +26,4 @@ config.json
|
|||||||
cookies.txt
|
cookies.txt
|
||||||
tmp/qrcode.png
|
tmp/qrcode.png
|
||||||
tmp/cookies.txt
|
tmp/cookies.txt
|
||||||
|
bye.json
|
||||||
@@ -22,6 +22,7 @@
|
|||||||
"basic": {
|
"basic": {
|
||||||
"name": "xx", // 你的名字
|
"name": "xx", // 你的名字
|
||||||
"qq_number": "123", // QQ号
|
"qq_number": "123", // QQ号
|
||||||
|
"nickname": "", // 网名
|
||||||
"age": 16, // 你的年龄
|
"age": 16, // 你的年龄
|
||||||
"cause_stop": "自杀" // 你最可能的死因
|
"cause_stop": "自杀" // 你最可能的死因
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"basic": {
|
"basic": {
|
||||||
"name": "xx",
|
"name": "xx",
|
||||||
"qq_number": "123",
|
"qq_number": "123",
|
||||||
|
"nickname": "",
|
||||||
"age": 16,
|
"age": 16,
|
||||||
"cause_stop": "自杀"
|
"cause_stop": "自杀"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ type GithubConfig struct {
|
|||||||
type Basic struct {
|
type Basic struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Age int `json:"age"`
|
Age int `json:"age"`
|
||||||
|
NickName string `json:"nickname"`
|
||||||
QQNumber string `json:"qq_number"`
|
QQNumber string `json:"qq_number"`
|
||||||
CauseStop string `json:"cause_stop"`
|
CauseStop string `json:"cause_stop"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ func GetBasicInfo() string {
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
lastSignalTime := now.Add(-time.Duration(config.DisconnectDuration) * time.Hour).Format("2006-01-02 15:04:05")
|
lastSignalTime := now.Add(-time.Duration(config.DisconnectDuration) * time.Hour).Format("2006-01-02 15:04:05")
|
||||||
|
|
||||||
return fmt.Sprintf("%s(QQ号:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
if config.Debug {
|
||||||
config.Basic.Name, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
return fmt.Sprintf("此消息为测试消息\n\n%s(QQ号:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
||||||
|
config.Basic.Name, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("%s(QQ号:%s,网名:%s)已于%s至%s(UTC+8)之间去世,享年%d岁,死因可能为%s(此死因为推测最可能的死因,请勿完全相信,请等待后续消息)",
|
||||||
|
config.Basic.Name, config.Basic.NickName, config.Basic.QQNumber, lastSignalTime, time.Now().Format("2006-01-02 15:04:05"), config.Basic.Age, config.Basic.CauseStop)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,22 @@ import (
|
|||||||
"github.com/ssdomei232/goodBaby/configs"
|
"github.com/ssdomei232/goodBaby/configs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func EnsureTmpDirectory() error {
|
||||||
|
_, err := os.Stat("tmp")
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir("tmp", 0755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Println("已创建tmp目录")
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
log.Println("tmp目录已存在")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func isCookiesExpired(cookiesString string) bool {
|
func isCookiesExpired(cookiesString string) bool {
|
||||||
// 解析cookie中的过期时间
|
// 解析cookie中的过期时间
|
||||||
lines := strings.SplitSeq(cookiesString, "\n")
|
lines := strings.SplitSeq(cookiesString, "\n")
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
// internal/timer_manager.go
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TimerManager struct {
|
||||||
|
duration time.Duration
|
||||||
|
resetTime time.Time
|
||||||
|
mutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
var GlobalTimerManager *TimerManager
|
||||||
|
|
||||||
|
func InitTimerManager(duration time.Duration) {
|
||||||
|
GlobalTimerManager = &TimerManager{
|
||||||
|
duration: duration,
|
||||||
|
resetTime: time.Now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *TimerManager) Reset() {
|
||||||
|
tm.mutex.Lock()
|
||||||
|
defer tm.mutex.Unlock()
|
||||||
|
tm.resetTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *TimerManager) GetRemainingTime() time.Duration {
|
||||||
|
tm.mutex.RLock()
|
||||||
|
defer tm.mutex.RUnlock()
|
||||||
|
|
||||||
|
elapsed := time.Since(tm.resetTime)
|
||||||
|
remaining := tm.duration - elapsed
|
||||||
|
if remaining < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return remaining
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleTimerStatus(c *gin.Context) {
|
||||||
|
remainingTime := GlobalTimerManager.GetRemainingTime()
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"code": 200,
|
||||||
|
"remaining_time": remainingTime.Seconds(), // 返回剩余秒数
|
||||||
|
"message": "ok",
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/CuteReimu/bilibili/v2"
|
"github.com/CuteReimu/bilibili/v2"
|
||||||
@@ -29,7 +28,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查并创建tmp目录
|
// 检查并创建tmp目录
|
||||||
if err := ensureTmpDirectory(); err != nil {
|
if err := internal.EnsureTmpDirectory(); err != nil {
|
||||||
log.Printf("创建tmp目录失败: %v", err)
|
log.Printf("创建tmp目录失败: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,6 +37,7 @@ func init() {
|
|||||||
initBilibili()
|
initBilibili()
|
||||||
startCookieChecker() // 启动定期检查cookie有效性
|
startCookieChecker() // 启动定期检查cookie有效性
|
||||||
|
|
||||||
|
internal.InitTimerManager(duration)
|
||||||
timer = time.NewTimer(duration)
|
timer = time.NewTimer(duration)
|
||||||
go func() {
|
go func() {
|
||||||
<-timer.C
|
<-timer.C
|
||||||
@@ -45,55 +45,21 @@ func init() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensureTmpDirectory 检查tmp目录是否存在,如果不存在则创建
|
|
||||||
func ensureTmpDirectory() error {
|
|
||||||
_, err := os.Stat("tmp")
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
err = os.Mkdir("tmp", 0755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Println("已创建tmp目录")
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
log.Println("tmp目录已存在")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func trigger(config configs.Config) {
|
func trigger(config configs.Config) {
|
||||||
if config.EnableQQ {
|
if config.EnableQQ {
|
||||||
go internal.SendQQ()
|
go internal.SendQQ()
|
||||||
}
|
}
|
||||||
go internal.SendMail()
|
go internal.SendMail()
|
||||||
go internal.Github()
|
go internal.Github()
|
||||||
go internal.SendBili(biliClient)
|
if !config.Debug {
|
||||||
|
go internal.SendBili(biliClient)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config, err := configs.GetConfig()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("获取配置文件失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/signal", func(c *gin.Context) {
|
r.GET("/signal", handleSignal)
|
||||||
secret := c.Query("secret")
|
r.GET("/timer/status", internal.HandleTimerStatus)
|
||||||
if secret != config.SignalSecret {
|
|
||||||
c.JSON(403, gin.H{
|
|
||||||
"code": 403,
|
|
||||||
"message": "secret error",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
timer.Reset(duration)
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"code": 200,
|
|
||||||
"message": "ok",
|
|
||||||
})
|
|
||||||
log.Println("触发信号")
|
|
||||||
})
|
|
||||||
r.Run(":8088")
|
r.Run(":8088")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,3 +84,26 @@ func startCookieChecker() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleSignal(c *gin.Context) {
|
||||||
|
config, err := configs.GetConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("获取配置文件失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
secret := c.Query("secret")
|
||||||
|
if secret != config.SignalSecret {
|
||||||
|
c.JSON(403, gin.H{
|
||||||
|
"code": 403,
|
||||||
|
"message": "secret error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
timer.Reset(duration)
|
||||||
|
internal.GlobalTimerManager.Reset()
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"code": 200,
|
||||||
|
"message": "ok",
|
||||||
|
})
|
||||||
|
log.Println("触发信号")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user