-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-watch.go
More file actions
113 lines (92 loc) · 2.45 KB
/
dev-watch.go
File metadata and controls
113 lines (92 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//go:build ignore
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func main() {
fmt.Println("🔥 Payment Gateway Hot Reload Server")
fmt.Println("📁 Watching for file changes...")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Add directories to watch
dirs := []string{".", "internal", "internal/handlers", "internal/services", "internal/models", "internal/kafka", "internal/logger", "internal/middleware", "internal/config", "internal/storage"}
for _, dir := range dirs {
if _, err := os.Stat(dir); err == nil {
err = watcher.Add(dir)
if err != nil {
log.Printf("Error watching %s: %v", dir, err)
} else {
fmt.Printf("👀 Watching: %s\n", dir)
}
}
}
var cmd *exec.Cmd
restart := make(chan bool, 1)
// Start the application
go startApp(&cmd, restart)
// Initial start
restart <- true
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
// Only restart on Go file changes
if strings.HasSuffix(event.Name, ".go") && (event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create) {
fmt.Printf("🔄 File changed: %s\n", filepath.Base(event.Name))
fmt.Println("🔨 Rebuilding and restarting...")
// Stop current process
if cmd != nil && cmd.Process != nil {
cmd.Process.Kill()
}
// Restart after a short delay
time.Sleep(500 * time.Millisecond)
restart <- true
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error:", err)
}
}
}
func startApp(cmd **exec.Cmd, restart <-chan bool) {
for range restart {
// Build the application
fmt.Println("🔨 Building application...")
buildCmd := exec.Command("go", "build", "-o", "payment-gateway", ".")
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
fmt.Printf("❌ Build failed: %v\n", err)
continue
}
fmt.Println("✅ Build successful!")
fmt.Println("🚀 Starting Payment Gateway...")
fmt.Println("" + strings.Repeat("=", 50))
// Start the application
*cmd = exec.Command("./payment-gateway")
(*cmd).Stdout = os.Stdout
(*cmd).Stderr = os.Stderr
if err := (*cmd).Start(); err != nil {
fmt.Printf("❌ Failed to start: %v\n", err)
continue
}
// Wait for the process to finish
go func() {
(*cmd).Wait()
}()
}
}