Messaging/pkg/main.go

95 lines
3.0 KiB
Go
Raw Normal View History

2024-03-26 15:05:13 +00:00
package main
import (
2024-11-02 05:23:27 +00:00
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"github.com/fatih/color"
2024-03-26 15:05:13 +00:00
"os"
"os/signal"
"syscall"
2024-09-11 15:58:02 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/grpc"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
2024-05-26 15:01:20 +00:00
"github.com/robfig/cron/v3"
2024-11-02 05:23:27 +00:00
"git.solsynth.dev/hydrogen/messaging/pkg/internal/http"
2024-03-26 15:05:13 +00:00
2024-09-11 15:58:02 +00:00
pkg "git.solsynth.dev/hydrogen/messaging/pkg/internal"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/cache"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
2024-03-26 15:05:13 +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-11-02 05:23:27 +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.Messaging"), pkg.AppVersion)
fmt.Printf("The instant messaging service in Hypernet\n")
color.HiBlack("=====================================================\n")
2024-03-26 15:05:13 +00:00
// 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.")
}
2024-11-02 05:23:27 +00:00
// Connect to nexus
if err := gap.InitializeToNexus(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when connecting to nexus...")
}
// 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 {
http.IReader = reader
log.Info().Msg("Internal jwt public key loaded.")
}
2024-03-26 15:05:13 +00:00
// Connect to database
2024-11-02 05:23:27 +00:00
if err := database.NewGorm(); err != nil {
2024-03-26 15:05:13 +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.")
}
// Initialize cache
if err := cache.NewStore(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when initializing cache.")
}
2024-03-26 15:05:13 +00:00
// Connect other services
2024-07-16 06:44:00 +00:00
services.SetupLiveKit()
2024-03-26 15:05:13 +00:00
// Server
2024-11-02 05:23:27 +00:00
go http.NewServer().Listen()
2024-03-26 15:05:13 +00:00
2024-11-02 05:23:27 +00:00
go grpc.NewGrpc().Listen()
2024-04-06 06:05:51 +00:00
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
quartz.Start()
2024-03-26 15:05:13 +00:00
// Messages
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
2024-04-06 06:05:51 +00:00
quartz.Stop()
2024-03-26 15:05:13 +00:00
}