package main import ( "fmt" "os" "os/signal" "syscall" "githib.com/Theodor-Springmann-Stiftung/kgpz_web/app" "githib.com/Theodor-Springmann-Stiftung/kgpz_web/helpers" "githib.com/Theodor-Springmann-Stiftung/kgpz_web/providers" "githib.com/Theodor-Springmann-Stiftung/kgpz_web/server" ) // 1. Check if folder exists // - If not, clone the repo, if possible or throw if error // 2. If the folder exists, we try to serialize -- and spawn a goroutine to pull. // Upon pulling, we read in the current state of the repository, even if it's up to date. // -> If the repo was changed we execute a callback and parse again. // -> If pulling fails, we retry after a certain amount of time. // Still we can continue if serialization proceeds. // -> If serialization fails, we throw an error, log it. We try to pull in the background. // - setup commit date & hash // - Setup GitHub webhook if set func main() { cfg := providers.NewConfigProvider([]string{"config.dev.json", "config.json"}) if err := cfg.Read(); err != nil { helpers.MaybePanic(err, "Error reading config") } kgpz := app.NewKGPZ(cfg) Bootstrap(kgpz) server := server.Start(kgpz) Start(kgpz, server) } func Bootstrap(k *app.KGPZ) { k.InitRepo() k.Serialize() } func Start(k *app.KGPZ, s *server.Server) { s.Start() sigs := make(chan os.Signal, 1) done := make(chan bool, 1) serversignals := s.Events.Subscribe(1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) go func() { _ = <-sigs fmt.Println("Received signal. Cleaning up.") // INFO: here we add cleanup functions s.Kill() s.BreakUntil(serversignals, server.Killed) fmt.Println("Server killed.") k.Shutdown() done <- true }() // Interactive listening for input if k.IsDebug() { go func() { for { var input string fmt.Scanln(&input) if input == "r" { fmt.Println("Restarting server.") s.Restart() } else if input == "p" { fmt.Println("Pulling repo.") k.Pull() } else if input == "q" { done <- true } } }() } <-done fmt.Println("Cleanup finished. Exiting.") }