serve.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package commands
  2. import (
  3. "fmt"
  4. "time"
  5. "encoding/json"
  6. "os"
  7. "path/filepath"
  8. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  9. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/gen"
  10. "github.com/fsnotify/fsnotify"
  11. "github.com/kataras/iris/v12"
  12. "github.com/kataras/iris/v12/middleware/logger"
  13. "github.com/kataras/iris/v12/middleware/recover"
  14. )
  15. var watcher, _ = fsnotify.NewWatcher()
  16. func serve() (err error) {
  17. var (
  18. project *Project
  19. // done = make(chan bool)
  20. )
  21. defer watcher.Close()
  22. fmt.Println("Serve WITH SETUP...")
  23. // Cria um novo projeto a partir do diretorio atual
  24. if project, err = CreateProject(); err != nil {
  25. panic(err)
  26. }
  27. // starting at the root of the project, walk each file/directory searching for
  28. // directories
  29. if err := filepath.Walk("./", watchDir); err != nil {
  30. fmt.Println("ERROR", err)
  31. }
  32. HubInstance.EventHandlers["initialize"] = func(hub *Hub, event *Event) {
  33. payload, err := json.Marshal(project)
  34. if err != nil {
  35. fmt.Println("error:", err)
  36. return
  37. }
  38. fmt.Println("run .. initialize")
  39. go func(){
  40. hub.Broadcast <- &Event{
  41. Kind : "project:update",
  42. Payload : string(payload),
  43. Date : time.Now(),
  44. }
  45. }()
  46. }
  47. go func() {
  48. count := 10
  49. for {
  50. select {
  51. case event, ok := <-watcher.Events:
  52. if !ok {
  53. return
  54. }
  55. // log.Println("event:", event)
  56. if event.Op & fsnotify.Write == fsnotify.Write {
  57. fmt.Println("modified file:", event.Name)
  58. fmt.Println("update...")
  59. time.Sleep(time.Second)
  60. if project, err = CreateProject(); err != nil {
  61. panic(err)
  62. }
  63. payload, err := json.Marshal(project)
  64. if err != nil {
  65. fmt.Println("error:", err)
  66. return
  67. }
  68. HubInstance.Broadcast <- &Event{
  69. Kind : "project:update",
  70. Payload : string(payload),
  71. Date : time.Now(),
  72. }
  73. }
  74. if count = count - 1; count == 0 {
  75. break
  76. }
  77. case err := <-watcher.Errors:
  78. fmt.Println("ERROR", err)
  79. }
  80. }
  81. }()
  82. app := iris.New()
  83. app.Logger().SetLevel("debug")
  84. // Optionally, add two built'n handlers
  85. // that can recover from any http-relative panics
  86. // and log the requests to the terminal.
  87. app.Use(recover.New())
  88. app.Use(logger.New())
  89. // Method: GET
  90. // Resource: http://localhost:8080/hello
  91. app.Get("/project", func(ctx iris.Context) {
  92. ctx.JSON(project)
  93. })
  94. app.Get("/ws", WsUpgrader)
  95. app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
  96. return
  97. }
  98. func watchDir(path string, fi os.FileInfo, err error) error {
  99. if fi.Mode().IsDir() {
  100. return watcher.Add(path)
  101. }
  102. return nil
  103. }