加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
options.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
it-zhaoo 提交于 2022-07-12 15:37 . grpc
package go_sensitive
import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Options struct {
//默认脏词库
DefaultDictDataPath string `yaml:"DefaultDictDataPath"`
//自定义脏词库
UserDictDataPath string `yaml:"UserDictDataPath"`
//TCP监听地址
TCPAddr string `yaml:"TCPAddr"`
}
func NewOptions(configPath string) *Options {
var err error
workDir, err := workDir()
if err != nil {
panic(err)
}
if len(configPath) == 0 {
configPath = path.Join(workDir, "../conf/app.yaml")
}
bConf, err := ioutil.ReadFile(configPath)
if err != nil {
panic(err)
}
options := &Options{}
err = yaml.Unmarshal(bConf, options)
if err != nil {
panic(err)
}
return options
}
// return app work dir.
func workDir() (string, error) {
appPath, err := exec.LookPath(os.Args[0])
if err != nil {
return "", err
}
appPath, err = filepath.Abs(appPath)
if err != nil {
return "", err
}
// Note: we don't use path.Dir here because it does not handle case
// which path starts with two "/" in Windows: "//psf/Home/..."
appPath = strings.Replace(appPath, "\\", "/", -1)
i := strings.LastIndex(appPath, "/")
if i == -1 {
return appPath, nil
}
return appPath[:i], nil
}
func init() {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化