2024-05-17 07:59:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-16 15:24:54 +00:00
|
|
|
database2 "git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
|
|
|
grpc2 "git.solsynth.dev/hydrogen/paperclip/pkg/internal/grpc"
|
2024-05-17 07:59:51 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2024-06-16 15:24:54 +00:00
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server"
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
2024-05-17 07:59:51 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
|
2024-06-16 15:24:54 +00:00
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal"
|
2024-05-17 07:59:51 +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
|
2024-06-16 15:24:54 +00:00
|
|
|
if err := database2.NewSource(); err != nil {
|
2024-05-17 07:59:51 +00:00
|
|
|
log.Fatal().Err(err).Msg("An error occurred when connect to database.")
|
2024-06-16 15:24:54 +00:00
|
|
|
} else if err := database2.RunMigration(database2.C); err != nil {
|
2024-05-17 07:59:51 +00:00
|
|
|
log.Fatal().Err(err).Msg("An error occurred when running database auto migration.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect other services
|
2024-06-16 15:24:54 +00:00
|
|
|
if err := grpc2.ConnectPassport(); err != nil {
|
2024-05-17 07:59:51 +00:00
|
|
|
log.Fatal().Err(err).Msg("An error occurred when connecting to passport grpc endpoint...")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure timed tasks
|
|
|
|
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
|
|
|
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
|
|
|
|
quartz.Start()
|
|
|
|
|
|
|
|
// Server
|
|
|
|
server.NewServer()
|
|
|
|
go server.Listen()
|
|
|
|
|
|
|
|
// Grpc Server
|
|
|
|
go func() {
|
2024-06-16 15:24:54 +00:00
|
|
|
if err := grpc2.StartGrpc(); err != nil {
|
2024-05-17 07:59:51 +00:00
|
|
|
log.Fatal().Err(err).Msg("An message occurred when starting grpc server.")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Messages
|
2024-06-16 15:24:54 +00:00
|
|
|
log.Info().Msgf("Paperclip v%s is started...", pkg.AppVersion)
|
2024-05-17 07:59:51 +00:00
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-quit
|
|
|
|
|
2024-06-16 15:24:54 +00:00
|
|
|
log.Info().Msgf("Paperclip v%s is quitting...", pkg.AppVersion)
|
2024-05-17 07:59:51 +00:00
|
|
|
|
|
|
|
quartz.Stop()
|
|
|
|
}
|