@@ -12,58 +12,85 @@ import (
1212 "time"
1313
1414 "github.com/fsnotify/fsnotify"
15+ "github.com/spf13/cobra"
1516 "github.com/spf13/viper"
1617)
1718
18- // Linker Injections
19- // Version injection with Docker Build & ldflags
20- // Do not modify, init or change in code!
21- var AppVersion string
19+ var (
20+ restartChan = make (chan struct {})
21+ quitChan = make (chan os.Signal , 1 )
22+ AppVersion string // Version injected with ldflags
23+ )
2224
23- // TODO: support for multiple network adapters.
25+ // Root command using Cobra
26+ var rootCmd = & cobra.Command {
27+ Use : "packagelock" ,
28+ Short : "Packagelock CLI tool" ,
29+ Long : `Packagelock CLI manages the server and other operations.` ,
30+ }
2431
25- func main () {
26- // Start Viper for config management
32+ // Start command to run the server
33+ var startCmd = & cobra.Command {
34+ Use : "start" ,
35+ Short : "Start the server" ,
36+ Run : func (cmd * cobra.Command , args []string ) {
37+ startServer ()
38+ },
39+ }
40+
41+ func init () {
42+ // Add commands to rootCmd
43+ rootCmd .AddCommand (startCmd )
44+
45+ // Initialize Viper config
46+ cobra .OnInitialize (initConfig )
47+ }
48+
49+ // initConfig initializes Viper and configures the application
50+ func initConfig () {
2751 config .Config = config .StartViper (viper .New ())
2852
2953 // If AppVersion is injected, set it in the configuration
3054 if AppVersion != "" {
3155 config .Config .SetDefault ("general.app-version" , AppVersion )
3256 }
3357
58+ // Check and create self-signed certificates if missing
3459 if _ , err := os .Stat (config .Config .GetString ("network.ssl-config.certificatepath" )); os .IsNotExist (err ) {
3560 fmt .Println ("Certificate files missing, creating new self-signed." )
36- err := certs .CreateSelfSignedCert (config .Config .GetString ("network.ssl-config.certificatepath" ), config .Config .GetString ("network.ssl-config.privatekeypath" ))
61+ err := certs .CreateSelfSignedCert (
62+ config .Config .GetString ("network.ssl-config.certificatepath" ),
63+ config .Config .GetString ("network.ssl-config.privatekeypath" ))
3764 if err != nil {
3865 fmt .Printf ("Error creating self-signed certificate: %v\n " , err )
39- return
66+ os . Exit ( 1 )
4067 }
4168 }
69+ }
4270
71+ // startServer starts the Fiber server with appropriate configuration
72+ func startServer () {
4373 fmt .Println (config .Config .AllSettings ())
4474
45- // Channel to signal the restart
46- restartChan := make (chan struct {})
47- quitChan := make (chan os.Signal , 1 )
4875 signal .Notify (quitChan , os .Interrupt , syscall .SIGTERM )
4976
5077 // Start the server in a goroutine
5178 go func () {
5279 for {
53- // Add Fiber routes
5480 router := server .AddRoutes (config .Config )
5581
56- // Fiber does not use the standard http.Server
5782 // Setup server address from config
5883 serverAddr := config .Config .GetString ("network.fqdn" ) + ":" + config .Config .GetString ("network.port" )
5984
60- // Fiber specific server start
85+ // Start server based on SSL config
6186 go func () {
62- fmt .Printf ("Starting Fiber HTTPS server at https://%s...\n " , serverAddr )
63-
64- // start ssl session if ssl:true is set in config file, else start http
65- if config .Config .Get ("network.ssl" ) == true {
66- if err := server .ListenAndServeTLS (router .Router , config .Config .GetString ("network.ssl-config.certificatepath" ), config .Config .GetString ("network.ssl-config.privatekeypath" ), serverAddr ); err != nil {
87+ if config .Config .GetBool ("network.ssl" ) {
88+ fmt .Printf ("Starting Fiber HTTPS server at https://%s...\n " , serverAddr )
89+ if err := server .ListenAndServeTLS (
90+ router .Router ,
91+ config .Config .GetString ("network.ssl-config.certificatepath" ),
92+ config .Config .GetString ("network.ssl-config.privatekeypath" ),
93+ serverAddr ); err != nil {
6794 fmt .Printf ("Server error: %s\n " , err )
6895 }
6996 } else {
@@ -74,12 +101,10 @@ func main() {
74101 }
75102 }()
76103
77- // Wait for either a restart signal or termination signal
104+ // Handle restart or quit signals
78105 select {
79106 case <- restartChan :
80107 fmt .Println ("Restarting Fiber server..." )
81-
82- // Gracefully shutdown the Fiber server
83108 _ , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
84109 defer cancel ()
85110 if err := router .Router .Shutdown (); err != nil {
@@ -90,8 +115,6 @@ func main() {
90115
91116 case <- quitChan :
92117 fmt .Println ("Shutting down Fiber server..." )
93-
94- // Gracefully shutdown on quit signal
95118 _ , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
96119 defer cancel ()
97120 if err := router .Router .Shutdown (); err != nil {
@@ -104,15 +127,23 @@ func main() {
104127 }
105128 }()
106129
107- // Watch for configuration changes
130+ // Watch for config changes
108131 config .Config .OnConfigChange (func (e fsnotify.Event ) {
109132 fmt .Println ("Config file changed:" , e .Name )
110133 fmt .Println ("Restarting to apply changes..." )
111- restartChan <- struct {}{} // Send signal to restart the server
134+ restartChan <- struct {}{}
112135 })
113136 config .Config .WatchConfig ()
114137
115138 // Block until quit signal is received
116139 <- quitChan
117140 fmt .Println ("Main process exiting." )
118141}
142+
143+ func main () {
144+ // Execute the Cobra root command
145+ if err := rootCmd .Execute (); err != nil {
146+ fmt .Println (err )
147+ os .Exit (1 )
148+ }
149+ }
0 commit comments