main.go 3.9 KB

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