index.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package app
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "pbx-api-gin/internal/app/ami/action"
  6. "pbx-api-gin/internal/app/mysql"
  7. "pbx-api-gin/internal/app/stc"
  8. "pbx-api-gin/internal/app/stc/active"
  9. "pbx-api-gin/internal/app/stc/socket"
  10. "pbx-api-gin/pkg/lfshook"
  11. "pbx-api-gin/pkg/utils"
  12. "strings"
  13. "time"
  14. )
  15. func StartApp() {
  16. //init mysql
  17. mysql.CreateDBInstance()
  18. //Get cab number acording to IP
  19. IP, err := getIPByCommand()
  20. if err != nil {
  21. lfshook.NewLogger().Infof("Get IP err :%+v", err)
  22. }
  23. if IP[len(IP)-2:] == "81" {
  24. active.CabNum = "8"
  25. } else {
  26. active.CabNum = "1"
  27. }
  28. lfshook.NewLogger().Infof("=================cab number:%s===========", active.CabNum)
  29. // 启动带有重连机制的连接管理协程
  30. go stc.StartStcConnection(socket.Conn)
  31. // 启动其他服务...
  32. // 启动 AMI
  33. go func() {
  34. action.StartAMI(func() {
  35. lfshook.NewLogger().Info("ami callback")
  36. }, []func(event map[string]string){})
  37. }()
  38. //refresh extension status
  39. time.Sleep(3 * time.Second)
  40. utils.ExecCmdAsync("/usr/sbin/asterisk", "-rx", "reload")
  41. }
  42. // Get eth0 IP
  43. func getIPByCommand() (string, error) {
  44. cmd := "ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | cut -d: -f2"
  45. out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
  46. if err != nil {
  47. return "", err
  48. }
  49. ip := strings.TrimSpace(string(out))
  50. if ip == "" {
  51. return "", fmt.Errorf("no IP address found")
  52. }
  53. return ip, nil
  54. }