github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/cmd/test_main/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/abolfazlbeh/zhycan/internal/config"
     6  	"github.com/radovskyb/watcher"
     7  	"log"
     8  	"os"
     9  	"os/signal"
    10  	"regexp"
    11  	"syscall"
    12  	"time"
    13  )
    14  
    15  func main() {
    16  	fmt.Println("Zhycan Project")
    17  
    18  	path := "/Users/abolfazl.beh/Projects/zhycan/"
    19  	initialMode := "dev"
    20  	prefix := "ZHYCAN"
    21  
    22  	err := config.CreateManager(path, initialMode, prefix)
    23  	if err != nil {
    24  		fmt.Println(err)
    25  		return
    26  	}
    27  
    28  	for {
    29  		flag := config.GetManager().IsInitialized()
    30  		if flag {
    31  			break
    32  		}
    33  		time.Sleep(1 * time.Second)
    34  	}
    35  	fmt.Println("All modules is initialized")
    36  
    37  	//l, _ := logger.GetManager().GetLogger()
    38  	//if l != nil {
    39  	//	l.Log(logger.NewLogObject(
    40  	//		logger.INFO, "test", logger.FuncMaintenanceType, time.Now().UTC(), "this is a test", nil))
    41  	//}
    42  
    43  	AddWatcher()
    44  
    45  	quit := make(chan os.Signal)
    46  	// kill (no param) default send syscall.SIGTERM
    47  	// kill -2 is syscall.SIGINT
    48  	// kill -9 is syscall.SIGKILL but can't be caught, so don't need to add it
    49  	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
    50  	<-quit
    51  	log.Println("Shutdown Server ...")
    52  
    53  }
    54  
    55  func AddWatcher() {
    56  	w := watcher.New()
    57  	w.FilterOps(watcher.Create, watcher.Move, watcher.Remove, watcher.Rename, watcher.Write)
    58  	w.AddFilterHook(watcher.RegexFilterHook(regexp.MustCompile(".*.go$"), false))
    59  	if err := w.AddRecursive("."); err != nil {
    60  		log.Fatalln(err)
    61  	}
    62  	w.SetMaxEvents(1)
    63  
    64  	go func() {
    65  		for {
    66  			select {
    67  			case event := <-w.Event:
    68  				fmt.Println(event) // Print the event's info.
    69  			case err := <-w.Error:
    70  				log.Fatalln(err)
    71  			case <-w.Closed:
    72  				return
    73  			}
    74  		}
    75  	}()
    76  
    77  	// Print a list of all of the files and folders currently
    78  	// being watched and their paths.
    79  	for path, f := range w.WatchedFiles() {
    80  		fmt.Printf("%s: %s\n", path, f.Name())
    81  	}
    82  
    83  	if err := w.Start(time.Millisecond * 100); err != nil {
    84  		log.Fatalln(err)
    85  	}
    86  
    87  }