-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (64 loc) · 2.18 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
package main
import (
"context"
"log"
"os"
"github.com/gofiber/fiber/v3"
"github.com/joho/godotenv"
"github.com/mexirica/hotel-reservation/api"
"github.com/mexirica/hotel-reservation/db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var config = fiber.Config{
ErrorHandler: api.ErrorHandler,
}
func main() {
mongoEndpoint := os.Getenv("MONGO_DB_URL")
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoEndpoint))
if err != nil {
log.Fatal(err)
}
var (
hotelStore = db.NewMongoHotelStore(client)
roomStore = db.NewMongoRoomStore(client, hotelStore)
userStore = db.NewMongoUserStore(client)
bookingStore = db.NewMongoBookingStore(client)
store = &db.Store{
Hotel: hotelStore,
Room: roomStore,
User: userStore,
Booking: bookingStore,
}
userHandler = api.NewUserHandler(userStore)
hotelHandler = api.NewHotelHandler(store)
authHandler = api.NewAuthHandler(userStore)
roomHandler = api.NewRoomHandler(store)
bookingHandler = api.NewBookingHandler(store)
app = fiber.New(config)
auth = app.Group("/api")
apiv1 = app.Group("/api/v1", api.JWTAuthentication(userStore))
admin = apiv1.Group("/admin", api.AdminAuth)
)
auth.Post("/auth", authHandler.HandleAuthenticate)
apiv1.Get("/user/:id", userHandler.HandleGetUser)
apiv1.Put("/user/:id", userHandler.HandlePutUser)
apiv1.Delete("/user/:id", userHandler.HandleDeleteUser)
apiv1.Post("/user", userHandler.HandlePostUser)
apiv1.Get("/user", userHandler.HandleGetUsers)
apiv1.Get("/hotel", hotelHandler.HandleGetHotels)
apiv1.Get("/hotel/:id", hotelHandler.HandleGetHotel)
apiv1.Get("/hotel/:id/rooms", hotelHandler.HandleGetRooms)
apiv1.Get("/room", roomHandler.HandleGetRooms)
apiv1.Post("/room/:id/book", roomHandler.HandleBookRoom)
apiv1.Get("/booking/:id", bookingHandler.HandleGetBooking)
apiv1.Get("/booking/:id/cancel", bookingHandler.HandleCancelBooking)
admin.Get("/booking", bookingHandler.HandleGetBookings)
listenAddr := os.Getenv("HTTP_LISTEN_ADDRESS")
app.Listen(listenAddr)
}
func init() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
}