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

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