-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
46 lines (36 loc) · 1.24 KB
/
serve.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
package cmd
import (
"context"
"github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/irvingdinh/osgarth-api/src/component/config"
"github.com/irvingdinh/osgarth-api/src/component/logger"
"github.com/irvingdinh/osgarth-api/src/component/repository"
"github.com/irvingdinh/osgarth-api/src/http/handler"
"github.com/irvingdinh/osgarth-api/src/http/server"
)
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start HTTP server on the predefined port",
Run: func(cmd *cobra.Command, args []string) {
log := logger.CToL(context.Background(), "serveCmd")
ctx := context.Background()
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(config.GetDatabaseConfig().URI))
if err != nil {
log.Fatalf("Failed to connect to MongoDB with error: %s", err.Error())
}
defer func() {
if err := mongoClient.Disconnect(ctx); err != nil {
log.Fatalf("Failed to disconnect from MongoDB with error: %s", err.Error())
}
}()
repositoryClient := repository.New(mongoClient)
s := server.New(
handler.New(repositoryClient),
)
if err := s.Start(); err != nil {
log.WithError(err).Fatalf("Failed to start HTTP server with error: %s", err.Error())
}
},
}