-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
553 lines (472 loc) · 13.6 KB
/
Copy pathmain.go
File metadata and controls
553 lines (472 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
// --- 数据结构定义 ---
// IndexEntry 对应 index.jsonl 中的行
type IndexEntry struct {
ID string `json:"id"`
RawLyricFile string `json:"rawLyricFile"`
MetadataRaw [][]interface{} `json:"metadata"`
SearchBlob string // 预处理的全文本索引(小写)
}
// SearchResult 对应 API 文档中的搜索结果格式
type SearchResult struct {
ID string `json:"id"`
RawLyricFile string `json:"rawLyricFile"`
Metadata [][]interface{} `json:"metadata"`
Platforms []string `json:"platforms"`
}
// --- 全局变量 ---
var (
// 命令行参数
repoURL = "https://github.com/Steve-xmh/amll-ttml-db.git"
noSync = flag.Bool("no-sync", false, "Disable git sync and use local data only")
noDownload = flag.Bool("no-download", false, "Disable the download API")
inputDataDir = flag.String("data-dir", "lyric-data", "Preferred path to the data directory")
syncInterval = flag.Duration("interval", 10*time.Minute, "Interval for automatic sync")
port = flag.String("port", "43594", "Server port")
// 内存数据库
dataStore = make(map[string][]IndexEntry)
platformPaths = make(map[string]string)
platforms = []string{"ncm", "qq", "am", "spotify", "raw"}
actualDataDir string
lastUpdateTime time.Time
// 并发控制
mu sync.RWMutex // 保护数据索引
gitMu sync.Mutex // 保护 Git 操作
// 查询缓存
queryCache = make(map[string][]SearchResult)
queryCacheMu sync.RWMutex
queryCacheTTL = 5 * time.Minute
queryTimestamp = make(map[string]time.Time)
)
// --- 路径嗅探逻辑 ---
func isDataDir(path string) bool {
indicators := []string{"ncm-lyrics", "qq-lyrics", "metadata"}
for _, ind := range indicators {
if info, err := os.Stat(filepath.Join(path, ind)); err == nil && info.IsDir() {
return true
}
}
return false
}
func findValidDataDir() string {
if isDataDir(*inputDataDir) {
p, _ := filepath.Abs(*inputDataDir)
return p
}
if isDataDir(".") {
p, _ := filepath.Abs(".")
return p
}
if isDataDir("..") {
p, _ := filepath.Abs("..")
return p
}
subDirs := []string{"lyric-data", "amll-ttml-db", "data"}
for _, sub := range subDirs {
if isDataDir(sub) {
p, _ := filepath.Abs(sub)
return p
}
}
return ""
}
// --- Git 同步与索引加载 ---
func syncRepo() bool {
if *noSync {
return false
}
gitMu.Lock()
defer gitMu.Unlock()
absTarget, _ := filepath.Abs(*inputDataDir)
if _, err := os.Stat(filepath.Join(absTarget, ".git")); os.IsNotExist(err) {
log.Printf("Repository not found. Initializing clone to %s...", absTarget)
cmd := exec.Command("git", "clone", "--depth", "1", repoURL, absTarget)
if err := cmd.Run(); err != nil {
log.Printf("Git clone failed: %v", err)
return false
}
return true
}
log.Println("Performing incremental update (git pull)...")
cmd := exec.Command("git", "-C", absTarget, "pull")
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Git pull failed: %v", err)
return false
}
return !strings.Contains(string(output), "Already up to date")
}
func loadMetadata() {
root := findValidDataDir()
if root == "" {
log.Println("Warning: No valid data directory found. API will return empty results.")
return
}
actualDataDir = root
configs := map[string]string{
"ncm": filepath.Join(root, "ncm-lyrics", "index.jsonl"),
"qq": filepath.Join(root, "qq-lyrics", "index.jsonl"),
"am": filepath.Join(root, "am-lyrics", "index.jsonl"),
"spotify": filepath.Join(root, "spotify-lyrics", "index.jsonl"),
"raw": filepath.Join(root, "metadata", "raw-lyrics-index.jsonl"),
}
tempStore := make(map[string][]IndexEntry)
tempPaths := make(map[string]string)
for key, path := range configs {
file, err := os.Open(path)
if err != nil {
continue
}
tempPaths[key] = filepath.Dir(path)
// 优化:预分配容量以减少扩容
var entries []IndexEntry
scanner := bufio.NewScanner(file)
// 优化:增大缓冲区以提高读取性能
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
for scanner.Scan() {
var entry IndexEntry
if err := json.Unmarshal(scanner.Bytes(), &entry); err == nil {
// 预处理 SearchBlob
var sb strings.Builder
sb.Grow(len(entry.ID) + len(entry.RawLyricFile) + 256) // 预分配容量
sb.WriteString(strings.ToLower(entry.ID))
sb.WriteString(" ")
sb.WriteString(strings.ToLower(entry.RawLyricFile))
sb.WriteString(" ")
for _, pair := range entry.MetadataRaw {
if len(pair) >= 2 {
if values, ok := pair[1].([]interface{}); ok {
for _, v := range values {
if s, ok := v.(string); ok {
sb.WriteString(strings.ToLower(s))
sb.WriteString(" ")
}
}
}
}
}
entry.SearchBlob = sb.String()
entries = append(entries, entry)
}
}
file.Close()
tempStore[key] = entries
}
mu.Lock()
dataStore = tempStore
platformPaths = tempPaths
lastUpdateTime = time.Now()
mu.Unlock()
total := getTotalCount()
log.Printf("Metadata reloaded. Root: %s, Total entries: %d", actualDataDir, total)
}
func getTotalCount() int {
count := 0
for _, v := range dataStore {
count += len(v)
}
return count
}
// --- 查询缓存管理 ---
func getFromCache(query string) ([]SearchResult, bool) {
queryCacheMu.RLock()
defer queryCacheMu.RUnlock()
if results, ok := queryCache[query]; ok {
if time.Since(queryTimestamp[query]) < queryCacheTTL {
return results, true
}
}
return nil, false
}
func saveToCache(query string, results []SearchResult) {
queryCacheMu.Lock()
defer queryCacheMu.Unlock()
queryCache[query] = results
queryTimestamp[query] = time.Now()
// 清理过期缓存
if len(queryCache) > 1000 {
now := time.Now()
for k, t := range queryTimestamp {
if now.Sub(t) > queryCacheTTL {
delete(queryCache, k)
delete(queryTimestamp, k)
}
}
}
}
func clearCache() {
queryCacheMu.Lock()
defer queryCacheMu.Unlock()
queryCache = make(map[string][]SearchResult)
queryTimestamp = make(map[string]time.Time)
log.Println("Query cache cleared")
}
// --- 中间件 ---
func Middleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next(w, r)
log.Printf("[%s] %s %s %v", r.Method, r.URL.Path, r.RemoteAddr, time.Since(start))
}
}
// --- 接口处理器 ---
func statusHandler(w http.ResponseWriter, r *http.Request) {
mu.RLock()
defer mu.RUnlock()
stats := make(map[string]int)
for k, v := range dataStore {
stats[k] = len(v)
}
queryCacheMu.RLock()
cacheSize := len(queryCache)
queryCacheMu.RUnlock()
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "active",
"last_update_time": lastUpdateTime.Format("2006-01-02 15:04:05"),
"total_entries": getTotalCount(),
"platform_stats": stats,
"repo_url": repoURL,
"cache_size": cacheSize,
})
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
// 添加上下文超时控制
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
var query string
var targetPlatforms []string
if r.Method == http.MethodPost {
var body struct {
Query string `json:"query"`
Platforms []string `json:"platforms"`
}
json.NewDecoder(r.Body).Decode(&body)
query = body.Query
targetPlatforms = body.Platforms
} else {
query = r.URL.Query().Get("query")
targetPlatforms = r.URL.Query()["platforms"]
}
query = strings.ToLower(strings.TrimSpace(query))
if query == "" {
json.NewEncoder(w).Encode(map[string]interface{}{"status": "success", "count": 0, "results": []SearchResult{}})
return
}
if len(targetPlatforms) == 0 {
targetPlatforms = platforms
}
// 尝试从缓存获取
if cachedResults, ok := getFromCache(query); ok {
log.Printf("Cache hit for query: %s", query)
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"count": len(cachedResults),
"results": cachedResults,
"cached": true,
})
return
}
// 预分配结果通道容量
resultChan := make(chan []SearchResult, len(targetPlatforms))
var wg sync.WaitGroup
// 并行搜索每个平台
for _, p := range targetPlatforms {
wg.Add(1)
go func(pName string) {
defer wg.Done()
// 检查上下文是否已取消
select {
case <-ctx.Done():
resultChan <- []SearchResult{}
return
default:
}
mu.RLock()
data := dataStore[pName]
mu.RUnlock()
// 预分配结果切片容量(假设匹配率约5-10%)
estimatedSize := len(data) / 20
if estimatedSize < 10 {
estimatedSize = 10
}
found := make([]SearchResult, 0, estimatedSize)
// 使用strings.Index替代strings.Contains以获得更好性能
for _, entry := range data {
if strings.Index(entry.SearchBlob, query) >= 0 {
found = append(found, SearchResult{
ID: entry.ID,
RawLyricFile: entry.RawLyricFile,
Metadata: entry.MetadataRaw,
Platforms: []string{pName},
})
}
}
resultChan <- found
}(p)
}
// 等待所有goroutine完成
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
// 超时控制
select {
case <-done:
case <-ctx.Done():
w.WriteHeader(http.StatusRequestTimeout)
json.NewEncoder(w).Encode(map[string]string{"error": "Search timeout"})
return
}
close(resultChan)
// 更高效的结果合并和去重
// 预分配map容量以减少扩容
estimatedResults := getTotalCount() / 50
if estimatedResults < 100 {
estimatedResults = 100
}
finalMap := make(map[string]*SearchResult, estimatedResults)
for list := range resultChan {
for i := range list {
item := &list[i]
if existing, ok := finalMap[item.RawLyricFile]; ok {
// 避免重复分配,直接append到existing.Platforms
existing.Platforms = append(existing.Platforms, item.Platforms...)
} else {
finalMap[item.RawLyricFile] = item
}
}
}
// 预分配最终结果切片
finalResults := make([]SearchResult, 0, len(finalMap))
for _, v := range finalMap {
finalResults = append(finalResults, *v)
}
// 保存到缓存
if len(finalResults) > 0 {
saveToCache(query, finalResults)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"count": len(finalResults),
"results": finalResults,
})
}
func downloadHandler(w http.ResponseWriter, r *http.Request) {
if *noDownload {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]string{"error": "Download API is disabled by server configuration"})
return
}
var platform, musicId, format string
if r.Method == http.MethodPost {
var body struct {
Platform string `json:"platform"`
MusicID string `json:"musicId"`
Format string `json:"format"`
}
json.NewDecoder(r.Body).Decode(&body)
platform, musicId, format = body.Platform, body.MusicID, body.Format
} else {
platform = r.URL.Query().Get("platform")
musicId = r.URL.Query().Get("musicId")
format = r.URL.Query().Get("format")
}
if format == "" {
format = "ttml"
}
mu.RLock()
dir, ok := platformPaths[platform]
mu.RUnlock()
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid platform"})
return
}
filePath := filepath.Join(dir, musicId+"."+format)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "Lyric file not found"})
return
}
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filepath.Base(filePath)))
http.ServeFile(w, r, filePath)
}
func formatsHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode([]string{"ttml", "lrc", "yrc", "qrc", "lys"})
}
func updateHandler(w http.ResponseWriter, r *http.Request) {
if *noSync {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]string{"error": "Git sync is disabled by server configuration"})
return
}
updated := syncRepo()
if updated {
loadMetadata()
clearCache() // 清除缓存以使用新数据
json.NewEncoder(w).Encode(map[string]string{"message": "Update successful and metadata reloaded"})
} else {
json.NewEncoder(w).Encode(map[string]string{"message": "Already up to date"})
}
}
// --- 主程序入口 ---
func main() {
flag.Parse()
log.SetFlags(log.LstdFlags)
log.Println("Starting AMLL TTML API Server (Optimized)...")
// 1. 初始化 Git 同步
if !*noSync {
syncRepo()
}
// 2. 加载元数据
loadMetadata()
// 3. 启动定时更新协程
if !*noSync {
go func() {
ticker := time.NewTicker(*syncInterval)
for range ticker.C {
if syncRepo() {
loadMetadata()
clearCache()
}
}
}()
}
// 4. 路由注册
http.HandleFunc("/api/status", Middleware(statusHandler))
http.HandleFunc("/api/search", Middleware(searchHandler))
http.HandleFunc("/api/download", Middleware(downloadHandler))
http.HandleFunc("/api/formats", Middleware(formatsHandler))
http.HandleFunc("/api/update", Middleware(updateHandler))
// 5. 启动服务
log.Printf("Server is listening on :%s", *port)
if err := http.ListenAndServe(":"+*port, nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}