From 92d17ab448b6c9fc1d19a6a2ec6254a3ff4c8dcd Mon Sep 17 00:00:00 2001 From: "CN\\liby71" <469997798@qq.com> Date: Tue, 16 Nov 2021 22:09:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=AE=9A=E4=B9=89=E5=9F=BA?= =?UTF-8?q?=E5=87=86=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/base/path.go | 51 ++++++++++++++++++++++++ pkg/base/this directory can not be moved | 0 pkg/common/config/config.go | 26 +++++++----- 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 pkg/base/path.go create mode 100644 pkg/base/this directory can not be moved diff --git a/pkg/base/path.go b/pkg/base/path.go new file mode 100644 index 000000000..0c500dec6 --- /dev/null +++ b/pkg/base/path.go @@ -0,0 +1,51 @@ +/** +为确保运行结果正常输出,请不要移动此文件 + */ +package base + +import ( + "errors" + "path/filepath" + "runtime" +) + +// 获取项目基准目录 +func baseDir(p string) (string, error) { + _, file, _, ok := runtime.Caller(0) + if !ok { + return "", errors.New("runtime.Caller error.") + } + dir:= filepath.Dir( filepath.Dir( filepath.Dir( file ) ) ) + return dir+ p, nil +} +// 获取项目根目录 +func ProjectDir(p string) string { + if p=="" { + p= "/" + } + dir, err:= baseDir(p) + if err!= nil { + panic(err) + } + return dir +} +// 获取各个常用目录 +func ConfigDir() string { + return ProjectDir("/config/") +} +func ScriptDir() string { + return ProjectDir("/script/") +} +func BinDir() string { + return ProjectDir("/bin/") +} +func LogDir() string { + return ProjectDir("/logs/") +} +func PkgDir() string { + return ProjectDir("/pkg/") +} +func InternalDir() string { + return ProjectDir("/internal/") +} + diff --git a/pkg/base/this directory can not be moved b/pkg/base/this directory can not be moved new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index f10abdc0d..d26ec0204 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -1,21 +1,20 @@ package config import ( + "Open_IM/pkg/base" "io/ioutil" - "gopkg.in/yaml.v3" - - "path/filepath" - "runtime" + //"path/filepath" + //"runtime" ) var Config config -var ( - _, b, _, _ = runtime.Caller(0) - // Root folder of this project - Root = filepath.Join(filepath.Dir(b), "../../..") -) +//var ( +// _, b, _, _ = runtime.Caller(0) +// // Root folder of this project +// Root = filepath.Join(filepath.Dir(b), "../../..") +//) type config struct { ServerIP string `yaml:"serverip"` @@ -162,9 +161,16 @@ type config struct { } func init() { + confDir:= base.ConfigDir() + logDir:= base.LogDir() + + // fix log dir + Config.Log.StorageLocation= logDir + // if we cd Open-IM-Server/src/utils and run go test // it will panic cannot find config/config.yaml - bytes, err := ioutil.ReadFile(Root + "/config/config.yaml") + bytes, err := ioutil.ReadFile(confDir + "config.yaml") + //bytes, err := ioutil.ReadFile(Root + "/config/config.yaml") if err != nil { panic(err) }