Paperclip/pkg/main.go

107 lines
3.5 KiB
Go
Raw Normal View History

2024-05-17 07:59:51 +00:00
package main
import (
2024-10-27 05:13:40 +00:00
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
2024-11-02 02:41:31 +00:00
pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
2024-10-27 05:13:40 +00:00
"github.com/fatih/color"
2024-05-17 07:59:51 +00:00
"os"
"os/signal"
"syscall"
2024-11-02 02:41:31 +00:00
"git.solsynth.dev/hypernet/paperclip/pkg/internal/cache"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/grpc"
2024-06-22 04:18:54 +00:00
2024-11-02 02:41:31 +00:00
"git.solsynth.dev/hypernet/paperclip/pkg/internal/server"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
2024-05-17 07:59:51 +00:00
"github.com/robfig/cron/v3"
"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-27 05:13:40 +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.Paperclip"), pkg.AppVersion)
fmt.Printf("The upload service in Hypernet\n")
color.HiBlack("=====================================================\n")
2024-05-17 07:59:51 +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-10-27 05:13:40 +00:00
// Connect to nexus
if err := gap.InitializeToNexus(); err != nil {
log.Error().Err(err).Msg("An error occurred when registering service 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 {
server.IReader = reader
log.Info().Msg("Internal jwt public key loaded.")
}
2024-05-17 07:59:51 +00:00
// Connect to database
2024-11-02 17:53:57 +00:00
if err := database.NewGorm(); 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:32:52 +00:00
} else if err := database.RunMigration(database.C); err != nil {
2024-05-17 07:59:51 +00:00
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-08-18 07:58:07 +00:00
// Set up some workers
2024-07-28 16:53:40 +00:00
for idx := 0; idx < viper.GetInt("workers.files_deletion"); idx++ {
go services.StartConsumeDeletionTask()
}
for idx := 0; idx < viper.GetInt("workers.files_analyze"); idx++ {
go services.StartConsumeAnalyzeTask()
}
2024-05-17 07:59:51 +00:00
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
2024-08-20 09:08:40 +00:00
quartz.AddFunc("@every 60m", services.RunMarkLifecycleDeletionTask)
quartz.AddFunc("@every 60m", services.RunMarkMultipartDeletionTask)
quartz.AddFunc("@midnight", services.RunScheduleDeletionTask)
2024-05-17 07:59:51 +00:00
quartz.Start()
// Server
2024-10-27 05:13:40 +00:00
go server.NewServer().Listen()
2024-05-17 07:59:51 +00:00
// Grpc Server
2024-10-27 05:13:40 +00:00
go grpc.NewGrpc().Listen()
2024-05-17 07:59:51 +00:00
2024-10-27 05:13:40 +00:00
// Post-boot actions
services.ScanUnanalyzedFileFromDatabase()
2024-08-20 09:08:40 +00:00
services.RunMarkLifecycleDeletionTask()
2024-10-27 05:13:40 +00:00
// Messages
2024-05-17 07:59:51 +00:00
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
quartz.Stop()
}