feat(internal/reminder): Signal提醒
This commit is contained in:
@@ -3,7 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
|
||||
"github.com/CuteReimu/bilibili/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/ssdomei232/goodBaby/configs"
|
||||
"github.com/ssdomei232/goodBaby/internal"
|
||||
)
|
||||
@@ -46,8 +46,8 @@ func init() {
|
||||
|
||||
// Init Bilibili Clinet
|
||||
biliClient = bilibili.New()
|
||||
initBilibili()
|
||||
startCookieChecker() // Check Bilibili cookie
|
||||
internal.InitBilibili(biliClient)
|
||||
internal.StartCookieChecker(cookieCheckTimer, biliClient) // Check Bilibili cookie
|
||||
|
||||
internal.InitTimerManager(duration)
|
||||
timer = time.NewTimer(duration)
|
||||
@@ -57,6 +57,7 @@ func init() {
|
||||
}()
|
||||
}
|
||||
|
||||
// trigger
|
||||
func trigger(config configs.Config) {
|
||||
if config.EnableQQ {
|
||||
go internal.SendQQ()
|
||||
@@ -69,44 +70,26 @@ func trigger(config configs.Config) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// cron job
|
||||
internal.Reminder()
|
||||
c := cron.New()
|
||||
c.AddFunc("@every 1h", internal.Reminder)
|
||||
c.Start()
|
||||
|
||||
// Web service
|
||||
r := gin.Default()
|
||||
templFS, _ := fs.Sub(templateFiles, "templates")
|
||||
r.SetHTMLTemplate(loadTemplates(templFS))
|
||||
r.SetHTMLTemplate(internal.LoadTemplates(templFS))
|
||||
staticFS, _ := fs.Sub(staticFiles, "static")
|
||||
r.StaticFS("/static", http.FS(staticFS))
|
||||
|
||||
r.GET("/", indexPage)
|
||||
r.GET("/", internal.IndexPage)
|
||||
r.GET("/signal", handleSignal)
|
||||
r.GET("/timer/status", internal.HandleTimerStatus)
|
||||
r.Run(":8088")
|
||||
}
|
||||
|
||||
func indexPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html", nil)
|
||||
}
|
||||
|
||||
func initBilibili() {
|
||||
// try cached cookie
|
||||
if !internal.LoadCookies(biliClient) {
|
||||
// if cookie check failed, request qrcode login
|
||||
internal.LoginWithQRCode(biliClient)
|
||||
} else {
|
||||
log.Println("使用已存储的有效cookie登录")
|
||||
}
|
||||
}
|
||||
|
||||
// Enable periodic cookie checks
|
||||
func startCookieChecker() {
|
||||
// check cookie per hour
|
||||
cookieCheckTimer = time.NewTicker(1 * time.Hour)
|
||||
go func() {
|
||||
for {
|
||||
<-cookieCheckTimer.C
|
||||
internal.CheckCookieValidity(biliClient)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Handle Signal
|
||||
func handleSignal(c *gin.Context) {
|
||||
config, err := configs.GetConfig()
|
||||
if err != nil {
|
||||
@@ -129,9 +112,3 @@ func handleSignal(c *gin.Context) {
|
||||
})
|
||||
log.Println("触发信号")
|
||||
}
|
||||
|
||||
// loadTemplates loads templates from the embedded file system
|
||||
func loadTemplates(filesystem fs.FS) *template.Template {
|
||||
templ := template.Must(template.New("").ParseFS(filesystem, "*.html"))
|
||||
return templ
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user