main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // This code is a Flask web application that manages Windows services.
  2. // The equivalent code is implemented using the Gin framework in Go.
  3. package main
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "log"
  8. "net/http"
  9. "os"
  10. "os/exec"
  11. "github.com/gin-gonic/gin"
  12. )
  13. var config map[string]interface{}
  14. var logger = log.Default()
  15. func getServiceHome(method string, apiHead string) string {
  16. text := fmt.Sprintf(`
  17. <p>执行完成,请返回</p>
  18. <form method="%s" action="%s">
  19. <input type="submit" value="返回">
  20. </form>
  21. `, method, apiHead)
  22. return text
  23. }
  24. func getServiceRestartList(method string, apiHead string) string {
  25. text := ""
  26. if restartServices, ok := config["restart_service"].([]interface{}); ok {
  27. for _, service := range restartServices {
  28. if serviceStr, valid := service.(string); valid {
  29. text += fmt.Sprintf(`
  30. <form method="%s" action="%s%s">
  31. <input type="submit" value="重启%s服务">
  32. </form>`, method, apiHead, serviceStr, serviceStr)
  33. }
  34. }
  35. }
  36. return text
  37. }
  38. func getServiceStopList(method string, apiHead string) string {
  39. text := ""
  40. if stopServices, ok := config["stop_service"].([]interface{}); ok {
  41. for _, service := range stopServices {
  42. if serviceStr, valid := service.(string); valid {
  43. text += fmt.Sprintf(`
  44. <form method="%s" action="%s%s">
  45. <input type="submit" value="停止%s服务">
  46. </form>`, method, apiHead, serviceStr, serviceStr)
  47. }
  48. }
  49. }
  50. return text
  51. }
  52. func main() {
  53. defConfig := map[string]interface{}{
  54. "host": "0.0.0.0",
  55. "port": 5000,
  56. "restart_service": []interface{}{"demo"},
  57. "stop_service": []interface{}{"demo"},
  58. }
  59. configPath := "config.json"
  60. if len(os.Args) > 1 {
  61. configPath = os.Args[1]
  62. }
  63. config = defConfig
  64. file, err := os.Open(configPath)
  65. if err == nil {
  66. defer file.Close()
  67. if err := json.NewDecoder(file).Decode(&config); err != nil {
  68. logger.Println("Error loading config:", err)
  69. } else {
  70. logger.Println("配置:", config)
  71. }
  72. }
  73. router := gin.Default()
  74. router.GET("/", func(c *gin.Context) {
  75. text := getServiceRestartList("POST", "/service/restart/")
  76. text += getServiceStopList("POST", "/service/stop/")
  77. c.Header("Content-Type", "text/html; charset=utf-8")
  78. c.Data(http.StatusOK, "text/html", []byte(text))
  79. })
  80. router.GET("/service/", func(c *gin.Context) {
  81. text := getServiceRestartList("POST", "/service/restart/")
  82. text += getServiceStopList("POST", "/service/stop/")
  83. c.Header("Content-Type", "text/html; charset=utf-8")
  84. c.Data(http.StatusOK, "text/html", []byte(text))
  85. })
  86. router.POST("/service/restart/:serviceName", func(c *gin.Context) {
  87. serviceName := c.Param("serviceName")
  88. exec.Command("sc", "stop", serviceName).Run() // Assuming service can be stopped via 'sc'
  89. exec.Command("sc", "start", serviceName).Run() // Assuming service can be started via 'sc'
  90. text := getServiceHome("GET", "/service/")
  91. c.Header("Content-Type", "text/html; charset=utf-8")
  92. c.Data(http.StatusOK, "text/html", []byte(text))
  93. })
  94. router.POST("/service/stop/:serviceName", func(c *gin.Context) {
  95. serviceName := c.Param("serviceName")
  96. exec.Command("sc", "stop", serviceName).Run() // Assuming service can be stopped via 'sc'
  97. text := getServiceHome("GET", "/service/")
  98. c.Header("Content-Type", "text/html; charset=utf-8")
  99. c.Data(http.StatusOK, "text/html", []byte(text))
  100. })
  101. router.POST("/service/start/:serviceName", func(c *gin.Context) {
  102. serviceName := c.Param("serviceName")
  103. exec.Command("sc", "start", serviceName).Run() // Assuming service can be started via 'sc'
  104. text := getServiceHome("GET", "/service/")
  105. c.Header("Content-Type", "text/html; charset=utf-8")
  106. c.Data(http.StatusOK, "text/html", []byte(text))
  107. })
  108. router.Run(fmt.Sprintf("%s:%.0f", config["host"], config["port"]))
  109. }