Passport/pkg/main.go

77 lines
2.1 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package main
import (
"os"
"os/signal"
"syscall"
2024-09-15 18:37:02 +00:00
pkg "git.solsynth.dev/hydrogen/passport/pkg/internal"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
2024-06-16 15:17:32 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/grpc"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
2024-03-20 12:56:43 +00:00
"github.com/robfig/cron/v3"
2024-06-16 15:17:32 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/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.NewGorm(); err != nil {
2024-01-06 17:56:32 +00:00
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.")
}
2024-05-18 12:39:46 +00:00
// Connect other services
if err := gap.RegisterService(); err != nil {
2024-06-16 14:16:09 +00:00
log.Error().Err(err).Msg("An error occurred when registering service to gateway...")
}
2024-05-18 12:39:46 +00:00
2024-01-26 17:11:32 +00:00
// Server
go server.NewServer().Listen()
2024-01-06 17:56:32 +00:00
2024-02-20 13:46:15 +00:00
// Grpc Server
go grpc.NewServer().Listen()
2024-02-20 13:46:15 +00:00
2024-02-18 08:26:46 +00:00
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 60m", services.DoAutoSignoff)
2024-04-05 17:07:36 +00:00
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
2024-05-17 11:37:58 +00:00
quartz.AddFunc("@every 60s", services.RecycleAuthContext)
2024-09-15 18:37:02 +00:00
quartz.AddFunc("@midnight", services.RecycleUnConfirmAccount)
quartz.AddFunc("@every 60s", services.SaveEventChanges)
quartz.Start()
2024-02-18 08:26:46 +00:00
2024-01-06 17:56:32 +00:00
// Messages
2024-04-20 06:05:50 +00:00
log.Info().Msgf("Passport v%s is started...", pkg.AppVersion)
2024-01-06 17:56:32 +00:00
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
2024-04-20 06:05:50 +00:00
log.Info().Msgf("Passport v%s is quitting...", pkg.AppVersion)
2024-02-18 08:26:46 +00:00
quartz.Stop()
2024-01-06 17:56:32 +00:00
}