-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (131 loc) · 4.19 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"os"
"log"
"github.com/adarsh-jaiss/GO-Hotel-reservation/api"
"github.com/adarsh-jaiss/GO-Hotel-reservation/db"
"github.com/adarsh-jaiss/GO-Hotel-reservation/middleware"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
// dbname = "Hotel-reservation"
)
func Init() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
// for debugging purposes only
// for _, envVar := range os.Environ() {
// fmt.Println(envVar)
// }
}
func main() {
// connecting with Mongo DB
Init()
mongoEndPoint := os.Getenv("MONGO_DB_URL")
// for debugging purposes only
// fmt.Println("MONGO_DB_URL:", mongoEndPoint)
// if mongoEndPoint == "" {
// log.Fatal("MONGO_DB_URL is not set in the environment")
// }
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoEndPoint))
if err != nil {
log.Fatal(err)
}
// handlers initialization
var (
hotelStore = db.NewMongoHotelStore(client)
roomStore = db.NewMongoRoomStore(client, hotelStore)
userStore = db.NewMongoUserStore(client)
bookingStore = db.NewMongoBookingStore(client)
store = &db.Store{
User: userStore,
Hotel: hotelStore,
Room: roomStore,
Booking: bookingStore,
}
hotelHandler = api.NewHotelHandler(store)
roomHandler = api.NewRoomHandler(store)
userHandler = api.NewUserhandler(userStore)
authHandler = api.NewAuthHandler(userStore)
BookingHandler = api.NewBookingHandler(store)
// Routes Grouping
app = fiber.New(config)
auth = app.Group("api")
appV1 = app.Group("api/v1", middleware.JWTAuthentication(userStore))
// appV1 = app.Group("api/v1")
admin = appV1.Group("/admin", middleware.AdminAuth)
)
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET,POST,PUT,DELETE",
AllowHeaders: "Content-Type, X-Api-Token",
AllowCredentials: true,
AllowOriginsFunc: func(origin string) bool {
return origin == "http://localhost:3000"
},
}))
auth.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET,POST,PUT,DELETE",
AllowHeaders: "Content-Type, X-Api-Token",
AllowCredentials: true,
AllowOriginsFunc: func(origin string) bool {
return origin == "http://localhost:3000"
},
}))
appV1.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowMethods: "GET,POST,PUT,DELETE",
AllowHeaders: "Content-Type, X-Api-Token",
AllowCredentials: true,
AllowOriginsFunc: func(origin string) bool {
return origin == "http://localhost:3000"
},
}))
// Auth Handlers
auth.Post("/auth/signin", authHandler.HandleAuthenticate)
auth.Post("/auth/signup", userHandler.HandlePostUser)
auth.Get("/auth/user/:id", userHandler.HandlerUser)
// Versioned API routes
// This is user handlers
appV1.Get("/user", userHandler.HandlerUsers)
appV1.Get("/user/:id", userHandler.HandlerUser)
appV1.Delete("/user/:id", userHandler.HandleDeleteUser)
appV1.Patch("/user/:id", userHandler.HandlePutUser)
// This is hotel handlers
appV1.Get("/hotel", hotelHandler.HandleGetHotels)
appV1.Get("/hotel/:id/rooms", hotelHandler.HandleGetRooms)
appV1.Get("/hotel/:id", hotelHandler.HandleGetHotel)
// rooms handlers
appV1.Get("/room", roomHandler.HandleGetRooms)
appV1.Post("/room/:id/book", roomHandler.HandleBookRoom)
// Todo : cancel a booking
// TODO : GET ALL BOOKINGS BY A USER
// booking Handlers
// appV1.Get("user/bookings",BookingHandler.HandleGetBookings)
appV1.Get("/booking/:id", BookingHandler.HandleGetBooking)
appV1.Get("/booking/:id/cancel", BookingHandler.HandleUpdateBooking)
// admin handlers
admin.Get("/booking", BookingHandler.HandleGetBookings)
// listerAddr:= flag.String("listenAddr",":3000","This is the listen Address of the API Server")
listenAddr := os.Getenv("HTTP_LISTEN_ADDRESS")
if listenAddr == "" {
listenAddr = ":8000"
}
log.Fatal(app.Listen("0.0.0.0"+ listenAddr))
}
// central error handling
// var config = fiber.Config{
// ErrorHandler: func(c *fiber.Ctx, err error) error {
// return c.JSON(map[string]string{"error":err.Error()})
// },
// }
var config = fiber.Config{
ErrorHandler: api.ErrorHandler,
}