app.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "assetcenter/log"
  8. "github.com/spf13/viper"
  9. )
  10. const (
  11. SaveType_Obs = "obs"
  12. SaveType_OSS = "oss"
  13. SaveType_Minio = "minio"
  14. )
  15. type TaskConfig struct {
  16. Name string
  17. Broker string
  18. DefaultQueue string
  19. ResultBackend string
  20. ResultsExpireIn int32
  21. AmqpExchange string
  22. AmqpExchangeType string
  23. AmqpBindingKey string
  24. AmqpPrefetchCount int32
  25. }
  26. type AppAsset struct {
  27. Name string //鞋楦
  28. Type string //"last"
  29. IsMesh bool //是否转换模型true
  30. IsImage bool
  31. DefaultCategory []string //默认的目录设定
  32. }
  33. type AppCatogry struct {
  34. Scope string //global全局设定 qiye 企业级设定
  35. StartUpdate bool //程序启动的时候是否更新category 表数据 和 AssetCategry表的数据
  36. QiyeUserId string //scope为qiye的时候默认企业主账号ID
  37. DefaultCategory []*DefaultCategoryConf
  38. }
  39. type DefaultCategoryConf struct {
  40. Children []*DefaultCategoryConf
  41. Name string //分类名字
  42. Value string //分类的值
  43. }
  44. type MinioConfig struct {
  45. Bucket string
  46. Endpoint string
  47. AccessKey string
  48. AccessSec string
  49. }
  50. type AppConf struct {
  51. Name string
  52. Version string
  53. SaveType string //obs, oss, minio
  54. LocalSaveMode bool //本地存储模式, 数据库里面只存储key, 不存储host地址,以便应对本地IP变化后,无法访问资源的问题。
  55. Assets []*AppAsset
  56. Category *AppCatogry
  57. Jwt struct {
  58. Realm string
  59. Key string
  60. TimeoutHour int
  61. }
  62. Etcd struct {
  63. Endpoints []string
  64. UserName string
  65. Password string
  66. DialTimeout int64
  67. }
  68. Mysql struct {
  69. DSN string
  70. }
  71. Redis struct {
  72. Addr string
  73. Password string // no password set
  74. Db int // use default DB
  75. }
  76. Log struct {
  77. FileName string
  78. Level int32
  79. ServiceName string
  80. }
  81. Port int32
  82. Mongo struct {
  83. DSN string
  84. Database string
  85. }
  86. Debug struct {
  87. UserId string
  88. UserPhone string
  89. UserRole string
  90. }
  91. Obs struct {
  92. Bucket string
  93. AccessKeyId string
  94. SecrateKey string
  95. Endpoint string
  96. }
  97. Minio MinioConfig
  98. Nats struct {
  99. Url string
  100. MaxReconnect int
  101. ReconnDelaySecond int
  102. MeshProcStreamTopic string
  103. HdrProcStreamTopic string
  104. OsgConvStreamTopic string
  105. OsgShadowStreamTopic string
  106. HdrConvStreamTopic string
  107. }
  108. TaskCenter string
  109. TreeDNS string
  110. TrimLocalUrl string
  111. SearchImageAddr string
  112. }
  113. func (c *AppConf) IsSaveLocal() bool {
  114. return c.LocalSaveMode && c.SaveType == SaveType_Minio
  115. }
  116. func (c *AppConf) TrimSaveLocalUrl(src string) string {
  117. if strings.HasPrefix(src, c.TrimLocalUrl) {
  118. return strings.TrimPrefix(src, c.TrimLocalUrl)
  119. }
  120. return src
  121. }
  122. func (c *AppConf) GetSaveLocalUrl(src string) string {
  123. if len(src) < 1 {
  124. return src
  125. }
  126. if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "//") || strings.HasPrefix(src, "https://") {
  127. return src
  128. }
  129. return "//" + c.Minio.Endpoint + "/" + src
  130. }
  131. func LoadConfFile(filepath string) (*AppConf, error) {
  132. file, err := os.Open(filepath)
  133. if err != nil {
  134. log.Errorf("can not open file:%s", filepath)
  135. return nil, err
  136. }
  137. v := viper.New()
  138. v.SetConfigType("yaml")
  139. err = v.ReadConfig(file)
  140. if err != nil {
  141. return nil, err
  142. }
  143. c := new(AppConf)
  144. err = v.Unmarshal(c)
  145. return c, err
  146. }
  147. func NewAppConf(filePath string) (*AppConf, error) {
  148. c, err := LoadConfFile(filePath)
  149. if err != nil {
  150. return c, err
  151. }
  152. //初始化log
  153. _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
  154. //环境变量
  155. mongo := os.Getenv("MONGO")
  156. fmt.Println("env MONGO=>", mongo)
  157. if len(mongo) > 0 {
  158. c.Mongo.DSN = mongo
  159. //解析出database mongodb://admin:123456@124.70.148.113:27018/sku3d_dadong?authSource=admin
  160. start := strings.LastIndex(mongo, "/")
  161. end := strings.LastIndex(mongo, "?")
  162. database := mongo[start+1 : end]
  163. if len(database) > 0 {
  164. c.Mongo.Database = database
  165. }
  166. fmt.Println(c.Mongo.DSN, c.Mongo.Database)
  167. }
  168. //redis
  169. redis := os.Getenv("REDIS")
  170. fmt.Println("Env REDIS=>", redis)
  171. if len(redis) > 0 {
  172. c.Redis.Addr = redis
  173. start := strings.LastIndex(redis, "/")
  174. if start > 0 {
  175. db := redis[start:]
  176. dbint, _ := strconv.Atoi(db)
  177. c.Redis.Db = dbint
  178. c.Redis.Addr = string(redis[0:start])
  179. }
  180. fmt.Println(c.Redis.Addr, c.Redis.Db)
  181. }
  182. //taskCenter
  183. natsHost := os.Getenv("NATS")
  184. if len(natsHost) > 0 {
  185. c.Nats.Url = natsHost
  186. }
  187. fmt.Println("Env NATS=>", natsHost)
  188. //taskCenter
  189. treeDns := os.Getenv("TREEDNS")
  190. if len(treeDns) > 0 {
  191. c.TreeDNS = treeDns
  192. }
  193. fmt.Println("Env TREEDNS=>", c.TreeDNS)
  194. searchImageAddr := os.Getenv("SEARCH_IMAGE_ADDR")
  195. if len(searchImageAddr) > 0 {
  196. c.SearchImageAddr = searchImageAddr
  197. }
  198. fmt.Println("search image addr=>", searchImageAddr)
  199. AppConfig = c
  200. return c, nil
  201. }
  202. var AppConfig *AppConf = nil