2024-02-01 15:26:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-22 13:16:56 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
pkg "git.solsynth.dev/hydrogen/interactive/pkg/internal"
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/grpc"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
2024-04-06 03:47:58 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-02-01 15:26:17 +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")
|
|
|
|
viper.SetConfigType("toml")
|
|
|
|
|
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
// Connect other services
|
2024-06-22 09:29:53 +00:00
|
|
|
if err := gap.Register(); err != nil {
|
2024-06-22 13:16:56 +00:00
|
|
|
log.Fatal().Err(err).Msg("An error occurred when connecting to consul...")
|
2024-06-22 09:29:53 +00:00
|
|
|
} else {
|
|
|
|
gap.NewHyperClient()
|
2024-04-26 16:13:47 +00:00
|
|
|
}
|
2024-02-21 14:58:51 +00:00
|
|
|
|
2024-04-06 03:47:58 +00:00
|
|
|
// Configure timed tasks
|
|
|
|
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
|
|
|
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
|
|
|
|
quartz.Start()
|
|
|
|
|
2024-02-01 15:26:17 +00:00
|
|
|
// Server
|
|
|
|
server.NewServer()
|
|
|
|
go server.Listen()
|
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
grpc.NewGRPC()
|
|
|
|
go grpc.ListenGRPC()
|
|
|
|
|
2024-02-01 15:26:17 +00:00
|
|
|
// Messages
|
2024-06-22 09:29:53 +00:00
|
|
|
log.Info().Msgf("Interactive v%s is started...", pkg.AppVersion)
|
2024-02-01 15:26:17 +00:00
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-quit
|
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
log.Info().Msgf("Interactive v%s is quitting...", pkg.AppVersion)
|
2024-04-06 03:47:58 +00:00
|
|
|
|
|
|
|
quartz.Stop()
|
2024-02-01 15:26:17 +00:00
|
|
|
}
|