数据结构优化
This commit is contained in:
@@ -3,6 +3,8 @@ package utils
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
@@ -15,28 +17,73 @@ var (
|
||||
|
||||
// InitIP2Region 初始化 ip2region
|
||||
// 需要 ip2region.xdb 文件,如果不存在则仅支持基本IP解析
|
||||
// 如果 dbPath 为空,将自动从环境变量或可执行文件目录查找
|
||||
func InitIP2Region(dbPath string) {
|
||||
once.Do(func() {
|
||||
var err error
|
||||
// 1. 尝试加载整个xdb到内存,性能最好
|
||||
cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
var finalPath string
|
||||
|
||||
// 1. 如果传入了路径,直接使用
|
||||
if dbPath != "" {
|
||||
finalPath = dbPath
|
||||
} else {
|
||||
// 2. 优先从环境变量读取
|
||||
finalPath = os.Getenv("IP2REGION_DB_PATH")
|
||||
if finalPath == "" {
|
||||
// 3. 尝试在可执行文件同级目录查找
|
||||
execPath, err := os.Executable()
|
||||
if err == nil {
|
||||
execDir := filepath.Dir(execPath)
|
||||
candidatePath := filepath.Join(execDir, "ip2region.xdb")
|
||||
if _, err := os.Stat(candidatePath); err == nil {
|
||||
finalPath = candidatePath
|
||||
}
|
||||
}
|
||||
// 4. 如果还没找到,尝试当前工作目录(兼容开发环境)
|
||||
if finalPath == "" {
|
||||
candidatePath := "./ip2region.xdb"
|
||||
if _, err := os.Stat(candidatePath); err == nil {
|
||||
finalPath = candidatePath
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果最终路径为空,说明找不到文件
|
||||
if finalPath == "" {
|
||||
log.Printf("IP2Region database file not found. Region lookup will be disabled.")
|
||||
searcher = nil
|
||||
return
|
||||
}
|
||||
|
||||
// 5. 尝试加载整个xdb到内存,性能最好
|
||||
cBuff, err := xdb.LoadContentFromFile(finalPath)
|
||||
if err != nil {
|
||||
log.Printf("Failed to load ip2region.xdb: %v. Region lookup will be disabled.", err)
|
||||
log.Printf("Failed to load ip2region.xdb from %s: %v. Region lookup will be disabled.", finalPath, err)
|
||||
searcher = nil
|
||||
return
|
||||
}
|
||||
|
||||
searcher, err = xdb.NewWithBuffer(nil, cBuff)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create searcher: %v", err)
|
||||
searcher = nil
|
||||
return
|
||||
}
|
||||
log.Println("IP2Region loaded successfully")
|
||||
log.Printf("IP2Region loaded successfully from %s", finalPath)
|
||||
})
|
||||
}
|
||||
|
||||
// GetRegion 获取IP归属地
|
||||
// 返回格式: 国家|区域|省份|城市|ISP
|
||||
func GetRegion(ip string) string {
|
||||
// 添加 recover 保护,防止 panic 导致整个请求失败
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in GetRegion for IP %s: %v", ip, r)
|
||||
}
|
||||
}()
|
||||
|
||||
if searcher == nil {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
@@ -68,9 +68,18 @@ type OSSUploader interface {
|
||||
func GetOSSUploader(config *OSSConfig) (OSSUploader, error) {
|
||||
switch StorageType(config.StorageType) {
|
||||
case StorageLocal:
|
||||
// 从环境变量读取配置,如果未设置则使用默认值
|
||||
basePath := os.Getenv("UPLOADS_BASE_PATH")
|
||||
if basePath == "" {
|
||||
basePath = "./uploads"
|
||||
}
|
||||
baseURL := os.Getenv("UPLOADS_BASE_URL")
|
||||
if baseURL == "" {
|
||||
baseURL = "/uploads"
|
||||
}
|
||||
return &LocalUploader{
|
||||
BasePath: "./uploads",
|
||||
BaseURL: "/uploads",
|
||||
BasePath: basePath,
|
||||
BaseURL: baseURL,
|
||||
}, nil
|
||||
case StorageQCloud:
|
||||
// 优先使用专用字段,如果为空则使用通用字段(向后兼容)
|
||||
|
||||
Reference in New Issue
Block a user