api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package api
  2. import (
  3. "dhcp/api/request"
  4. "dhcp/global"
  5. "dhcp/internal/ip"
  6. "dhcp/internal/service"
  7. "dhcp/model"
  8. "dhcp/result"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func DHCPServerRegisterRoute(r *gin.Engine) {
  12. dhcpServer := r.Group("/dhcpServer")
  13. dhcpServer.POST("/start", DHCPServerStart)
  14. dhcpServer.POST("/stop", DHCPServerStop)
  15. dhcpServer.GET("/info", DHCPServerInfo)
  16. dhcpServer.GET("/localip", GetLocalIPAndInterfaces)
  17. dhcpServer.POST("/delete", DHCPServerDelete)
  18. dhcpInfo := r.Group("/dhcpInfo")
  19. dhcpInfo.POST("/add", AddDHCPInfo)
  20. dhcpInfo.GET("/get", GetDHCPInfo)
  21. dhcpInfo.GET("/delete", DeleteDHCPInfo)
  22. dhcpInfo.POST("/update", UpdateDHCPInfo)
  23. dhcpInfo.GET("/getByMac", GetDHCPInfoByMac)
  24. }
  25. func DHCPServerStart(c *gin.Context) {
  26. var req request.DHCPService
  27. if err := c.ShouldBindJSON(&req); err != nil {
  28. c.JSON(200, result.Result{
  29. Code: 400,
  30. Data: nil,
  31. Msg: "参数错误",
  32. })
  33. return
  34. }
  35. re := service.StartDHCPService(c.Request.Context(), global.Log, req)
  36. c.JSON(200, re)
  37. }
  38. func DHCPServerDelete(c *gin.Context) {
  39. var req request.DHCPService
  40. if err := c.ShouldBindJSON(&req); err != nil {
  41. c.JSON(200, result.Result{
  42. Code: 400,
  43. Data: nil,
  44. Msg: "参数错误",
  45. })
  46. return
  47. }
  48. re := service.DeleteDHCPService(c.Request.Context(), global.Log, req)
  49. c.JSON(200, re)
  50. }
  51. func DHCPServerStop(c *gin.Context) {
  52. var req request.DHCPService
  53. if err := c.ShouldBindJSON(&req); err != nil {
  54. c.JSON(200, result.Result{
  55. Code: 400,
  56. Data: nil,
  57. Msg: "参数错误",
  58. })
  59. return
  60. }
  61. re := service.StopDHCPService(c.Request.Context(), global.Log, req)
  62. c.JSON(200, re)
  63. }
  64. func DHCPServerInfo(c *gin.Context) {
  65. var d []request.DHCPService
  66. for _, v := range service.DHCPServiceInfoMap {
  67. d = append(d, v)
  68. }
  69. c.JSON(200, result.Result{
  70. Code: 200,
  71. Data: d,
  72. Msg: "获取当前服务成功",
  73. })
  74. }
  75. func GetLocalIPAndInterfaces(c *gin.Context) {
  76. re := ip.GetLocalIPAndInterface()
  77. c.JSON(200, re)
  78. }
  79. func AddDHCPInfo(c *gin.Context) {
  80. var req model.DHCP
  81. if err := c.ShouldBindJSON(&req); err != nil {
  82. c.JSON(200, result.Result{
  83. Code: 400,
  84. Data: nil,
  85. Msg: "参数错误",
  86. })
  87. return
  88. }
  89. re := service.AddDHCPInfo(req)
  90. c.JSON(200, re)
  91. }
  92. func GetDHCPInfo(c *gin.Context) {
  93. re := service.GetDHCPInfo()
  94. c.JSON(200, re)
  95. }
  96. func DeleteDHCPInfo(c *gin.Context) {
  97. req := c.Query("mac")
  98. re := service.DeleteDHCPInfo(req)
  99. c.JSON(200, re)
  100. }
  101. func UpdateDHCPInfo(c *gin.Context) {
  102. var req model.DHCP
  103. if err := c.ShouldBindJSON(&req); err != nil {
  104. c.JSON(200, result.Result{
  105. Code: 400,
  106. Data: nil,
  107. Msg: "参数错误",
  108. })
  109. return
  110. }
  111. re := service.UpdateDHCPInfo(req)
  112. c.JSON(200, re)
  113. }
  114. func GetDHCPInfoByMac(c *gin.Context) {
  115. req := c.Query("mac")
  116. re := service.GetDHCPInfoByMac(req)
  117. c.JSON(200, re)
  118. }