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.8KB

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