config2.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "time"
  8. "go-common/app/infra/config/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. )
  13. // versions client versions which the configuration is complete
  14. func versions2(c *bm.Context) {
  15. var (
  16. err error
  17. svr string
  18. data *model.Versions
  19. bver string
  20. token string
  21. env string
  22. zone string
  23. )
  24. query := c.Request.URL.Query()
  25. // params
  26. svr = query.Get("service")
  27. if svr == "" {
  28. token = query.Get("token")
  29. zone = query.Get("zone")
  30. env = query.Get("env")
  31. if zone == "" || env == "" || token == "" {
  32. c.JSON(nil, ecode.RequestErr)
  33. return
  34. }
  35. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  36. c.JSON(nil, ecode.RequestErr)
  37. return
  38. }
  39. }
  40. if bver = query.Get("build"); bver == "" {
  41. c.JSON(nil, ecode.RequestErr)
  42. return
  43. }
  44. if data, err = confSvc2.VersionSuccess(c, svr, bver); err != nil {
  45. c.JSON(nil, err)
  46. return
  47. }
  48. c.JSON(data, nil)
  49. }
  50. // config get config file
  51. func config2(c *bm.Context) {
  52. var (
  53. err error
  54. svr string
  55. buildVer string
  56. version int64
  57. token string
  58. ids []int64
  59. zone string
  60. env string
  61. )
  62. query := c.Request.URL.Query()
  63. // params
  64. if token = query.Get("token"); token == "" {
  65. c.JSON(nil, ecode.RequestErr)
  66. return
  67. }
  68. svr = query.Get("service")
  69. if svr == "" {
  70. zone = query.Get("zone")
  71. env = query.Get("env")
  72. if zone == "" || env == "" || token == "" {
  73. c.JSON(nil, ecode.RequestErr)
  74. return
  75. }
  76. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  77. c.JSON(nil, ecode.RequestErr)
  78. return
  79. }
  80. }
  81. if buildVer = query.Get("build"); buildVer == "" {
  82. c.JSON(nil, ecode.RequestErr)
  83. return
  84. }
  85. if version, err = strconv.ParseInt(query.Get("version"), 10, 64); err != nil {
  86. c.JSON(nil, ecode.RequestErr)
  87. return
  88. }
  89. if idsStr := query.Get("ids"); len(idsStr) != 0 {
  90. if err = json.Unmarshal([]byte(query.Get("ids")), &ids); err != nil {
  91. c.JSON(nil, ecode.RequestErr)
  92. return
  93. }
  94. }
  95. data, err := confSvc2.Config(c, svr, token, version, ids)
  96. if err != nil {
  97. c.JSON(nil, err)
  98. return
  99. }
  100. c.JSON(data, nil)
  101. }
  102. // file get one file value
  103. func file2(c *bm.Context) {
  104. var (
  105. err error
  106. svr string
  107. buildVer string
  108. token string
  109. file string
  110. ver int64
  111. treeID string
  112. env string
  113. zone string
  114. data string
  115. )
  116. query := c.Request.URL.Query()
  117. // params
  118. if verStr := query.Get("version"); verStr == "" {
  119. ver = model.UnknownVersion
  120. } else {
  121. if ver, err = strconv.ParseInt(verStr, 10, 64); err != nil {
  122. data = "version must be num"
  123. }
  124. }
  125. if env = query.Get("env"); env == "" {
  126. data = "env is null"
  127. }
  128. if zone = query.Get("zone"); zone == "" {
  129. data = "zone is null"
  130. }
  131. if token = query.Get("token"); token == "" {
  132. data = "token is null"
  133. }
  134. if treeID = query.Get("treeid"); treeID == "" {
  135. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  136. data = "appid is null"
  137. }
  138. } else {
  139. svr = fmt.Sprintf("%s_%s_%s", treeID, env, zone)
  140. }
  141. if buildVer = query.Get("build"); buildVer == "" {
  142. data = "build is null"
  143. }
  144. if file = query.Get("fileName"); file == "" {
  145. data = "fileName is null"
  146. }
  147. service := &model.Service{Name: svr, BuildVersion: buildVer, File: file, Token: token, Version: ver}
  148. if data == "" {
  149. if data, err = confSvc2.File(c, service); err != nil {
  150. data = err.Error()
  151. c.AbortWithStatus(http.StatusInternalServerError)
  152. }
  153. } else {
  154. c.AbortWithStatus(http.StatusBadRequest)
  155. }
  156. if _, err = c.Writer.Write([]byte(data)); err != nil {
  157. log.Error("Response().Write(%v) error(%v)", data, err)
  158. }
  159. }
  160. // check check config version
  161. func check2(c *bm.Context) {
  162. var (
  163. err error
  164. svr string
  165. host string
  166. buildVer string
  167. ip string
  168. ver int64
  169. token string
  170. appoint int64
  171. zone string
  172. env string
  173. query = c.Request.URL.Query()
  174. )
  175. // params
  176. if token = query.Get("token"); token == "" {
  177. c.JSON(nil, ecode.RequestErr)
  178. return
  179. }
  180. svr = query.Get("service")
  181. if svr == "" {
  182. zone = query.Get("zone")
  183. env = query.Get("env")
  184. if zone == "" || env == "" || token == "" {
  185. c.JSON(nil, ecode.RequestErr)
  186. return
  187. }
  188. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  189. c.JSON(nil, ecode.RequestErr)
  190. return
  191. }
  192. }
  193. if ip = query.Get("ip"); ip == "" {
  194. c.JSON(nil, ecode.RequestErr)
  195. return
  196. }
  197. if host = query.Get("hostname"); host == "" {
  198. c.JSON(nil, ecode.RequestErr)
  199. return
  200. }
  201. if buildVer = query.Get("build"); buildVer == "" {
  202. c.JSON(nil, ecode.RequestErr)
  203. return
  204. }
  205. if ver, err = strconv.ParseInt(query.Get("version"), 10, 64); err != nil {
  206. c.JSON(nil, ecode.RequestErr)
  207. return
  208. }
  209. appoint, _ = strconv.ParseInt(query.Get("appoint"), 10, 64)
  210. // check config version
  211. rhost := &model.Host{Service: svr, Name: host, BuildVersion: buildVer, IP: ip, ConfigVersion: ver, Appoint: appoint, Customize: query.Get("customize")}
  212. evt, err := confSvc2.CheckVersion(c, rhost, token)
  213. if err != nil {
  214. c.JSON(nil, err)
  215. return
  216. }
  217. // wait for version change
  218. select {
  219. case e := <-evt:
  220. c.JSON(e, nil)
  221. case <-time.After(time.Duration(cnf.PollTimeout)):
  222. c.JSON(nil, ecode.NotModified)
  223. case <-c.Writer.(http.CloseNotifier).CloseNotify():
  224. c.JSON(nil, ecode.NotModified)
  225. }
  226. confSvc2.Unsub(svr, host)
  227. }
  228. //clear host in redis
  229. func clearhost2(c *bm.Context) {
  230. var (
  231. svr string
  232. zone string
  233. env string
  234. token string
  235. err error
  236. )
  237. query := c.Request.Form
  238. svr = query.Get("service")
  239. if svr == "" {
  240. token = query.Get("token")
  241. zone = query.Get("zone")
  242. env = query.Get("env")
  243. if zone == "" || env == "" || token == "" {
  244. c.JSON(nil, ecode.RequestErr)
  245. return
  246. }
  247. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  248. c.JSON(nil, ecode.RequestErr)
  249. return
  250. }
  251. }
  252. c.JSON(nil, confSvc2.ClearHost(c, svr))
  253. }
  254. // versions client versions which the configuration is complete
  255. func builds2(c *bm.Context) {
  256. var (
  257. err error
  258. svr string
  259. data []string
  260. token string
  261. env string
  262. zone string
  263. )
  264. query := c.Request.URL.Query()
  265. // params
  266. svr = query.Get("service")
  267. if svr == "" {
  268. token = query.Get("token")
  269. zone = query.Get("zone")
  270. env = query.Get("env")
  271. if zone == "" || env == "" || token == "" {
  272. c.JSON(nil, ecode.RequestErr)
  273. return
  274. }
  275. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  276. c.JSON(nil, ecode.RequestErr)
  277. return
  278. }
  279. }
  280. if data, err = confSvc2.Builds(c, svr); err != nil {
  281. c.JSON(nil, err)
  282. return
  283. }
  284. c.JSON(data, nil)
  285. }
  286. // config get config file
  287. func latest(c *bm.Context) {
  288. var (
  289. err error
  290. svr string
  291. buildVer string
  292. version int64
  293. token string
  294. ids []int64
  295. zone string
  296. env string
  297. verStr string
  298. )
  299. query := c.Request.URL.Query()
  300. // params
  301. if token = query.Get("token"); token == "" {
  302. c.JSON(nil, ecode.RequestErr)
  303. return
  304. }
  305. svr = query.Get("service")
  306. if svr == "" {
  307. zone = query.Get("zone")
  308. env = query.Get("env")
  309. if zone == "" || env == "" || token == "" {
  310. c.JSON(nil, ecode.RequestErr)
  311. return
  312. }
  313. if svr, err = confSvc2.AppService(zone, env, token); err != nil {
  314. c.JSON(nil, ecode.RequestErr)
  315. return
  316. }
  317. }
  318. if buildVer = query.Get("build"); buildVer == "" {
  319. c.JSON(nil, ecode.RequestErr)
  320. return
  321. }
  322. verStr = query.Get("version")
  323. if len(verStr) > 0 {
  324. if version, err = strconv.ParseInt(verStr, 10, 64); err != nil {
  325. c.JSON(nil, ecode.RequestErr)
  326. return
  327. }
  328. } else {
  329. rhost := &model.Host{Service: svr, BuildVersion: buildVer}
  330. version, err = confSvc2.CheckLatest(c, rhost, token)
  331. if err != nil {
  332. c.JSON(nil, err)
  333. return
  334. }
  335. }
  336. data, err := confSvc2.ConfigCheck(c, svr, token, version, ids)
  337. if err != nil {
  338. c.JSON(nil, err)
  339. return
  340. }
  341. c.JSON(data, nil)
  342. }