initial commit
This commit is contained in:
108
cmd/server.go
Normal file
108
cmd/server.go
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"base/internal/pkg"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"base/config"
|
||||
"base/internal/application"
|
||||
"base/internal/delivery"
|
||||
"base/internal/repository"
|
||||
"base/internal/server"
|
||||
"base/internal/server/middleware"
|
||||
"base/pkg/metrics"
|
||||
)
|
||||
|
||||
// serverCmd represents the server command
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("server called")
|
||||
serverInit()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serverCmd)
|
||||
}
|
||||
|
||||
func serverInit() {
|
||||
app := fx.New(
|
||||
fx.Supply(metrics.GetMetrics("base", "api", "base-service")),
|
||||
fx.Provide(config.NewConfig),
|
||||
fx.Provide(middleware.NewMiddleware),
|
||||
pkg.Module,
|
||||
application.Module,
|
||||
repository.Module,
|
||||
delivery.Module,
|
||||
server.Server,
|
||||
fx.Invoke(registerHooks),
|
||||
)
|
||||
|
||||
startCtx, startCtxCancel := context.WithTimeout(context.Background(), fx.DefaultTimeout*10)
|
||||
defer startCtxCancel()
|
||||
|
||||
if err := app.Start(startCtx); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Wait for interrupt signal
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-c
|
||||
|
||||
stopCtx, stopCtxCancel := context.WithTimeout(context.Background(), fx.DefaultTimeout)
|
||||
defer stopCtxCancel()
|
||||
|
||||
if err := app.Stop(stopCtx); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// registerHooks registers lifecycle hooks with fx
|
||||
func registerHooks(
|
||||
lc fx.Lifecycle,
|
||||
l zerolog.Logger,
|
||||
c *config.AppConfig,
|
||||
m *metrics.Metrics,
|
||||
) error {
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
config.PrintConfig(l, c)
|
||||
|
||||
// Start system metrics collection
|
||||
if c.Metrics.Enabled {
|
||||
l.Info().Msg("System metrics collection started")
|
||||
}
|
||||
|
||||
l.Info().Msg("Application started")
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
l.Info().Msg("Shutting down application")
|
||||
l.Info().Msg("Application stopped")
|
||||
|
||||
return nil
|
||||
},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user