[beego]在windows下,不以登录用户运行,出现找不到配置文件的错误

2024-01-16 225 views
2

├── Beego : Beego is not installed. Please do consider installing it first: https://github.com/beego/beego/v2. If you are using go mod, and you don't install the beego under $GOPATH/src/github.com/astaxie, just ignore this. ├── GoVersion : go1.15.11 ├── GOOS : windows ├── GOARCH : amd64 ├── NumCPU : 8 ├── GOPATH : C:\Users\toxin\go ├── GOROOT : C:/Users/toxin/sdk/go1.15.11 ├── Compiler : gc └── Date : Tuesday, 25 May 2021


2. What operating system and processor architecture are you using (`go env`)?

3. What did you do?
编译为exe文件后,如果直接双击或在命令行允许,无报错。
但如果以服务的方式运行,或者使用浏览器调用(通过注册表关联自定义协议类型)运行程序,则出现类似如下这样的错误:

2021/05/25 19:51:26.434 [W] init global config instance failed. If you donot use this, just ignore it. open conf/app.conf: The system cannot find the path specified.

logs.BeeLogger.SetLogger: open logs/nms-toxin.log: Access is denied.


目录给了everyone所有权限,beego1.x版本可以正常运行,但beego2无法运行。

回答

0

运行模式已改为prod仍然有此报错

8

经过反复测试,只要不在exe文件所在目录运行程序,就会出现无法读取配置文件。

0

已解决,主动修改go的工作目录为程序所在目录即可,但希望beego能添加默认支持:

   rootDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
   if err != nil {
      return
   }
   err = os.Chdir(rootDir)
1
├── GoVersion : go1.20.7
├── GOOS      : windows
├── GOARCH    : amd64
├── NumCPU    : 12
├── GOPATH    : C:\Users\44939\go
├── GOROOT    : D:\tools\golang
├── Compiler  : gc
└── Date      : Tuesday, 15 Aug 2023

我也遇到了,我解决方法:

func main() {
    // debug的时候会失败 可以用if判断是否加载
    rootDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
    os.Chdir(rootDir)
    config.InitGlobalInstance("ini", rootDir+"/conf/app.conf")
......
吐槽

我想通过 github.com/kardianos/service 来做成服务的,然后他指定的是绝对路径:D:\xxx\xxx\xxx.exe

而运行目录又在用户目录,本想通过 os.Chdir直接改变的,因为代码用了 config.String("xxx") ,一直报错。

然后一直google,半天都没找到各答案 难道没人这么玩吗? 感觉生态真差,还不如自己看源码。