BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr
Added run_dev.go and run_dev.sh for development. Fixed some bugs.
Jackacc1005242c0
diff --git a/core/.gitignore b/core/.gitignoreindex 23fac285..33810030 100644--- a/core/.gitignore+++ b/core/.gitignore@@ -17,4 +17,5 @@ temp/temp.yamlbin**/config/config.yaml-data/\ No newline at end of file+data/+/test.godiff --git a/core/internal/service/rbac/role.go b/core/internal/service/rbac/role.goindex 11c9aa6f..adb2d525 100644--- a/core/internal/service/rbac/role.go+++ b/core/internal/service/rbac/role.go@@ -62,8 +62,12 @@ func (s *roleService) GetById(ctx context.Context, roleId int64) (*model.Role, e// Create creates a new rolefunc (s *roleService) Create(ctx context.Context, name, description string, status int) (int64, error) {- result, err := g.DB().Model("role").Insert(model.Role{- RoleName: name,+ result, err := g.DB().Model("role").Insert(g.Map{+ "role_name": name,+ "description": description,+ "status": status,+ "create_time": time.Now().Unix(),+ "update_time": time.Now().Unix(),})if err != nil {return 0, errdiff --git a/core/run_dev.go b/core/run_dev.gonew file mode 100644index 00000000..1934d0f3--- /dev/null+++ b/core/run_dev.go@@ -0,0 +1,322 @@+package main++import (+ "bufio"+ "context"+ "fmt"+ "log"+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "sync"+ "time"++ "github.com/fsnotify/fsnotify"+)++var (+ isDev = false+ ROOT_PATH = GetCwdPath()+)++// FileWatcher structure+type FileWatcher struct {+ watcher *fsnotify.Watcher // File system watcher+ rootPath string // Root directory to monitor+ scriptPath string // Script path to execute+ extensions []string // File extensions to monitor+ mu sync.Mutex // Mutex to prevent concurrent script execution+ isExecuting bool // Flag indicating if script is executing+ debounceMap map[string]int64 // Debounce map, recording last modification time of each file+ debounceMs int64 // Debounce time interval (milliseconds)+ logger *log.Logger // Logger+}++// NewFileWatcher creates a new file watcher+func NewFileWatcher(rootPath string, scriptPath string, extensions []string) (*FileWatcher, error) {+ watcher, err := fsnotify.NewWatcher()+ if err != nil {+ return nil, fmt.Errorf("failed to create file watcher: %w", err)+ }++ return &FileWatcher{+ watcher: watcher,+ rootPath: rootPath,+ scriptPath: scriptPath,+ extensions: extensions,+ debounceMap: make(map[string]int64),+ debounceMs: 500, // Default debounce time: 500 milliseconds+ logger: log.New(os.Stdout, "[FileWatcher] ", log.LstdFlags),+ }, nil+}++// Start begins monitoring file changes+func (fw *FileWatcher) Start(ctx context.Context) error {+ // Add all directories to the watcher+ if err := fw.addDirsRecursively(fw.rootPath); err != nil {+ return err+ }++ fw.logger.Printf("Started monitoring directory: %s", fw.rootPath)+ fw.logger.Printf("Script to execute: %s", fw.scriptPath)+ fw.logger.Printf("Monitoring file types: %v", fw.extensions)++ // Start the monitoring loop+ go fw.watchLoop(ctx)++ return nil+}++// Stop stops monitoring+func (fw *FileWatcher) Stop() {+ fw.watcher.Close()+}++// addDirsRecursively recursively adds directories to the watcher+func (fw *FileWatcher) addDirsRecursively(root string) error {+ return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {+ if err != nil {+ return err+ }++ // Skip hidden directories and files+ if strings.HasPrefix(filepath.Base(path), ".") {+ if info.IsDir() {+ return filepath.SkipDir+ }+ return nil+ }++ // Skip vendor and node_modules directories+ if info.IsDir() && (filepath.Base(path) == "vendor" || filepath.Base(path) == "node_modules") {+ return filepath.SkipDir+ }++ // Only watch directory changes+ if info.IsDir() {+ if err := fw.watcher.Add(path); err != nil {+ return fmt.Errorf("failed to add directory %s to watcher: %w", path, err)+ }+ }+ return nil+ })+}++// watchLoop monitoring loop+func (fw *FileWatcher) watchLoop(ctx context.Context) {+ for {+ select {+ case event, ok := <-fw.watcher.Events:+ if !ok {+ return+ }++ // Only focus on write and create events+ if event.Op&(fsnotify.Write|fsnotify.Create) == 0 {+ continue+ }++ // If a new directory is created, add it to the watcher+ if fi, err := os.Stat(event.Name); err == nil && fi.IsDir() {+ fw.watcher.Add(event.Name)+ continue+ }++ // Check if file extension matches+ if !fw.hasMatchingExtension(event.Name) {+ continue+ }++ // Apply debounce logic+ if fw.shouldDebounce(event.Name) {+ continue+ }++ // Execute script+ fw.logger.Printf("Detected file change: %s", event.Name)+ go fw.executeScript(ctx)++ case err, ok := <-fw.watcher.Errors:+ if !ok {+ return+ }+ fw.logger.Printf("Watcher error: %v", err)+ }+ }+}++// hasMatchingExtension checks if file has a matching extension+func (fw *FileWatcher) hasMatchingExtension(filename string) bool {+ ext := strings.ToLower(filepath.Ext(filename))+ for _, e := range fw.extensions {+ if ext == e {+ return true+ }+ }+ return false+}++// shouldDebounce checks if debouncing should be applied (avoid multiple executions in short time)+func (fw *FileWatcher) shouldDebounce(filename string) bool {+ fw.mu.Lock()+ defer fw.mu.Unlock()++ now := time.Now().UnixMilli()+ lastTime, exists := fw.debounceMap[filename]++ // Update last modification time+ fw.debounceMap[filename] = now++ // If it's the first modification or beyond debounce time, don't debounce+ if !exists || now-lastTime > fw.debounceMs {+ return false+ }++ return true+}++// executeScript executes the script+func (fw *FileWatcher) executeScript(ctx context.Context) {+ fw.mu.Lock()+ if fw.isExecuting {+ fw.mu.Unlock()+ return+ }+ fw.isExecuting = true+ fw.mu.Unlock()++ defer func() {+ fw.mu.Lock()+ fw.isExecuting = false+ fw.mu.Unlock()+ }()++ // Check if script exists+ if _, err := os.Stat(fw.scriptPath); os.IsNotExist(err) {+ fw.logger.Printf("Script does not exist: %s", fw.scriptPath)+ return+ }++ // Create command+ cmd := exec.Command("/bin/bash", fw.scriptPath)+ cmd.Env = os.Environ()++ // Get standard output and error output+ stdout, err := cmd.StdoutPipe()+ if err != nil {+ fw.logger.Printf("Failed to get standard output: %v", err)+ return+ }++ stderr, err := cmd.StderrPipe()+ if err != nil {+ fw.logger.Printf("Failed to get error output: %v", err)+ return+ }++ // Start command+ fw.logger.Printf("Executing script: %s", fw.scriptPath)+ if err := cmd.Start(); err != nil {+ fw.logger.Printf("Failed to start script: %v", err)+ return+ }++ // Real-time reading and output of standard output+ go func() {+ scanner := bufio.NewScanner(stdout)+ for scanner.Scan() {+ fmt.Println("►", scanner.Text())+ }+ }()++ // Real-time reading and output of error output+ go func() {+ scanner := bufio.NewScanner(stderr)+ for scanner.Scan() {+ fmt.Println("⚠", scanner.Text())+ }+ }()++ // Wait for command to complete+ if err := cmd.Wait(); err != nil {+ fw.logger.Printf("Script execution failed: %v", err)+ return+ }++ fw.logger.Printf("Script execution completed")+}++// GetCwdPath Get current working directory+func GetCwdPath() string {+ if len(os.Args) == 0 {+ p, _ := os.Getwd()+ return p+ }++ // Get executable path+ exePath := filepath.Dir(os.Args[0])++ // Check if it is a development environment+ if isDev || strings.Contains(filepath.ToSlash(exePath), "/go-build") {+ isDev = true+ p, _ := os.Getwd()+ return p+ }++ // Get absolute path+ p, _ := filepath.Abs(exePath)+ return p+}++// AbsPath Get absolute path+func AbsPath(p string) string {+ // In Linux, if the path starts with "/", it is an absolute path+ if strings.HasPrefix(p, "/") {+ return p+ }++ // In Windows, if the path starts with "C:", it is an absolute path+ if len(p) > 1 && p[1] == ':' {+ return p+ }++ p, _ = filepath.Abs(filepath.Join(ROOT_PATH, p))++ return p+}++func main() {+ ctx := context.Background()+ logger := log.New(os.Stdout, "[Main] ", log.LstdFlags)++ // Arguments for root path and script path+ var rootPath, scriptPath string++ // Get command line arguments+ args := os.Args+ if len(args) < 3 {+ rootPath = "." // Default root path+ scriptPath = "./run_dev.sh" // Default script path+ } else {+ rootPath = args[1]+ scriptPath = args[2]+ }++ // Create file watcher+ watcher, err := NewFileWatcher(AbsPath(rootPath), AbsPath(scriptPath), []string{".go"})+ if err != nil {+ logger.Fatalf("Failed to create file watcher: %v", err)+ }+ defer watcher.Stop()++ // Start monitoring+ if err := watcher.Start(ctx); err != nil {+ logger.Fatalf("Failed to start monitoring: %v", err)+ }++ logger.Printf("File watcher started, press Ctrl+C to stop")++ // Keep the main goroutine running+ select {}+}diff --git a/core/run_dev.sh b/core/run_dev.shnew file mode 100644index 00000000..ef5ab67b--- /dev/null+++ b/core/run_dev.sh@@ -0,0 +1,31 @@+#!/bin/bash++echo "Compiling billionmail..."++# Using the alpine image to compile the Go application+docker exec p-g-alpine sh -c "cd /opt/core && sh ./go-build.sh"++echo "Copying billionmail to billionmail-core-billionmail-1 container..."++# Determine the architecture and set the binary name accordingly+ARCH=$(uname -m)+if [[ "$ARCH" == "x86_64" ]]; then+ BINARY="billionmail-amd64"+ echo "Detect x86_64 architecture, using amd64 binary"+elif [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; then+ BINARY="billionmail-arm64"+ echo "Detect arm64/aarch64 architecture, using arm64 binary"+else+ echo "Unsupported architecture: $ARCH"+ exit 1+fi++# Copy the compiled binary to the billionmail-core-billionmail-1 container+docker cp $BINARY billionmail-core-billionmail-1:/opt/billionmail/core/billionmail++echo "Restarting billionmail-core-billionmail-1 container..."++# Restart the billionmail-core-billionmail-1 container to apply changes+docker restart billionmail-core-billionmail-1++echo "billionmail has been successfully compiled and deployed."