-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
72 lines (61 loc) · 1.48 KB
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/rs/zerolog/log"
storageserver "github.com/spaghettifunk/norman/internal/storage"
"github.com/spaghettifunk/norman/pkg/logger"
"github.com/spf13/cobra"
)
// storageCmd represents the storage command
var storageCmd = &cobra.Command{
Use: "storage",
Short: "A brief description of your command",
Long: ``,
Run: storageRun,
}
func storageRun(cmd *cobra.Command, args []string) {
if normanCfg == nil {
panic(fmt.Errorf("configuration has not loaded correctly"))
}
// initialize global logging
logger.InitLogger("storage", *normanCfg)
// initialize service
st, err := storageserver.New(*normanCfg)
if err != nil {
panic(err.Error())
}
// signal channel to capture system calls
done := make(chan bool, 1)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
// start shutdown goroutine
go func() {
// capture sigterm and other system call here
<-sigCh
// shutdown http server
if err := st.ShutdownServer(); err != nil {
log.Fatal().Err(err)
return
}
log.Info().Msg("Storage Server is down. Bye Bye!")
close(done)
}()
go func() {
addr := fmt.Sprintf("%s:%d", normanCfg.Storage.Address, normanCfg.Storage.Port)
if err := st.StartServer(addr); err != nil {
log.Fatal().Err(err)
close(done)
}
}()
// time to say goodbye
<-done
}
func init() {
rootCmd.AddCommand(storageCmd)
}