Files
kgpz_web/kgpz_web.go
2024-11-11 04:50:00 +01:00

70 lines
1.8 KiB
Go

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) {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 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()
k.Shutdown()
done <- true
}()
// INFO: if the server fails to start this exits the program
// We handle graceful server recovery in the server itself.
go func() {
s.Start()
<-s.Killed
fmt.Println("Server exited. Cleaning up.")
}()
<-done
fmt.Println("Cleanup finished. Exiting.")
}