main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "log"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "53it.net/zql" // http://git.53it.net/zuoxiupeng/zql or https://github.com/shiguanghuxian/zql
  10. "gopkg.in/mgo.v2"
  11. )
  12. // Data Ajax相应数据根结构
  13. type Data struct {
  14. State string `json:"state"`
  15. Msg string `json:"msg"`
  16. Data interface{} `json:"data"`
  17. }
  18. func main() {
  19. // 系统日志显示文件和行号
  20. log.SetFlags(log.Lshortfile | log.LstdFlags)
  21. rootDir := flag.String("d", "./", "The root directory of the website")
  22. port := flag.String("p", "80", "Web port")
  23. ipAddress := flag.String("a", "0.0.0.0", "Site IP address")
  24. help := flag.Bool("h", false, "Use the help")
  25. flag.Parse()
  26. // 帮助
  27. if *help {
  28. flag.Usage()
  29. os.Exit(1)
  30. }
  31. // 静态路径处理
  32. staticPath := GetRootDir() + "/view/" // 静态文件路径
  33. if *rootDir != "" {
  34. staticPath = *rootDir
  35. }
  36. // 启动http服务
  37. StartHTTP(*ipAddress, *port, staticPath)
  38. }
  39. // StartHTTP 启动http服务
  40. func StartHTTP(ipAddr, port, staticPath string) {
  41. // 防止空参数
  42. if ipAddr == "" {
  43. ipAddr = "0.0.0.0"
  44. }
  45. if port == "" {
  46. port = "80"
  47. }
  48. if staticPath == "" {
  49. staticPath = "./"
  50. }
  51. // 端口监听ip和端口
  52. listenAddress := ipAddr + ":" + port // 启动ip和端口
  53. // 静态文件服务器
  54. http.Handle("/", http.FileServer(http.Dir(staticPath)))
  55. // ajax请求数据表数据
  56. http.HandleFunc("/convert", AjaxConvert)
  57. // 启动http服务
  58. log.Println("正在启动http服务:", listenAddress)
  59. err := http.ListenAndServe(listenAddress, nil)
  60. if err != nil {
  61. log.Println("http服务启动失败:", err)
  62. os.Exit(1)
  63. }
  64. log.Println("启动http服成功")
  65. }
  66. // AjaxConvert 处理ajax请求
  67. func AjaxConvert(w http.ResponseWriter, r *http.Request) {
  68. // 接收参数
  69. valS := r.URL.Query()
  70. cType := valS.Get("type")
  71. zqlStr := valS.Get("zql")
  72. // 最终发送的数据包
  73. data := &Data{State: "1", Msg: "服务端错误"}
  74. // 判断是否未传参数
  75. if cType == "" {
  76. data.Msg = "参数'type'不能为空"
  77. } else if zqlStr == "" {
  78. data.Msg = "参数'zql'不能为空"
  79. } else {
  80. // 表前缀
  81. prefix := valS.Get("prefix")
  82. // 创建zql对象
  83. zqlObj, err := zql.New(prefix, zqlStr)
  84. if err != nil {
  85. data.Msg = err.Error()
  86. } else {
  87. data.State = "0" // 返回状态置为0
  88. data.Msg = "zql转换成功"
  89. switch cType {
  90. case "mongodb":
  91. mgoDb := new(mgo.Session).DB("dbname")
  92. mgoStr, err := zqlObj.GetMongoQueryStr(mgoDb, "")
  93. if err != nil {
  94. data.State = "1"
  95. data.Msg = err.Error()
  96. } else {
  97. data.Data = mgoStr
  98. }
  99. break
  100. case "influxdb":
  101. infStr, err := zqlObj.GetInfluxdbQuery("")
  102. if err != nil {
  103. data.State = "1"
  104. data.Msg = err.Error()
  105. } else {
  106. data.Data = infStr
  107. }
  108. break
  109. case "elasticsearch":
  110. esStr, err := zqlObj.GetElasticQueryStr()
  111. if err != nil {
  112. data.State = "1"
  113. data.Msg = err.Error()
  114. } else {
  115. data.Data = esStr
  116. }
  117. break
  118. default:
  119. data.State = "1"
  120. data.Msg = "参数'type'无法识别,请检查是否错误"
  121. break
  122. }
  123. }
  124. }
  125. // 转json
  126. wdata, err := json.Marshal(data)
  127. if err != nil {
  128. log.Println("ajax输出转json失败:" + err.Error())
  129. }
  130. // 发送数据
  131. w.Write(wdata)
  132. }
  133. // GetRootDir 获取程序跟目录
  134. func GetRootDir() string {
  135. // 获取当前根目录
  136. file, err := filepath.Abs(filepath.Dir(os.Args[0]))
  137. if err != nil {
  138. return "."
  139. }
  140. return file
  141. }