controller.go 9.5 KB

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