controller.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package abango
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "strings"
  6. "time"
  7. e "github.com/dabory/abango-rest/etc"
  8. "github.com/go-xorm/xorm"
  9. "gopkg.in/yaml.v2"
  10. )
  11. func (c *Controller) Init() (int, string) {
  12. status, msg := c.GetYDB()
  13. return status, msg
  14. }
  15. func (c *Controller) GetYDB() (int, string) {
  16. gtb := &struct {
  17. GateTokenBase
  18. }{}
  19. var gtbStr string
  20. var err error
  21. if XConfig["IsYDBFixed"] == "Yes" {
  22. data, err := ioutil.ReadFile("models/custom.yml")
  23. if err != nil {
  24. return 507, e.LogStr("ASDFQEWFA", "Can NOT Read custom.yml")
  25. }
  26. var config Config
  27. if err := yaml.Unmarshal(data, &config); err == nil {
  28. c.Gtb = config.Source
  29. c.Gtb.ConnString = config.Source.ConnStr // custom.yml의 Variable name 이 서로 달라서 복사해줌.
  30. } else {
  31. return 507, e.LogStr("ASDWEWFA", "connString in custom.yml format mismatch ")
  32. }
  33. } else {
  34. if gtbStr, err = MdbView(c.GateToken); err != nil {
  35. // if c.ConnString, err = MdbView(c.GateToken); err != nil {
  36. return 505, e.LogStr("QWFAECAFVD", "GateToken Not Found: "+c.GateToken)
  37. }
  38. if err := json.Unmarshal([]byte(gtbStr), gtb); err == nil {
  39. c.Gtb.ConnString = gtb.ConnString
  40. c.Gtb.RemoteIp = gtb.RemoteIp
  41. c.Gtb.DeviceDesc = gtb.DeviceDesc
  42. c.Gtb.FrontIp = gtb.FrontIp
  43. c.Gtb.FrontHost = gtb.FrontHost
  44. c.Gtb.Referer = gtb.Referer
  45. c.Gtb.SsoSubId = gtb.SsoSubId
  46. c.Gtb.UserId = gtb.UserId
  47. c.Gtb.UserPermId = gtb.UserPermId
  48. c.Gtb.MemberId = gtb.MemberId
  49. c.Gtb.MemberPermId = gtb.MemberPermId
  50. c.Gtb.SgroupId = gtb.SgroupId
  51. c.Gtb.BranchId = gtb.BranchId
  52. c.Gtb.StorageId = gtb.StorageId
  53. c.Gtb.AgroupId = gtb.AgroupId
  54. c.Gtb.MemberBuyerId = gtb.MemberBuyerId
  55. c.Gtb.MemberCompanyId = gtb.MemberCompanyId
  56. c.Gtb.CompanySort = gtb.CompanySort
  57. c.Gtb.SalesQtyPoint = gtb.SalesQtyPoint
  58. c.Gtb.SalesPrcPoint = gtb.SalesPrcPoint
  59. c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
  60. c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
  61. c.Gtb.PurchPrcPoint = gtb.PurchPrcPoint
  62. c.Gtb.PurchAmtPoint = gtb.PurchAmtPoint
  63. c.Gtb.StockQtyPoint = gtb.StockQtyPoint
  64. c.Gtb.StockPrcPoint = gtb.StockPrcPoint
  65. c.Gtb.StockAmtPoint = gtb.StockAmtPoint
  66. c.Gtb.AccAmtPoint = gtb.AccAmtPoint
  67. } else {
  68. return 505, e.LogStr("QWFAEC1AFVDS", "AfterBase64Content Format mismatch: "+c.GateToken)
  69. }
  70. }
  71. if c.Db, err = xorm.NewEngine(XConfig["DbType"], c.Gtb.ConnString); err != nil {
  72. return 600, e.LogStr("ADASEF", "DBEngine Open Error")
  73. }
  74. if status, msg := c.AttachDB(); status != 200 { // DB 까지 붙여야 memory error 가 안난다.
  75. return status, e.LogStr("PBUYJM-", msg)
  76. }
  77. return 200, ""
  78. }
  79. type Config struct {
  80. // GateTokenDev string `yaml:"gate_token_dev"`
  81. Source GateTokenBase
  82. }
  83. type Target struct {
  84. Type string `yaml:"type"`
  85. Language string `yaml:"language"`
  86. OutputDir string `yaml:"output_dir"`
  87. }
  88. func (c *Controller) CustomYmlToGateTokenBaseAndDbGet() (int, string) {
  89. data, err := ioutil.ReadFile("models/custom.yml")
  90. if err != nil {
  91. return 507, e.LogStr("ASDFQEWFA", "Can NOT Read custom.yml")
  92. }
  93. var config Config
  94. if err := yaml.Unmarshal(data, &config); err == nil {
  95. c.Gtb = config.Source
  96. c.Gtb.ConnString = config.Source.ConnStr // custom.yml의 Variable name 이 서로 달라서 복사해줌.
  97. } else {
  98. return 507, e.LogStr("ASDWEWFA", "connString in custom.yml format mismatch ")
  99. }
  100. if status, msg := c.AttachDB(); status != 200 { // DB 까지 붙여야 memory error 가 안난다.
  101. return status, e.LogStr("PBUYJM-", msg)
  102. }
  103. return 200, ""
  104. }
  105. func (c *Controller) AttachDB() (int, string) {
  106. var err error
  107. if c.Db, err = xorm.NewEngine(XConfig["DbType"], c.Gtb.ConnString); err != nil {
  108. return 600, e.LogStr("ADASEF", "DBEngine Open Error")
  109. }
  110. var connHint string
  111. strArr := strings.Split(c.Gtb.ConnString, "@tcp")
  112. if len(strArr) == 2 {
  113. connHint = strArr[1]
  114. } else {
  115. return 507, e.LogStr("ASDFQEWFA", "connString format mismatch: "+strArr[1])
  116. }
  117. c.Db.ShowSQL(false)
  118. c.Db.SetMaxOpenConns(100)
  119. c.Db.SetMaxIdleConns(20)
  120. c.Db.SetConnMaxLifetime(60 * time.Second)
  121. if _, err := c.Db.IsTableExist("aaa"); err == nil {
  122. return 200, e.LogStr("ASDFASFQFE", "YDB connection in "+connHint)
  123. } else {
  124. return 600, e.LogStr("PMUHIUYBUYJM-", "YDB connection Fail in "+connHint)
  125. }
  126. return 200, ""
  127. }
  128. // func (c *Controller) Init(ask AbangoAsk) {
  129. // c.ServerVars = make(map[string]string) // 반드시 할당해줘야 함.
  130. // c.Data = make(map[interface{}]interface{})
  131. // c.Ctx.Ask = ask
  132. // c.ConnString = XConfig["KafkaConnect"]
  133. // c.Ctx.ReturnTopic = c.Ctx.Ask.UniqueId
  134. // for _, p := range c.Ctx.Ask.ServerParams {
  135. // c.ServerVars[p.Key] = p.Value
  136. // }
  137. // c.GetAbangoAccessAndDb()
  138. // }
  139. // func (c *Controller) InitNormal() {
  140. // c.ServerVars = make(map[string]string) // 반드시 할당해줘야 함.
  141. // c.Data = make(map[interface{}]interface{})
  142. // //c.Ctx.ReturnTopic = "ljsldjfalsdfja" // 여기서 메모리 할당이 한됨'
  143. // c.GetAbangoAccessAndDb()
  144. // }
  145. // func (c *Controller) KafkaAnswer(body string) {
  146. // // c.Ctx.Answer.Body = []byte(body) // 쓸데없는 것 같은데 나중에 지
  147. // // e.Tp("ReturnTopic=" + c.Ctx.ReturnTopic)
  148. // if _, _, err := KafkaProducer(body,
  149. // c.Ctx.ReturnTopic, c.ConnString, XConfig["api_method"]); err != nil {
  150. // e.MyErr("WERRWEEWQRFDFHQW", err, false)
  151. // }
  152. // }
  153. func (c *Controller) AnswerJson() {
  154. // var ret []byte
  155. // if c.Data["json"] == nil {
  156. // msg := " QVCZEFAQWERQ " + c.Ctx.Ask.AskName + "[\"" + c.Ctx.Ask.ApiType + "\"] Data[json] is empty !"
  157. // e.MyErr(msg, errors.New(""), true)
  158. // return
  159. // }
  160. // if c.ServerVars["indent_answer"] == "yes" {
  161. // ret, _ = json.MarshalIndent(c.Data["json"], "", " ")
  162. // } else {
  163. // ret, _ = json.Marshal(c.Data["json"])
  164. // }
  165. // // e.Tp(string(ret))
  166. // if c.Ctx.Ask.ApiType == "Kafka" {
  167. // c.KafkaAnswer(string(ret))
  168. // } else if c.Ctx.Ask.ApiType == "gRpc" {
  169. // // 점진적으로 채워나가자.
  170. // } else if c.Ctx.Ask.ApiType == "Rest" {
  171. // // 점진적으로 채워나가자.
  172. // }
  173. }
  174. func (c *Controller) GetAbangoAccessAndDb() error {
  175. // if err := c.GetAccessAuth(); err == nil {
  176. // var err2 error
  177. // if c.Db, err2 = xorm.NewEngine(c.Access.DbType, c.Access.DbConnStr); err2 == nil {
  178. // // db, err := xorm.NewEngine(XEnc.DbType, "root:root@tcp(127.0.0.1:3306)/kangan?charset=utf8&parseTime=True")
  179. // strArr := strings.Split(c.Access.DbConnStr, "@tcp")
  180. // if len(strArr) == 2 {
  181. // e.OkLog(strArr[1])
  182. // } else {
  183. // e.MyErr(strArr[1], err2, true)
  184. // return err2
  185. // }
  186. // c.Db.ShowSQL(false)
  187. // c.Db.SetMaxOpenConns(100)
  188. // c.Db.SetMaxIdleConns(20)
  189. // c.Db.SetConnMaxLifetime(60 * time.Second)
  190. // if _, err := c.Db.IsTableExist("admin_menu"); err != nil {
  191. // e.MyErr("DB DISconnected in "+strArr[1], err, true)
  192. // } else {
  193. // e.OkLog("DB connect in " + strArr[1])
  194. // }
  195. // } else {
  196. // e.MyErr("xorm.NewEngine", err, true)
  197. // }
  198. // } else {
  199. // e.MyErr("GetAccessAuth", err, true)
  200. // }
  201. return nil
  202. }
  203. func (c *Controller) GetAccessAuth() error {
  204. return nil
  205. }