[gogf/gf]改变配置文件默认的目录或者文件名后,编译后的二进制文件复制到其他环境提示找不到配置文件

2024-07-09 879 views
0

go 版本 1.11

v1.13.4

不知道呀

第一种情况,使用 g.Cfg().SetFileName(configPath),设置配置文件名为 : config-dev.yaml;

第二种情况

  • 使用 g.Cfg().SetPath("etc") 设置配置文件目录为 etc
  • 使用 g.Cfg().SetFileName(configPath),设置配置文件名为 : config-dev.yaml
  • 将etc 配置目录打包 gf pack etc packed/data.go -n packed
  • 在 main.go 第一个引入 _ boot,并且在 boot 第一个引入 packed

本机是 deepin linux,go 1.15,在本机开发,使用 goland 调试,或者 go build 成二进制都没有问题。但是在本地编译后,将二进制文件复制到其他环境,比如本地的虚拟机(ubuntu),或者复制到服务器 (centos),就会提示找不到配置文件,上面两种情况都会出现找不到配置文件的情况。如果在服务器去编译,同时在服务器运行,就不会有问题。

期望使用 g.Cfg().SetFileName(configPath) 和 使用 g.Cfg().SetPath("etc") 能够正常的设置配置文件名、以及文件路径,不会出现二进制文件复制到其他环境后找不到配置文件

回答

8

@dabuge 给个可以复现这个问题的代码吧,我直接运行科复现的,这样比较高效一些。

2

好,稍等,我搞两个项目代码出来

1

@gqcn 配置文件打包后,在读取的时候,只从当前目录或者config目录里读取了,在 AddPath 时 应该更新 配置文件打包目录数组

resourceTryFiles = []string{"", "/", "config/", "config", "/config", "/config/"}

1

完整的复现流程

  • gf update
  • gf init s1
  • cd s1 && mv config etc
  • cd etc && mv config.toml config.yaml
  • 修改 config.yaml 内容
    
    # HTTP Server
    server:
    Address: ":8199"
    ServerRoot: "public"
    ServerAgent: "gf-app"
    LogPath: "/tmp/log/gf-app/server"
Logger.

logger: Path: "/tmp/log/gf-app" Level: "all" Stdout: true

Template.

viewer: Path: "template" DefaultFile: "index.html" Delimiters: [ "${", "}" ]

Database.

database: link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test" debug: true

Database logger.

database.logger: Path: "/tmp/log/gf-app/sql" Level: "all" Stdout: true

- `cd ../ && gf pack etc,public,template,i18n packed/data.go -n packed`
- 修改 boot.go 的内容

package boot

import ( _ "s1/packed"

"github.com/gogf/gf/frame/g"

)

func init() { _ = g.Cfg().SetPath("etc") g.Cfg().SetFileName("config.yaml")

g.Res().Dump()

}

- `gf build`
- 将生成的 main 文件复制到虚拟机运行,报错

vagrant@homestead:~$ mv sail_code/test/s1/main s1 vagrant@homestead:~$ ./s1 2021-01-06T09:30:54+00:00 0.00B etc 2021-01-06T09:02:40+00:00 0.00B etc/.gitkeep 2021-01-06T09:27:30+00:00 492.00B etc/config.yaml 2021-01-06T09:30:54+00:00 0.00B i18n 2021-01-06T09:02:40+00:00 0.00B i18n/.gitkeep 2021-01-06T09:30:54+00:00 0.00B public 2021-01-06T09:02:40+00:00 0.00B public/html 2021-01-06T09:02:40+00:00 0.00B public/html/.gitkeep 2021-01-06T09:02:40+00:00 0.00B public/plugin 2021-01-06T09:02:40+00:00 0.00B public/plugin/.gitkeep 2021-01-06T09:02:40+00:00 0.00B public/resource 2021-01-06T09:02:40+00:00 0.00B public/resource/css 2021-01-06T09:02:40+00:00 0.00B public/resource/css/.gitkeep 2021-01-06T09:02:40+00:00 0.00B public/resource/image 2021-01-06T09:02:40+00:00 0.00B public/resource/image/.gitkeep 2021-01-06T09:02:40+00:00 0.00B public/resource/js 2021-01-06T09:02:40+00:00 0.00B public/resource/js/.gitkeep 2021-01-06T09:30:54+00:00 0.00B template 2021-01-06T09:02:40+00:00 0.00B template/.gitkeep TOTAL FILES: 19

SERVER DOMAIN ADDRESS METHOD ROUTE HANDLER MIDDLEWARE
default default :80 ALL / s1/app/api/hello.Hello
--------- --------- --------- -------- ------- ------------------------ ------------

2021-01-06 09:31:52.734 [FATA] 4041: net.Listen error: listen tcp :80: bind: permission denied Stack:

  1. github.com/gogf/gf/net/ghttp.(*Server).startServer.func1 /home/dabuge/go/pkg/mod/github.com/gogf/gf@v1.15.0/net/ghttp/ghttp_server.go:431

vagrant@homestead:~$


没有找到正确的配置文件,导致使用了默认的80端口,所以报错
2

感动啊,我解决了快上千个Issue,很难见得到这么规范的Issue复现流程。好的,我马上瞅瞅。

0

非常感谢,用这种方法修改配置文件,完全是为了兼容老的项目规范,没办法,哈哈