-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.go
115 lines (102 loc) · 3.05 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package commands
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/imrenagicom/demo-app/course/catalog"
"github.com/imrenagicom/demo-app/course/server/apiserver"
"github.com/imrenagicom/demo-app/internal/config"
"github.com/imrenagicom/demo-app/internal/instrumentation"
"github.com/imrenagicom/demo-app/internal/postgres"
"github.com/imrenagicom/demo-app/internal/redis"
"github.com/imrenagicom/demo-app/internal/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
type serverOpts struct {
envPrefix string
}
func newServer(opts *opts) *cobra.Command {
serverOpts := &serverOpts{}
command := &cobra.Command{
Use: "server",
Short: "server subcommands",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
},
}
command.AddCommand(
newServerStart(opts, serverOpts),
newServerSeed(opts, serverOpts),
)
command.PersistentFlags().StringVar(&serverOpts.envPrefix, "env-prefix", "COURSE_SERVER", "config prefix")
return command
}
func newServerStart(opts *opts, serverOpts *serverOpts) *cobra.Command {
command := &cobra.Command{
Use: "start",
RunE: func(c *cobra.Command, args []string) error {
conf, err := config.NewServer(opts.configPath, serverOpts.envPrefix)
if err != nil {
log.Fatal().Err(err).Msg("unable to load config file")
}
logFn := instrumentation.InitializeLogger(conf.Log)
defer logFn()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
go func() {
oscall := <-ch
log.Warn().Msgf("system call:%+v", oscall)
cancel()
}()
log.Debug().Msgf("running migration on %s", opts.migrationDir)
if err := postgres.Migrate(opts.migrationDir, conf.DB.DatabaseUrl(), true); err != nil {
log.Fatal().Err(err).Msg("unable to run migration")
}
server := apiserver.NewServer(apiserver.ServerOpts{
Config: conf,
Clients: &util.Clients{
DB: postgres.NewSQLx(conf.DB),
Redis: redis.New(conf.Redis),
},
})
return server.Run(ctx)
},
}
return command
}
func newServerSeed(opts *opts, serverOpts *serverOpts) *cobra.Command {
command := &cobra.Command{
Use: "seed",
Short: "seed db",
RunE: func(c *cobra.Command, args []string) error {
conf, err := config.NewServer(opts.configPath, serverOpts.envPrefix)
if err != nil {
log.Fatal().Err(err).Msg("unable to load config file")
}
logFn := instrumentation.InitializeLogger(conf.Log)
defer logFn()
ctx := log.With().Logger().WithContext(context.Background())
ctx, cancel := context.WithCancel(ctx)
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
go func() {
oscall := <-ch
log.Warn().Msgf("system call:%+v", oscall)
cancel()
}()
clients := &util.Clients{
DB: postgres.NewSQLx(conf.DB),
}
concertStore := catalog.NewStore(clients.DB, clients.Redis)
catalogSvc := catalog.NewService(concertStore, clients.DB)
return catalogSvc.Seed(ctx)
},
}
return command
}