123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package abango
- import (
- "encoding/json"
- "io/ioutil"
- "strings"
- "time"
- e "github.com/dabory/abango-rest/etc"
- "github.com/go-xorm/xorm"
- "gopkg.in/yaml.v2"
- )
- func (c *Controller) Init() (int, string) {
- // status, msg := c.GetYDB()
- // return status, msg
- var gtb GateTokenBase
- // gtb := &struct {
- // GateTokenBase
- // }{}
- var gtbStr string
- var err error
- if XConfig["IsYDBFixed"] == "Yes" {
- data, err := ioutil.ReadFile("models/custom.yml")
- if err != nil {
- return 507, e.LogStr("ASDFQEWFA", "Can NOT Read custom.yml")
- }
- var config Config
- if err := yaml.Unmarshal(data, &config); err == nil {
- c.Gtb = config.Source
- c.Gtb.ConnString = config.Source.ConnStr // custom.yml의 Variable name 이 서로 달라서 복사해줌.
- } else {
- return 507, e.LogStr("ASDWEWFA", "connString in custom.yml format mismatch ")
- }
- } else {
- if c.GateToken == "" {
- return 505, e.LogStr("QWCAFVD", "GateToken is Empty: ")
- }
- if gtbStr, err = MdbView(c.GateToken); err != nil {
- return 505, e.LogStr("QWFAECD", "GateToken Not Found in MemoryDB: "+c.GateToken)
- }
- if err := json.Unmarshal([]byte(gtbStr), >b); err == nil {
- c.Gtb = gtb
- // c.Gtb.ConnString = gtb.ConnString
- // c.Gtb.RemoteIp = gtb.RemoteIp
- // c.Gtb.DeviceDesc = gtb.DeviceDesc
- // c.Gtb.FrontIp = gtb.FrontIp
- // c.Gtb.FrontHost = gtb.FrontHost
- // c.Gtb.Referer = gtb.Referer
- // c.Gtb.SsoSubId = gtb.SsoSubId
- // c.Gtb.UserId = gtb.UserId
- // c.Gtb.UserPermId = gtb.UserPermId
- // c.Gtb.MemberId = gtb.MemberId
- // c.Gtb.MemberPermId = gtb.MemberPermId
- // c.Gtb.SgroupId = gtb.SgroupId
- // c.Gtb.BranchId = gtb.BranchId
- // c.Gtb.StorageId = gtb.StorageId
- // c.Gtb.AgroupId = gtb.AgroupId
- // c.Gtb.MemberBuyerId = gtb.MemberBuyerId
- // c.Gtb.MemberCompanyId = gtb.MemberCompanyId
- // c.Gtb.CompanySort = gtb.CompanySort
- // c.Gtb.SalesQtyPoint = gtb.SalesQtyPoint
- // c.Gtb.SalesPrcPoint = gtb.SalesPrcPoint
- // c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
- // c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
- // c.Gtb.PurchPrcPoint = gtb.PurchPrcPoint
- // c.Gtb.PurchAmtPoint = gtb.PurchAmtPoint
- // c.Gtb.StockQtyPoint = gtb.StockQtyPoint
- // c.Gtb.StockPrcPoint = gtb.StockPrcPoint
- // c.Gtb.StockAmtPoint = gtb.StockAmtPoint
- // c.Gtb.AccAmtPoint = gtb.AccAmtPoint
- } else {
- return 505, e.LogStr("QWFAEC1AFVDS", "AfterBase64Content Format mismatch: "+c.GateToken)
- }
- }
- if c.Db, err = xorm.NewEngine(XConfig["DbType"], c.Gtb.ConnString); err != nil {
- return 600, e.LogStr("ADASEF", "DBEngine Open Error")
- }
- if status, msg := c.AttachDB(); status != 200 { // DB 까지 붙여야 memory error 가 안난다.
- return status, e.LogStr("PBUYJM-", msg)
- }
- return 200, ""
- }
- // func (c *Controller) GetYDB() (int, string) {
- // gtb := &struct {
- // GateTokenBase
- // }{}
- // var gtbStr string
- // var err error
- // if XConfig["IsYDBFixed"] == "Yes" {
- // data, err := ioutil.ReadFile("models/custom.yml")
- // if err != nil {
- // return 507, e.LogStr("ASDFQEWFA", "Can NOT Read custom.yml")
- // }
- // var config Config
- // if err := yaml.Unmarshal(data, &config); err == nil {
- // c.Gtb = config.Source
- // c.Gtb.ConnString = config.Source.ConnStr // custom.yml의 Variable name 이 서로 달라서 복사해줌.
- // } else {
- // return 507, e.LogStr("ASDWEWFA", "connString in custom.yml format mismatch ")
- // }
- // } else {
- // if gtbStr, err = MdbView(c.GateToken); err != nil {
- // // if c.ConnString, err = MdbView(c.GateToken); err != nil {
- // return 505, e.LogStr("QWFAECAFVD", "GateToken Not Found: "+c.GateToken)
- // }
- // if err := json.Unmarshal([]byte(gtbStr), gtb); err == nil {
- // c.Gtb.ConnString = gtb.ConnString
- // c.Gtb.RemoteIp = gtb.RemoteIp
- // c.Gtb.DeviceDesc = gtb.DeviceDesc
- // c.Gtb.FrontIp = gtb.FrontIp
- // c.Gtb.FrontHost = gtb.FrontHost
- // c.Gtb.Referer = gtb.Referer
- // c.Gtb.SsoSubId = gtb.SsoSubId
- // c.Gtb.UserId = gtb.UserId
- // c.Gtb.UserPermId = gtb.UserPermId
- // c.Gtb.MemberId = gtb.MemberId
- // c.Gtb.MemberPermId = gtb.MemberPermId
- // c.Gtb.SgroupId = gtb.SgroupId
- // c.Gtb.BranchId = gtb.BranchId
- // c.Gtb.StorageId = gtb.StorageId
- // c.Gtb.AgroupId = gtb.AgroupId
- // c.Gtb.MemberBuyerId = gtb.MemberBuyerId
- // c.Gtb.MemberCompanyId = gtb.MemberCompanyId
- // c.Gtb.CompanySort = gtb.CompanySort
- // c.Gtb.SalesQtyPoint = gtb.SalesQtyPoint
- // c.Gtb.SalesPrcPoint = gtb.SalesPrcPoint
- // c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
- // c.Gtb.SalesAmtPoint = gtb.SalesAmtPoint
- // c.Gtb.PurchPrcPoint = gtb.PurchPrcPoint
- // c.Gtb.PurchAmtPoint = gtb.PurchAmtPoint
- // c.Gtb.StockQtyPoint = gtb.StockQtyPoint
- // c.Gtb.StockPrcPoint = gtb.StockPrcPoint
- // c.Gtb.StockAmtPoint = gtb.StockAmtPoint
- // c.Gtb.AccAmtPoint = gtb.AccAmtPoint
- // } else {
- // return 505, e.LogStr("QWFAEC1AFVDS", "AfterBase64Content Format mismatch: "+c.GateToken)
- // }
- // }
- // if c.Db, err = xorm.NewEngine(XConfig["DbType"], c.Gtb.ConnString); err != nil {
- // return 600, e.LogStr("ADASEF", "DBEngine Open Error")
- // }
- // if status, msg := c.AttachDB(); status != 200 { // DB 까지 붙여야 memory error 가 안난다.
- // return status, e.LogStr("PBUYJM-", msg)
- // }
- // return 200, ""
- // }
- type Config struct {
- // GateTokenDev string `yaml:"gate_token_dev"`
- Source GateTokenBase
- }
- type Target struct {
- Type string `yaml:"type"`
- Language string `yaml:"language"`
- OutputDir string `yaml:"output_dir"`
- }
- // func (c *Controller) CustomYmlToGateTokenBaseAndDbGet() (int, string) {
- // data, err := ioutil.ReadFile("models/custom.yml")
- // if err != nil {
- // return 507, e.LogStr("ASDFQEWFA", "Can NOT Read custom.yml")
- // }
- // var config Config
- // if err := yaml.Unmarshal(data, &config); err == nil {
- // c.Gtb = config.Source
- // c.Gtb.ConnString = config.Source.ConnStr // custom.yml의 Variable name 이 서로 달라서 복사해줌.
- // } else {
- // return 507, e.LogStr("ASDWEWFA", "connString in custom.yml format mismatch ")
- // }
- // if status, msg := c.AttachDB(); status != 200 { // DB 까지 붙여야 memory error 가 안난다.
- // return status, e.LogStr("PBUYJM-", msg)
- // }
- // return 200, ""
- // }
- func (c *Controller) AttachDB() (int, string) {
- var err error
- if c.Db, err = xorm.NewEngine(XConfig["DbType"], c.Gtb.ConnString); err != nil {
- return 600, e.LogStr("ADASEF", "DBEngine Open Error")
- }
- var connHint string
- strArr := strings.Split(c.Gtb.ConnString, "@tcp")
- if len(strArr) == 2 {
- connHint = strArr[1]
- } else {
- return 507, e.LogStr("ASDFQEWFA", "connString format mismatch: "+strArr[1])
- }
- c.Db.ShowSQL(false)
- c.Db.SetMaxOpenConns(100)
- c.Db.SetMaxIdleConns(20)
- c.Db.SetConnMaxLifetime(60 * time.Second)
- if _, err := c.Db.IsTableExist("aaa"); err == nil {
- return 200, e.LogStr("ASDFASFQFE", "YDB connection in "+connHint)
- } else {
- return 600, e.LogStr("PMUHIUYBUYJM-", "YDB connection Fail in "+connHint)
- }
- return 200, ""
- }
- // func (c *Controller) Init(ask AbangoAsk) {
- // c.ServerVars = make(map[string]string) // 반드시 할당해줘야 함.
- // c.Data = make(map[interface{}]interface{})
- // c.Ctx.Ask = ask
- // c.ConnString = XConfig["KafkaConnect"]
- // c.Ctx.ReturnTopic = c.Ctx.Ask.UniqueId
- // for _, p := range c.Ctx.Ask.ServerParams {
- // c.ServerVars[p.Key] = p.Value
- // }
- // c.GetAbangoAccessAndDb()
- // }
- // func (c *Controller) InitNormal() {
- // c.ServerVars = make(map[string]string) // 반드시 할당해줘야 함.
- // c.Data = make(map[interface{}]interface{})
- // //c.Ctx.ReturnTopic = "ljsldjfalsdfja" // 여기서 메모리 할당이 한됨'
- // c.GetAbangoAccessAndDb()
- // }
- // func (c *Controller) KafkaAnswer(body string) {
- // // c.Ctx.Answer.Body = []byte(body) // 쓸데없는 것 같은데 나중에 지
- // // e.Tp("ReturnTopic=" + c.Ctx.ReturnTopic)
- // if _, _, err := KafkaProducer(body,
- // c.Ctx.ReturnTopic, c.ConnString, XConfig["api_method"]); err != nil {
- // e.MyErr("WERRWEEWQRFDFHQW", err, false)
- // }
- // }
- func (c *Controller) AnswerJson() {
- // var ret []byte
- // if c.Data["json"] == nil {
- // msg := " QVCZEFAQWERQ " + c.Ctx.Ask.AskName + "[\"" + c.Ctx.Ask.ApiType + "\"] Data[json] is empty !"
- // e.MyErr(msg, errors.New(""), true)
- // return
- // }
- // if c.ServerVars["indent_answer"] == "yes" {
- // ret, _ = json.MarshalIndent(c.Data["json"], "", " ")
- // } else {
- // ret, _ = json.Marshal(c.Data["json"])
- // }
- // // e.Tp(string(ret))
- // if c.Ctx.Ask.ApiType == "Kafka" {
- // c.KafkaAnswer(string(ret))
- // } else if c.Ctx.Ask.ApiType == "gRpc" {
- // // 점진적으로 채워나가자.
- // } else if c.Ctx.Ask.ApiType == "Rest" {
- // // 점진적으로 채워나가자.
- // }
- }
- func (c *Controller) GetAbangoAccessAndDb() error {
- // if err := c.GetAccessAuth(); err == nil {
- // var err2 error
- // if c.Db, err2 = xorm.NewEngine(c.Access.DbType, c.Access.DbConnStr); err2 == nil {
- // // db, err := xorm.NewEngine(XEnc.DbType, "root:root@tcp(127.0.0.1:3306)/kangan?charset=utf8&parseTime=True")
- // strArr := strings.Split(c.Access.DbConnStr, "@tcp")
- // if len(strArr) == 2 {
- // e.OkLog(strArr[1])
- // } else {
- // e.MyErr(strArr[1], err2, true)
- // return err2
- // }
- // c.Db.ShowSQL(false)
- // c.Db.SetMaxOpenConns(100)
- // c.Db.SetMaxIdleConns(20)
- // c.Db.SetConnMaxLifetime(60 * time.Second)
- // if _, err := c.Db.IsTableExist("admin_menu"); err != nil {
- // e.MyErr("DB DISconnected in "+strArr[1], err, true)
- // } else {
- // e.OkLog("DB connect in " + strArr[1])
- // }
- // } else {
- // e.MyErr("xorm.NewEngine", err, true)
- // }
- // } else {
- // e.MyErr("GetAccessAuth", err, true)
- // }
- return nil
- }
- func (c *Controller) GetAccessAuth() error {
- return nil
- }
|