package app

import (
	"os"
	"os/signal"
	"syscall"

	"github.com/fiskerinc/cloud-services/pkg/logger"
)

// Setup primes application while enabling proper
// cleanup methods
func Setup(app string, cleanup func()) {
	logger.Info().Msgf("initializing %s", app)
	EnableGracefulShutdown(cleanup)
}

// EnableGracefulShutdown catches ctrl+c interrupt
// to allow for graceful shutdowns
func EnableGracefulShutdown(cleanup func()) {
	c := make(chan os.Signal)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-c
		cleanup()
		os.Exit(1)
	}()
}
