Passport/pkg/cmd/main.go

87 lines
2.4 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package main
import (
"os"
"os/signal"
"syscall"
2024-03-20 12:56:43 +00:00
"git.solsynth.dev/hydrogen/identity/pkg/external"
"git.solsynth.dev/hydrogen/identity/pkg/grpc"
"git.solsynth.dev/hydrogen/identity/pkg/server"
"git.solsynth.dev/hydrogen/identity/pkg/services"
"github.com/robfig/cron/v3"
identity "git.solsynth.dev/hydrogen/identity/pkg"
"git.solsynth.dev/hydrogen/identity/pkg/database"
2024-01-06 17:56:32 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
func main() {
// Configure settings
viper.AddConfigPath(".")
viper.AddConfigPath("..")
viper.SetConfigName("settings")
2024-01-28 16:32:39 +00:00
viper.SetConfigType("toml")
2024-01-06 17:56:32 +00:00
// Load settings
if err := viper.ReadInConfig(); err != nil {
log.Panic().Err(err).Msg("An error occurred when loading settings.")
}
// Connect to database
if err := database.NewSource(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when connect to database.")
} else if err := database.RunMigration(database.C); err != nil {
log.Fatal().Err(err).Msg("An error occurred when running database auto migration.")
}
// External
// All the things are optional so when error occurred the server won't crash
if err := external.SetupFirebase(viper.GetString("external.firebase.credentials")); err != nil {
log.Error().Err(err).Msg("An error occurred when starting firebase communicating...")
2024-02-08 04:35:52 +00:00
} else {
log.Info().Msg("Successfully setup firebase communication.")
}
2024-01-26 17:11:32 +00:00
// Server
server.NewServer()
go server.Listen()
2024-01-06 17:56:32 +00:00
2024-02-20 13:46:15 +00:00
// Grpc Server
go func() {
if err := grpc.StartGrpc(); err != nil {
log.Fatal().Err(err).Msg("An message occurred when starting grpc server.")
}
}()
2024-02-18 08:26:46 +00:00
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
2024-02-18 08:35:28 +00:00
quartz.AddFunc("@every 60m", func() {
2024-02-18 08:26:46 +00:00
log.Info().Msg("Running auto sign off...")
if tx := services.PerformAutoSignoff(); tx.Error != nil {
log.Error().Err(tx.Error).Msg("An error occurred when running auto sign off...")
} else {
log.Info().Int64("affected", tx.RowsAffected).Msg("Auto sign off accomplished.")
}
})
quartz.Run()
2024-01-06 17:56:32 +00:00
// Messages
2024-02-18 08:26:46 +00:00
log.Info().Msgf("Identity v%s is started...", identity.AppVersion)
2024-01-06 17:56:32 +00:00
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
2024-02-18 08:26:46 +00:00
log.Info().Msgf("Identity v%s is quitting...", identity.AppVersion)
quartz.Stop()
2024-01-06 17:56:32 +00:00
}