serve.go 2.7 KB

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