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

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