forked from m1k1o/go-transcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 1.37 KB
/
main.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
package transcode
import (
"os"
"os/signal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/m1k1o/go-transcode/internal/api"
"github.com/m1k1o/go-transcode/internal/config"
"github.com/m1k1o/go-transcode/internal/http"
)
var Service *Main
func init() {
Service = &Main{
RootConfig: &config.Root{},
ServerConfig: &config.Server{},
}
}
type Main struct {
RootConfig *config.Root
ServerConfig *config.Server
logger zerolog.Logger
apiManager *api.ApiManagerCtx
server *http.ServerCtx
}
func (main *Main) Preflight() {
main.logger = log.With().Str("service", "main").Logger()
}
func (main *Main) Start() {
main.apiManager = api.New()
main.server = http.New(
main.apiManager,
main.ServerConfig,
)
main.server.Start()
}
func (main *Main) Shutdown() {
if err := main.server.Shutdown(); err != nil {
main.logger.Err(err).Msg("server shutdown with an error")
} else {
main.logger.Debug().Msg("server shutdown")
}
}
func (main *Main) ServeCommand(cmd *cobra.Command, args []string) {
main.logger.Info().Msg("starting main server")
main.Start()
main.logger.Info().Msg("main ready")
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
sig := <-quit
main.logger.Warn().Msgf("received %s, attempting graceful shutdown: \n", sig)
main.Shutdown()
main.logger.Info().Msg("shutdown complete")
}