Passport/pkg/cmd/main.go

62 lines
1.7 KiB
Go
Raw Normal View History

2024-01-06 17:56:32 +00:00
package main
import (
"code.smartsheep.studio/hydrogen/passport/pkg/external"
2024-01-26 17:11:32 +00:00
"code.smartsheep.studio/hydrogen/passport/pkg/server"
2024-01-06 17:56:32 +00:00
"os"
"os/signal"
"syscall"
passport "code.smartsheep.studio/hydrogen/passport/pkg"
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"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
// Messages
log.Info().Msgf("Passport v%s is started...", passport.AppVersion)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info().Msgf("Passport v%s is quitting...", passport.AppVersion)
}