changed param logic & add more routes
This commit is contained in:
65
master/master.go
Normal file
65
master/master.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package master
|
||||
|
||||
import (
|
||||
"bdgrp_go/pbutil"
|
||||
"bdgrp_go/utils"
|
||||
"bytes"
|
||||
"compress/bzip2"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
cache map[string]any
|
||||
mu sync.RWMutex
|
||||
)
|
||||
|
||||
func GetMaster() map[string]any {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
if cache != nil {
|
||||
return cache
|
||||
}
|
||||
mu.RUnlock()
|
||||
return ReloadMaster()
|
||||
}
|
||||
|
||||
func ReloadMaster() map[string]any {
|
||||
server := os.Getenv("SERVER")
|
||||
filePath := filepath.Join("resp", server, "suitemaster.bz2")
|
||||
|
||||
fileBuffer, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return cache
|
||||
}
|
||||
|
||||
decrypted, err := utils.Decrypt(fileBuffer)
|
||||
if err != nil {
|
||||
return cache
|
||||
}
|
||||
|
||||
// bzip2 解壓
|
||||
decompressed, err := io.ReadAll(bzip2.NewReader(bytes.NewReader(decrypted)))
|
||||
if err != nil {
|
||||
return cache
|
||||
}
|
||||
|
||||
// protobuf decode → JSON map
|
||||
msg, err := pbutil.Decode("SuiteMasterGetResponse", decompressed)
|
||||
if err != nil {
|
||||
return cache
|
||||
}
|
||||
|
||||
result, err := pbutil.ToMap(msg)
|
||||
if err != nil {
|
||||
return cache
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
cache = result
|
||||
mu.Unlock()
|
||||
|
||||
return cache
|
||||
}
|
||||
Reference in New Issue
Block a user