A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

kick.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "os"
  7. "os/exec"
  8. "os/signal"
  9. "path/filepath"
  10. "syscall"
  11. "github.com/fsnotify/fsnotify"
  12. )
  13. var appPath string
  14. var mainSourceFile string
  15. var gopherjsAppPath string
  16. func start() *exec.Cmd {
  17. if gopherjsAppPath != "" {
  18. cwd, err := os.Getwd()
  19. if err != nil {
  20. log.Fatal("Encountered an error while attempting to get the cwd: ", err)
  21. } else {
  22. os.Chdir(gopherjsAppPath)
  23. gjsCommand := exec.Command("gopherjs", "build")
  24. gjsCommand.Start()
  25. os.Chdir(cwd)
  26. }
  27. }
  28. cmd := exec.Command("go", "run", appPath+"/"+mainSourceFile)
  29. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  30. cmd.Stdout = os.Stdout
  31. cmd.Stderr = os.Stderr
  32. cmd.Start()
  33. return cmd
  34. }
  35. func stop(cmd *exec.Cmd) {
  36. pgid, err := syscall.Getpgid(cmd.Process.Pid)
  37. if err == nil {
  38. syscall.Kill(-pgid, 15)
  39. }
  40. }
  41. func restart(cmd *exec.Cmd) *exec.Cmd {
  42. var newCommand *exec.Cmd
  43. stop(cmd)
  44. newCommand = start()
  45. return newCommand
  46. }
  47. func initializeWatcher(shouldRestart chan bool, dirList []string) {
  48. watcher, err := fsnotify.NewWatcher()
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. defer watcher.Close()
  53. done := make(chan bool)
  54. go func() {
  55. for {
  56. select {
  57. case event := <-watcher.Events:
  58. if event.Op == fsnotify.Write || event.Op == fsnotify.Rename {
  59. if filepath.Ext(event.Name) == ".go" {
  60. shouldRestart <- true
  61. }
  62. }
  63. case err := <-watcher.Errors:
  64. if err != nil {
  65. log.Println("error:", err)
  66. }
  67. }
  68. }
  69. }()
  70. err = watcher.Add(appPath)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. // watch subdirectories also
  75. for _, element := range dirList {
  76. watcher.Add(element)
  77. }
  78. <-done
  79. }
  80. func pathExists(path string) (bool, error) {
  81. _, err := os.Stat(path)
  82. if err == nil {
  83. return true, nil
  84. }
  85. if os.IsNotExist(err) {
  86. return false, nil
  87. }
  88. return true, err
  89. }
  90. func main() {
  91. flag.StringVar(&appPath, "appPath", "", "The path to your Go project")
  92. flag.StringVar(&mainSourceFile, "mainSourceFile", "", "The Go source file with the main func()")
  93. flag.StringVar(&gopherjsAppPath, "gopherjsAppPath", "", "The path to your GopherJS project (optional)")
  94. flag.Parse()
  95. // Exit if no appPath is supplied
  96. if appPath == "" {
  97. fmt.Println("You must supply the appPath parameter")
  98. os.Exit(1)
  99. }
  100. if appPathExists, appPathErr := pathExists(appPath); appPathExists != true || appPathErr != nil {
  101. fmt.Println("The path you specified to your Go application project does not exist.")
  102. os.Exit(1)
  103. }
  104. if mainSourceFile == "" {
  105. fmt.Println("You must supply the mainSourceFile parameter")
  106. os.Exit(1)
  107. }
  108. if sourceFileExists, sourceFilePathErr := pathExists(appPath + "/" + mainSourceFile); sourceFileExists != true || sourceFilePathErr != nil {
  109. fmt.Println("The path to the main source file you provided does not exist.")
  110. os.Exit(1)
  111. }
  112. if gopherjsAppPath != "" {
  113. if gopherjsFileExists, gopherjsFileErr := pathExists(gopherjsAppPath); gopherjsFileExists != true || gopherjsFileErr != nil {
  114. fmt.Println("The path you specified to the GopherJS application project does not exist.")
  115. os.Exit(1)
  116. }
  117. }
  118. dirList := []string{}
  119. filepath.Walk(appPath, func(path string, f os.FileInfo, err error) error {
  120. if f.IsDir() == true {
  121. dirList = append(dirList, path)
  122. }
  123. return nil
  124. })
  125. shouldRestart := make(chan bool, 1)
  126. go initializeWatcher(shouldRestart, dirList)
  127. interrupt := make(chan os.Signal, 1)
  128. signal.Notify(interrupt, os.Interrupt)
  129. cmd := start()
  130. for {
  131. select {
  132. case <-interrupt:
  133. stop(cmd)
  134. os.Exit(0)
  135. case <-shouldRestart:
  136. fmt.Println("Recompiling and Restarting")
  137. cmd = restart(cmd)
  138. }
  139. }
  140. }