mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
修复 鉴权令牌刷新
This commit is contained in:
@@ -3,28 +3,25 @@ package utils
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetRootDir 获取当前程序运行的真实根目录
|
||||
// 能够智能、跨平台地识别是编译后的可执行文件运行,还是通过 `go run` 运行(通常在临时目录下)
|
||||
// 能跨平台地识别是在生产环境执行二进制文件,还是在开发阶段使用 go run/test 乃至 IDE 调试运行
|
||||
func GetRootDir() string {
|
||||
var baseDir string
|
||||
|
||||
// 首先尝试获取当前工作目录
|
||||
workDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
workDir = "."
|
||||
}
|
||||
|
||||
// 获取程序可执行文件所在目录
|
||||
execPath, err := os.Executable()
|
||||
if err != nil {
|
||||
// 如果获取可执行文件路径失败,使用当前工作目录
|
||||
return workDir
|
||||
}
|
||||
|
||||
// 解析软链接,获取真实物理路径(macOS 下 /tmp 经常是 /private/tmp 的软链)
|
||||
realExecPath, err := filepath.EvalSymlinks(execPath)
|
||||
if err == nil {
|
||||
execPath = realExecPath
|
||||
@@ -36,27 +33,23 @@ func GetRootDir() string {
|
||||
realTempDir = os.TempDir()
|
||||
}
|
||||
|
||||
// 跨平台安全地判断 execDir 是否在 realTempDir 内部
|
||||
// 使用 filepath.Rel 可以避免直接 HasPrefix 带来的大小写、路径分隔符以及部分目录名重合的问题
|
||||
rel, err := filepath.Rel(realTempDir, execDir)
|
||||
isGoRun := false
|
||||
if err == nil {
|
||||
// 如果 rel 不以 ".." 开头,说明 execDir 在 TempDir 内部,即为 go run 模式
|
||||
if rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
isGoRun = true
|
||||
}
|
||||
} else {
|
||||
// fallback: 如果 Rel 失败(例如跨盘符),则退回简单的 HasPrefix 判断(带上分隔符防误判)
|
||||
cleanTemp := filepath.Clean(realTempDir) + string(os.PathSeparator)
|
||||
cleanExec := filepath.Clean(execDir) + string(os.PathSeparator)
|
||||
if strings.HasPrefix(strings.ToLower(cleanExec), strings.ToLower(cleanTemp)) {
|
||||
isGoRun = true
|
||||
}
|
||||
rel, err := filepath.Rel(realTempDir, execDir)
|
||||
if err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
isGoRun = true
|
||||
} else if strings.Contains(execPath, "go-build") || strings.Contains(execPath, "__debug_bin") || strings.HasSuffix(execPath, ".test") {
|
||||
isGoRun = true
|
||||
} else if strings.Contains(filepath.Base(execPath), "___go_build") || strings.HasPrefix(filepath.Base(execPath), "dlv") {
|
||||
isGoRun = true
|
||||
}
|
||||
|
||||
if isGoRun {
|
||||
baseDir = workDir
|
||||
// 开发模式下,利用 runtime 获取 utils/path.go 所在的绝对路径
|
||||
// 向上两级即可得到项目的真实绝对根目录,避免因终端 CWD 不同导致的配置读取和连接失败
|
||||
_, b, _, _ := runtime.Caller(0)
|
||||
baseDir = filepath.Dir(filepath.Dir(b))
|
||||
} else {
|
||||
// 生产模式下(正式编译的独立二进制),返回可执行文件所在目录
|
||||
baseDir = execDir
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user