/
server.go
48 lines (37 loc) · 1.15 KB
/
server.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
package cmd
import (
"os"
"github.com/CafeKetab/user/internal/api/grpc"
"github.com/CafeKetab/user/internal/api/http"
"github.com/CafeKetab/user/internal/config"
"github.com/CafeKetab/user/internal/repository"
"github.com/CafeKetab/user/pkg/logger"
"github.com/CafeKetab/user/pkg/rdbms"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
type Server struct{}
func (cmd Server) Command(trap chan os.Signal) *cobra.Command {
run := func(_ *cobra.Command, _ []string) {
cmd.main(config.Load(true), trap)
}
return &cobra.Command{
Use: "server",
Short: "run user server",
Run: run,
}
}
func (cmd *Server) main(cfg *config.Config, trap chan os.Signal) {
logger := logger.NewZap(cfg.Logger)
rdbms, err := rdbms.NewPostgres(cfg.RDBMS)
if err != nil {
logger.Panic("Error creating rdbms database", zap.Error(err))
}
repo := repository.New(logger, rdbms)
authGrpcClient := grpc.NewAuthClient(cfg.GRPC, logger)
server := http.New(cfg.HTTP, logger, repo, authGrpcClient)
go server.Serve()
// Keep this at the bottom of the main function
field := zap.String("signal trap", (<-trap).String())
logger.Info("exiting by receiving a unix signal", field)
}