2024-01-06 17:56:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-26 16:06:23 +00:00
|
|
|
"fmt"
|
2024-10-23 16:46:59 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-10-26 16:06:23 +00:00
|
|
|
"github.com/fatih/color"
|
2024-01-06 17:56:32 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
pkg "git.solsynth.dev/hypernet/passport/pkg/internal"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
|
2024-09-15 18:37:02 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/grpc"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-03-20 12:56:43 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
|
|
|
"git.solsynth.dev/hypernet/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() {
|
2024-10-26 16:06:23 +00:00
|
|
|
// Booting screen
|
|
|
|
fmt.Println(color.YellowString(" ____ _\n| _ \\ __ _ ___ ___ _ __ ___ _ __| |_\n| |_) / _` / __/ __| '_ \\ / _ \\| '__| __|\n| __/ (_| \\__ \\__ \\ |_) | (_) | | | |_\n|_| \\__,_|___/___/ .__/ \\___/|_| \\__|\n |_|"))
|
|
|
|
fmt.Printf("%s v%s\n", color.New(color.FgHiYellow).Add(color.Bold).Sprintf("Hypernet.Passport"), pkg.AppVersion)
|
|
|
|
fmt.Printf("The user identity service in Hypernet\n")
|
|
|
|
color.HiBlack("=====================================================\n")
|
|
|
|
|
2024-01-06 17:56:32 +00:00
|
|
|
// 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-10-23 16:13:16 +00:00
|
|
|
// Connect to nexus
|
|
|
|
if err := gap.InitializeToNexus(); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("An error occurred when connecting to nexus...")
|
|
|
|
}
|
|
|
|
|
2024-10-23 16:46:59 +00:00
|
|
|
// Load keypair
|
|
|
|
if reader, err := sec.NewInternalTokenReader(viper.GetString("security.internal_public_key")); err != nil {
|
|
|
|
log.Error().Err(err).Msg("An error occurred when reading internal public key for jwt. Authentication related features will be disabled.")
|
|
|
|
} else {
|
2024-10-27 04:50:07 +00:00
|
|
|
http.IReader = reader
|
2024-10-23 16:46:59 +00:00
|
|
|
log.Info().Msg("Internal jwt public key loaded.")
|
|
|
|
}
|
2024-10-30 16:23:53 +00:00
|
|
|
if reader, err := sec.NewJwtReader(viper.GetString("security.public_key")); err != nil {
|
|
|
|
log.Error().Err(err).Msg("An error occurred when reading public key for jwt. Signing token may not work.")
|
|
|
|
} else {
|
|
|
|
services.EReader = reader
|
|
|
|
log.Info().Msg("Jwt public key loaded.")
|
|
|
|
}
|
|
|
|
if writer, err := sec.NewJwtWriter(viper.GetString("security.private_key")); err != nil {
|
|
|
|
log.Error().Err(err).Msg("An error occurred when reading private key for jwt. Signing token may not work.")
|
|
|
|
} else {
|
|
|
|
services.EWriter = writer
|
|
|
|
log.Info().Msg("Jwt private key loaded.")
|
|
|
|
}
|
2024-10-23 16:46:59 +00:00
|
|
|
|
2024-01-06 17:56:32 +00:00
|
|
|
// Connect to database
|
2024-03-22 16:28:27 +00:00
|
|
|
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-09-22 05:13:05 +00:00
|
|
|
// Initialize cache
|
|
|
|
if err := cache.NewStore(); err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("An error occurred when initializing cache.")
|
|
|
|
}
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
// App
|
|
|
|
go http.NewServer().Listen()
|
2024-01-06 17:56:32 +00:00
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
// Grpc App
|
2024-06-24 15:06:20 +00:00
|
|
|
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)))
|
2024-03-23 05:04:25 +00:00
|
|
|
quartz.AddFunc("@every 60m", services.DoAutoSignoff)
|
2024-04-05 17:07:36 +00:00
|
|
|
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
|
2024-09-15 18:37:02 +00:00
|
|
|
quartz.AddFunc("@midnight", services.RecycleUnConfirmAccount)
|
2024-07-03 14:57:17 +00:00
|
|
|
quartz.AddFunc("@every 60s", services.SaveEventChanges)
|
2024-04-06 03:48:45 +00:00
|
|
|
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
|
|
|
}
|