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

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