Passport/pkg/cmd/main.go

91 lines
2.5 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package main
import (
2024-04-20 06:05:50 +00:00
"git.solsynth.dev/hydrogen/passport/pkg"
"git.solsynth.dev/hydrogen/passport/pkg/i18n"
2024-01-06 17:56:32 +00:00
"os"
"os/signal"
"syscall"
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/external"
"git.solsynth.dev/hydrogen/passport/pkg/grpc"
"git.solsynth.dev/hydrogen/passport/pkg/server"
"git.solsynth.dev/hydrogen/passport/pkg/services"
2024-03-20 12:56:43 +00:00
"github.com/robfig/cron/v3"
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/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.")
}
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.")
}
if err := database.NewBolt(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when init bolt db.")
}
2024-01-06 17:56:32 +00:00
// 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)))
quartz.AddFunc("@every 60m", services.DoAutoSignoff)
quartz.AddFunc("@every 60m", services.DoAutoAuthCleanup)
2024-04-05 17:07:36 +00:00
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
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()
database.B.Close()
2024-01-06 17:56:32 +00:00
}