package conf import ( "fmt" "os" "strconv" "strings" "assetcenter/log" "github.com/spf13/viper" ) const ( SaveType_Obs = "obs" SaveType_OSS = "oss" SaveType_Minio = "minio" ) type TaskConfig struct { Name string Broker string DefaultQueue string ResultBackend string ResultsExpireIn int32 AmqpExchange string AmqpExchangeType string AmqpBindingKey string AmqpPrefetchCount int32 } type AppAsset struct { Name string //鞋楦 Type string //"last" IsMesh bool //是否转换模型true IsImage bool DefaultCategory []string //默认的目录设定 } type AppCatogry struct { Scope string //global全局设定 qiye 企业级设定 StartUpdate bool //程序启动的时候是否更新category 表数据 和 AssetCategry表的数据 QiyeUserId string //scope为qiye的时候默认企业主账号ID DefaultCategory []*DefaultCategoryConf } type DefaultCategoryConf struct { Children []*DefaultCategoryConf Name string //分类名字 Value string //分类的值 } type MinioConfig struct { Bucket string Endpoint string AccessKey string AccessSec string } type AppConf struct { Name string Version string SaveType string //obs, oss, minio LocalSaveMode bool //本地存储模式, 数据库里面只存储key, 不存储host地址,以便应对本地IP变化后,无法访问资源的问题。 Assets []*AppAsset Category *AppCatogry Jwt struct { Realm string Key string TimeoutHour int } Etcd struct { Endpoints []string UserName string Password string DialTimeout int64 } Mysql struct { DSN string } Redis struct { Addr string Password string // no password set Db int // use default DB } Log struct { FileName string Level int32 ServiceName string } Port int32 Mongo struct { DSN string Database string } Debug struct { UserId string UserPhone string UserRole string } Obs struct { Bucket string AccessKeyId string SecrateKey string Endpoint string } Minio MinioConfig Nats struct { Url string MaxReconnect int ReconnDelaySecond int MeshProcStreamTopic string HdrProcStreamTopic string OsgConvStreamTopic string OsgShadowStreamTopic string HdrConvStreamTopic string } TaskCenter string TreeDNS string TrimLocalUrl string SearchImageAddr string } func (c *AppConf) IsSaveLocal() bool { return c.LocalSaveMode && c.SaveType == SaveType_Minio } func (c *AppConf) TrimSaveLocalUrl(src string) string { if strings.HasPrefix(src, c.TrimLocalUrl) { return strings.TrimPrefix(src, c.TrimLocalUrl) } return src } func (c *AppConf) GetSaveLocalUrl(src string) string { if len(src) < 1 { return src } if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, "https://") { return src } return "//" + c.Minio.Endpoint + "/" + src } func LoadConfFile(filepath string) (*AppConf, error) { file, err := os.Open(filepath) if err != nil { log.Errorf("can not open file:%s", filepath) return nil, err } v := viper.New() v.SetConfigType("yaml") err = v.ReadConfig(file) if err != nil { return nil, err } c := new(AppConf) err = v.Unmarshal(c) return c, err } func NewAppConf(filePath string) (*AppConf, error) { c, err := LoadConfFile(filePath) if err != nil { return c, err } //初始化log _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level) //环境变量 mongo := os.Getenv("MONGO") fmt.Println("env MONGO=>", mongo) if len(mongo) > 0 { c.Mongo.DSN = mongo //解析出database mongodb://admin:123456@124.70.148.113:27018/sku3d_dadong?authSource=admin start := strings.LastIndex(mongo, "/") end := strings.LastIndex(mongo, "?") database := mongo[start+1 : end] if len(database) > 0 { c.Mongo.Database = database } fmt.Println(c.Mongo.DSN, c.Mongo.Database) } //redis redis := os.Getenv("REDIS") fmt.Println("Env REDIS=>", redis) if len(redis) > 0 { c.Redis.Addr = redis start := strings.LastIndex(redis, "/") if start > 0 { db := redis[start:] dbint, _ := strconv.Atoi(db) c.Redis.Db = dbint c.Redis.Addr = string(redis[0:start]) } fmt.Println(c.Redis.Addr, c.Redis.Db) } //taskCenter natsHost := os.Getenv("NATS") if len(natsHost) > 0 { c.Nats.Url = natsHost } fmt.Println("Env NATS=>", natsHost) //taskCenter treeDns := os.Getenv("TREEDNS") if len(treeDns) > 0 { c.TreeDNS = treeDns } fmt.Println("Env TREEDNS=>", c.TreeDNS) searchImageAddr := os.Getenv("SEARCH_IMAGE_ADDR") if len(searchImageAddr) > 0 { c.SearchImageAddr = searchImageAddr } fmt.Println("search image addr=>", searchImageAddr) AppConfig = c return c, nil } var AppConfig *AppConf = nil