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

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