-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (86 loc) · 2.49 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
package main
import (
"context"
"greens-basket/utils"
"os"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
controller "greens-basket/controllers"
"greens-basket/jwt"
"greens-basket/repositories"
)
type Application struct {
Fiber *fiber.App
Config *utils.Config
Client *mongo.Client
}
func (app *Application) Init() {
// // Create logger for writing information and error messages.
// app.Fiber.Use(func(c *fiber.Ctx) error {
// c.Locals(utils.MongoClient, app.Client) // Store the client in the context
// return c.Next()
// })
app.registerRoutes()
}
func (app *Application) registerRoutes() {
// Register handler functions.
jFactory := &jwt.JWTFactory{
Config: app.Config,
}
db := app.Client.Database(utils.Database)
userRepo := repositories.NewUserRepository(db)
authController := &controller.AuthController{
JFactor: jFactory,
UserRepo: userRepo,
}
userController := &controller.UserController{
UserRepo: userRepo,
}
authGroup := app.Fiber.Group("/auth")
authGroup.Post("/verify-pincode", jFactory.Authenticate(utils.TempToken, ""), authController.VerifyPinCode)
authGroup.Post("/authenticate", authController.Authenticate)
userGroup := app.Fiber.Group("/user")
userGroup.Get("/", jFactory.Authenticate(utils.AccessToken, utils.User), userController.Profile)
userGroup.Post("/", jFactory.Authenticate(utils.AccessToken, utils.User), userController.UpdateProfile)
//userGroup.Use(jwt.Protected(utils.AccessToken, utils.User)) // protecting group route with token and role
}
func main() {
config, err := utils.LoadConfig(".")
if err != nil {
log.Fatal().Err(err).Msg("cannot load config")
}
if config.Environment == "development" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
app := fiber.New()
//fiber.Use(SetCommonHeaders())
//serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(config.DBSource) //.SetServerAPIOptions(serverAPI)
// Create a new client and connect to the server
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
log.Fatal().Err(err).Msg("cannot load config")
panic(err)
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
ap := Application{
Fiber: app,
Config: &config,
Client: client,
}
ap.Init()
if err != nil {
panic(err)
}
e := app.Listen(":3000")
if e != nil {
panic(e)
}
}