代码拉取完成,页面将自动刷新
package config
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
)
const (
EProd = "prod"
ETest = "test"
EDev = "dev"
)
const (
KForever = "forever"
KDaemon = "daemon"
KPprof = "pprof"
KPath = "path"
KEnv = "env"
KConf = "conf"
KDataPath = "dataPath"
KAppId = "appid"
)
var (
Version = "1.0.0"
AppName = "unknown"
AppAlias = "" //别名,example 某某公司的某某应用
AppId string
SeqId int64 = 0
Source string
Env = EDev
WorkDir string
MainConf string
DataPath string //程序存储数据
Daemon bool
Forever bool
Pprof bool
)
var (
appExecPath string
envPath string
confPath string
appConfPath string
appDataPath string //程序运行数据目录,如views,docs
appDataPathV2 string
dataVersion int //数据版本
closeCh chan bool
)
type Config struct {
Mode string `json:"mode" yaml:"mode" xml:"mode"`
Server string `json:"server" yaml:"server" xml:"server"`
PubIP string `json:"publicIp" yaml:"publicIp" xml:"publicIp"`
DataVersion int `json:"dataVersion" yaml:"dataVersion" xml:"dataVersion"`
EnabledRedis bool `json:"enabledRedis" yaml:"enabledRedis" xml:"enabledRedis"`
EnabledDb bool `json:"enabledDb" yaml:"enabledDb" xml:"enabledDb"`
EnabledEs bool `json:"enabledEs" yaml:"enabledEs" xml:"enabledEs"`
EnabledMQ bool `json:"enabledMQ" yaml:"enabledMQ" xml:"enabledMQ"`
}
func (*Config) Init() {
if len(Env) <= 0 {
Env = EDev
}
if WorkDir == "." || WorkDir == "" {
app := GetExecPath()
if len(app) > 0 {
WorkDir = app
}
}
WorkDir += "/"
WorkDir = filepath.Dir(WorkDir)
envPath = fmt.Sprintf("%s/env", WorkDir)
confPath = fmt.Sprintf("%s/conf", WorkDir)
appConfPath = fmt.Sprintf("%s/%s", confPath, AppName)
if len(appDataPath) <= 0 {
appDataPath = WorkDir
}
if len(MainConf) <= 0 {
MainConf = fmt.Sprintf("%s/%s.main.json", appConfPath, Env)
}
if len(DataPath) <= 0 {
DataPath = fmt.Sprintf("%s/data/%s", WorkDir, AppName)
}
Source = AppName
}
func init() {
var _ = AppAlias
var _ = closeCh
closeCh = make(chan bool)
flag.StringVar(&Env, KEnv, EDev, "dev,test,prod")
flag.StringVar(&WorkDir, KPath, ".", "path of work")
flag.StringVar(&MainConf, KConf, "", "config path of main.json")
flag.StringVar(&AppId, KAppId, "", "appid")
flag.StringVar(&DataPath, KDataPath, "", "data path")
flag.BoolVar(&Forever, KForever, false, "forever run")
flag.BoolVar(&Daemon, KDaemon, false, "daemon run")
flag.BoolVar(&Pprof, KPprof, false, "pprof switch")
flag.Usage = func() {
flag.PrintDefaults()
}
}
func IsProdEnvironment() bool {
return Env == EProd
}
func IsDevEnvironment() bool {
return Env == EDev
}
func IsTestEnvironment() bool {
return Env == ETest
}
// FileNameInEnvPath
// env/{Env}/name
func FileNameInEnvPath(name string) string {
return filepath.Join(envPath, Env, name)
}
// FileNameWithoutEnvInEnvPath
// env/name
func FileNameWithoutEnvInEnvPath(name string) string {
return filepath.Join(envPath, name)
}
// FileNameWithEnvInConfAppPath
// conf/appName 带env "conf/{appName}/{Env}.name"
func FileNameWithEnvInConfAppPath(name string) string {
return filepath.Join(appConfPath, WithEnv(name))
}
// FileNameWithoutEnvInConfAppPath
// conf/appName 不带env "conf/{appName}/name"
func FileNameWithoutEnvInConfAppPath(name string) string {
return filepath.Join(appConfPath, name)
}
// FileNameInConfPath
// conf/ "confPath/name"
func FileNameInConfPath(name string) string {
return filepath.Join(confPath, name)
}
// FileNameInAppDataPath
// conf/ "{workdir}/name" 或 {workdir}/app/name
func FileNameInAppDataPath(name string) string {
return filepath.Join(GetAppDataPath(), name)
}
// FileNameInAppDataPathV2
// conf/ "{workdir}/app/name"
func FileNameInAppDataPathV2(name string) string {
return filepath.Join(GetAppDataPathV2(), name)
}
// FileNameInDataPath
// conf/ 目录下 "{DataPath}/name"
func FileNameInDataPath(name string) string {
return filepath.Join(DataPath, name)
}
// FileNameWithEnvInDataPath
// conf/ 目录下 "{DataPath}/{Env}.name"
func FileNameWithEnvInDataPath(name string) string {
return filepath.Join(DataPath, WithEnv(name))
}
// FileNameInEnvDataPath
// conf/ 目录下 "{DataPath}/{Env}/name"
func FileNameInEnvDataPath(name string) string {
return filepath.Join(DataPath, Env, name)
}
// WithEnv
// 与env组合
func WithEnv(name string) string {
return fmt.Sprintf("%s.%s", Env, name)
}
// MainFileName env.main.json 全路径文件名
func MainFileName() string {
return MainConf
}
// GetConfPath ./conf
func GetConfPath() string {
return confPath
}
// GetConfAppPath ./conf/appName
func GetConfAppPath() string {
return appConfPath
}
// GetDataPath 业务定义存储路径
func GetDataPath() string {
return DataPath
}
// GetAppDataPath 程序运行数据目录,如views,docs
func GetAppDataPath() string {
if dataVersion >= 1 {
return GetAppDataPathV2()
}
return appDataPath
}
func GetAppDataPathV2() string {
if len(appDataPathV2) == 0 {
appDataPathV2 = fmt.Sprintf("%s/app", WorkDir)
}
return appDataPathV2
}
func GetEnvPath() string {
return envPath
}
func UseAppDataPathV2() {
UseDataVersion(1)
// to adjust
GetAppDataPath()
}
func UseDataVersion(ver int) {
dataVersion = ver
}
func GetExecPath() string {
if len(appExecPath) > 0 {
return appExecPath
}
path, err := exec.LookPath(os.Args[0])
if err != nil {
return ""
}
appExecPath = filepath.Dir(path)
return appExecPath
}
func Closed() <-chan bool {
return closeCh
}
func Close() {
close(closeCh)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。