-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (95 loc) · 2.83 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
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
//go:generate swagger generate spec
// Documentation of our Books API
//
// Simple Gin API
//
// Schemes: http
// Host: localhost:8080
// BasePath: /
// Version: 1.0.0
// Contact: Test User <[email protected]> http://github.com/
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Security:
// - basic
//
// SecurityDefinitions:
// basic:
// type: basic
//
// swagger:meta
package main
import (
"context"
"fmt"
"log"
"github.com/PauloPortugal/gin-gonic-rest-mongodb/datastore"
"github.com/PauloPortugal/gin-gonic-rest-mongodb/handlers"
"github.com/PauloPortugal/gin-gonic-rest-mongodb/middleware"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const pathToTemplates = "web/templates/*"
func main() {
ctx := context.Background()
cfg := readConfig()
mongoDBClient := setupMongoDBClient(ctx, cfg)
mongoBooksClient := datastore.NewBooksClient(mongoDBClient, cfg)
mongoUsersClient := datastore.NewUsersClient(mongoDBClient, cfg)
mongoBooksClient.InitBooks(ctx)
mongoUsersClient.InitUsers(ctx)
redisClient := setupRedisClient(ctx, cfg)
redisBooksClient := datastore.NewRedisClient(redisClient, cfg)
m := middleware.Client{}
cs := datastore.CookieStoreClient{Config: cfg}
router := gin.Default()
if err := handlers.Setup(ctx, cfg, router, pathToTemplates, mongoBooksClient, mongoUsersClient, redisBooksClient, m, cs).Run(); err != nil {
return
}
}
func readConfig() *viper.Viper {
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
log.Fatal(fmt.Errorf("fatal error config file: %w", err))
}
return viper.GetViper()
}
func setupMongoDBClient(ctx context.Context, cfg *viper.Viper) *mongo.Client {
uri := fmt.Sprintf("mongodb://%s:%s@%s/test?authSource=admin",
cfg.GetString("mongodb.dbuser"),
cfg.GetString("mongodb.dbpassword"),
cfg.GetString("mongodb.dbhost"))
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
log.Fatal(fmt.Errorf("could not connect to databse: %w", err))
}
if err = client.Ping(ctx, readpref.Primary()); err != nil {
log.Fatal(fmt.Errorf("could not ping databse: %w", err))
}
log.Println("Connected to MongoDB")
return client
}
func setupRedisClient(ctx context.Context, cfg *viper.Viper) *redis.Client {
redisOpt := &redis.Options{
Addr: fmt.Sprint(cfg.Get("redis.host")),
Password: "",
DB: 0,
}
client := redis.NewClient(redisOpt)
status := client.Ping(ctx)
log.Printf("redis status: %q", status)
return client
}