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

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