lancher-server.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package comm
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/nats-io/nats.go"
  12. "github.com/spf13/viper"
  13. )
  14. const (
  15. APPSTATE_RUNNING = "运行中"
  16. APPSTATE_INSTALLED = "已安装"
  17. )
  18. type App struct {
  19. Id string
  20. Name string
  21. Label string
  22. Desc string
  23. Icon string
  24. IconUrl string
  25. Version string
  26. Type string
  27. ExeFpath string
  28. Pwd string
  29. Args string
  30. RegTime time.Time
  31. LastRuningTime time.Time
  32. Status string //运行中
  33. }
  34. func GetAppId() (string, error) {
  35. f, e := os.Executable()
  36. if e != nil {
  37. return "", e
  38. }
  39. _, exeName := filepath.Split(f)
  40. if strings.Contains(f, "go-build") {
  41. fmt.Println()
  42. pwd, e := os.Getwd()
  43. if e != nil {
  44. return "", e
  45. }
  46. f = path.Join(pwd, exeName)
  47. }
  48. fmt.Println("app path=>", f)
  49. md := md5.Sum([]byte(f))
  50. return fmt.Sprintf("%x", md), nil
  51. }
  52. func GetAppPathId(fpath string) string {
  53. md := md5.Sum([]byte(fpath))
  54. return fmt.Sprintf("%x", md)
  55. }
  56. func ParseConfigFile(filepath string) (*App, error) {
  57. file, err := os.Open(filepath)
  58. if err != nil {
  59. return nil, err
  60. }
  61. v := viper.New()
  62. v.SetConfigType("yaml")
  63. err = v.ReadConfig(file)
  64. if err != nil {
  65. return nil, err
  66. }
  67. c := new(App)
  68. err = v.Unmarshal(c)
  69. return c, err
  70. }
  71. var _callCancel = func() {}
  72. func CancelServeRun() {
  73. _callCancel()
  74. }
  75. func ServerRun(bus *NatsBus) error {
  76. cancel := WatchChange(bus)
  77. if cancel == nil {
  78. return fmt.Errorf("启动App监听失败")
  79. }
  80. _callCancel = cancel
  81. bus.Subscribe("lancher.apps.list", func(msg *nats.Msg) {
  82. out := []*App{}
  83. nc := bus.GetNatsConn()
  84. js, e := nc.JetStream()
  85. if e == nil {
  86. kv, e := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "lancher-apps"})
  87. if e == nil {
  88. out = getApps(kv)
  89. }
  90. }
  91. payload, _ := json.Marshal(out)
  92. msg.Respond(payload)
  93. })
  94. //启动app
  95. bus.Subscribe("lancher.start.*", func(msg *nats.Msg) {
  96. //获取AppId
  97. fmt.Println("starting=>", msg.Subject)
  98. })
  99. return nil
  100. }
  101. func WatchChange(bus *NatsBus) func() {
  102. nc := bus.GetNatsConn()
  103. js, e := nc.JetStream()
  104. if e != nil {
  105. fmt.Println(e)
  106. return nil
  107. }
  108. if e != nil {
  109. fmt.Println(e)
  110. return nil
  111. }
  112. kv, e := js.CreateKeyValue(&nats.KeyValueConfig{Bucket: "lancher-apps"})
  113. if e != nil {
  114. return nil
  115. }
  116. watch, e := kv.WatchAll()
  117. if e != nil {
  118. return nil
  119. }
  120. quiteCh := make(chan int, 1)
  121. go func() {
  122. for {
  123. conti := true
  124. select {
  125. case <-quiteCh:
  126. conti = false
  127. default:
  128. v := <-watch.Updates()
  129. if v != nil {
  130. fmt.Println("wathing update=>", v.Key(), v.Bucket(), v.Operation())
  131. }
  132. bus.PublishObj("lancher.apps.updated", getApps(kv))
  133. time.Sleep(time.Second)
  134. }
  135. if !conti {
  136. break
  137. }
  138. }
  139. fmt.Println("waching app quited")
  140. }()
  141. return func() {
  142. quiteCh <- 1
  143. }
  144. }
  145. func getApps(kv nats.KeyValue) []*App {
  146. out := []*App{}
  147. keys, e := kv.Keys()
  148. if e != nil {
  149. fmt.Println("keys error=>", e)
  150. return out
  151. }
  152. for _, key := range keys {
  153. v, _ := kv.Get(key)
  154. if v != nil {
  155. app := &App{}
  156. e := json.Unmarshal(v.Value(), app)
  157. if e == nil {
  158. fmt.Println("app=>", *app)
  159. out = append(out, app)
  160. } else {
  161. fmt.Println("Unmarshal errr=>", e)
  162. }
  163. }
  164. }
  165. return out
  166. }