Passport/pkg/main.go

93 lines
2.6 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package main
import (
2024-06-16 15:17:32 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
2024-01-06 17:56:32 +00:00
"os"
"os/signal"
"syscall"
2024-06-16 15:17:32 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/i18n"
2024-06-02 12:15:04 +00:00
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.")
}
2024-04-20 06:05:50 +00:00
i18n.InitInternationalization()
2024-01-06 17:56:32 +00:00
// 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
2024-06-16 14:16:09 +00:00
if err := gap.Register(); err != nil {
log.Error().Err(err).Msg("An error occurred when registering service to gateway...")
}
2024-06-06 14:48:43 +00:00
if err := services.SetupFirebase(); err != nil {
log.Error().Err(err).Msg("An error occurred when connecting firebase...")
}
if err := services.SetupAPNS(); err != nil {
log.Error().Err(err).Msg("An error occurred when connecting APNs...")
}
2024-05-18 12:39:46 +00:00
if err := grpc.ConnectPaperclip(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when connecting to paperclip...")
}
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)))
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-05-09 15:35:13 +00:00
quartz.AddFunc("@every 5m", services.KexCleanup)
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
}