package main

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

	"git.pyer.club/kingecg/godaemon"
	"git.pyer.club/kingecg/gologger"
)

func main() {
	LoadConfig()
	l := gologger.GetLogger("main")
	defer func() {
		if err := recover(); err != nil {
			l.Error("panic", err)
		}
	}()
	daemon := godaemon.NewGoDaemon(start, stop)
	daemon.Start()
}

var waiter chan os.Signal

func start(g *godaemon.GoDaemon) {
	waiter = make(chan os.Signal, 1) // buffered channel
	signal.Notify(waiter, syscall.SIGTERM, syscall.SIGINT)

	httpd := &GoHttp{}

	httpd.Start()

	// blocks here until there's a signal
	<-waiter
	httpd.logger.Info("Exit")
}

func stop(g *godaemon.GoDaemon) {
	g.Running.Process.Signal(syscall.SIGTERM)
}
