package main

import (
	"fmt"
	"log"
	"os"

	cnf "git.pbiernat.io/egommerce/go-api-pkg/config"

	"git.pbiernat.io/egommerce/identity-service/internal/app"
	"git.pbiernat.io/egommerce/identity-service/internal/server"
)

func main() {
	if cnf.ErrLoadingEnvs != nil {
		log.Panicln("Error loading .env file", cnf.ErrLoadingEnvs)
	}

	c := server.NewConfig("identity")
	cArr := c.GetArray()

	doer := server.New(c)
	a := app.NewApp(doer)
	a.RegisterPlugin(app.LoggerPlugin(cArr))
	a.RegisterPlugin(app.CachePlugin(cArr))
	a.RegisterPlugin(app.DatabasePlugin(cArr))
	// a.RegisterPlugin(app.EventbusPlugin(cArr))
	a.RegisterPlugin(app.RegistryPlugin(cArr))

	while := make(chan struct{})
	err := a.Start(while)
	<-while

	if err != nil {
		log.Fatalf("Failed to start server. Reason: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("Gone")
	os.Exit(0)
}
