Skip to content

Commit 795c583

Browse files
committed
feat: .env持久化
1 parent e4d97f7 commit 795c583

5 files changed

Lines changed: 47 additions & 0 deletions

File tree

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BILI_ROOM=7394063
2+
BILI_COOKIE="buvid3=49FFE070-F309-D1B1-B8FA-00D463C9D3D203375infoc; SESSDATA=5997b3dc%2C1788271713%2C04fd6%2A32CjBs3VQzSERGzBZF9dQe8QuArTf55vMgIAO69yAEHFsdTJ9lz7Wex0e8bTaM5vaBd4cSVk5JUUgxRUE5RHJsLVBXaGZDU2xwWTBkNWRMdEotOVVrS2lBS1FPRWcwVmo1Z3lXZW01MGZJYlZxWFotUHdNQ0V1UE1uUkdHdDhRWTdIX2V5YjJ4dFFRIIEC; bili_jct=791fe73eb5f1f0249f0ce1d1860d5afd"

.env_example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Bilibili 自动监听配置
2+
# BILI_ROOM: 你的B站直播间房间号
3+
BILI_ROOM=
4+
5+
# BILI_COOKIE: 你的B站Cookie (可选,如果不需要登录身份则留空)
6+
BILI_COOKIE=

docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ services:
88
ports:
99
- "8080:8080"
1010
- "7777:7777"
11+
env_file:
12+
- .env
1113
volumes:
1214
- ./data:/app/data
1315
restart: always

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ func main() {
8686
Value: 0,
8787
Usage: "日志等级 (0: 默认, 1: 简洁, 2: 静默)",
8888
},
89+
// 新增:B站房间号和Cookie环境变量注入
90+
&cli.IntFlag{
91+
Name: "biliRoom",
92+
EnvVars: []string{"BILI_ROOM"},
93+
Usage: "Bilibili 房间号 (自动启动监听)",
94+
},
95+
&cli.StringFlag{
96+
Name: "biliCookie",
97+
EnvVars: []string{"BILI_COOKIE"},
98+
Usage: "Bilibili Cookie",
99+
},
89100
},
90101
Action: func(c *cli.Context) error {
91102
// 初始化 Trace
@@ -128,6 +139,13 @@ func main() {
128139
// 恢复之前保存的服务状态
129140
api.RecoverService()
130141

142+
// 新增:通过环境变量自动启动 Bilibili 服务
143+
biliRoom := c.Int("biliRoom")
144+
if biliRoom > 0 {
145+
biliCookie := c.String("biliCookie")
146+
api.AutoStartBilibili(biliRoom, biliCookie)
147+
}
148+
131149
// 处理程序信号以进行优雅退出
132150
trace.HandleSignal()
133151

services/api/restful.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,22 @@ func ServeDashboard(w http.ResponseWriter, r *http.Request) {
582582
w.WriteHeader(http.StatusOK)
583583
_, _ = w.Write(web.DashboardHTML)
584584
}
585+
586+
// AutoStartBilibili 提供给 main 包调用的自动启动 Bilibili 服务的方法
587+
func AutoStartBilibili(roomID int, cookie string) {
588+
serviceKey := generateServiceKey("bilibili", strconv.Itoa(roomID))
589+
590+
// 如果由于 RecoverService 已经从文件恢复了这个房间,则跳过
591+
if _, exists := serviceMap.GetService(serviceKey); exists {
592+
log.Printf("INFO", "Bilibili 房间 %d 已在监听(已从持久化配置恢复),跳过自动启动", roomID)
593+
return
594+
}
595+
596+
stopChan := make(chan struct{})
597+
err := startBilibiliService(roomID, cookie, stopChan)
598+
if err != nil {
599+
log.Printf("ERROR", "通过环境变量自动启动 Bilibili 监听失败: %v", err)
600+
} else {
601+
log.Printf("INFO", "通过环境变量自动启动 Bilibili 监听成功 (房间: %d)", roomID)
602+
}
603+
}

0 commit comments

Comments
 (0)