-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (102 loc) · 3.41 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
package main
import (
"context"
"github.com/Ndeta100/CamHotelConnect/api"
"github.com/Ndeta100/CamHotelConnect/db"
"github.com/Ndeta100/CamHotelConnect/utils"
"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"
"log"
"os"
)
const userCollection = "users"
// Configurations
// 1. MongoDB endpoint
// 2. Listen address of http server
// 3. JWT secret
// 4. MongoDBNAME
var config = fiber.Config{
ErrorHandler: api.ErrorHandler,
}
func main() {
mongoUri := os.Getenv("MONGO_DB_URL")
cloudName := os.Getenv("CLOUDINARY_CLOUD_NAME")
cloudKey := os.Getenv("CLOUDINARY_API_KEY")
cloudSecret := os.Getenv("CLOUDINARY_API_SECRET")
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri))
if mongoUri == "" || cloudName == "" || cloudKey == "" || cloudSecret == "" {
log.Fatal("Error loading environment variables")
}
imageUploader, err := utils.NewCloudinaryUploader(cloudName, cloudKey, cloudSecret)
if err != nil {
log.Fatalf("error initialing cloudinary uploader %v", err)
}
//handlers initialization
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, imageUploader)
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")
admin = apiV1.Group("/admin", api.AdminAuth)
)
// Use CORS middleware
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:3000", // Change this to restrict to specific origins
AllowMethods: "GET,POST,PUT,DELETE",
AllowHeaders: "Content-Type, X-Api-Token",
AllowCredentials: true,
}))
// Define unauthenticated routes
auth.Post("/auth", authHandler.HandleAuth)
apiV1.Post("/user", userHandler.HandlePostUser)
//Hotel public routes
apiV1.Get("/hotel", hotelHandler.HandleGetHotels)
apiV1.Get("/hotel/:id", hotelHandler.HandleGetHotel)
apiV1.Get("hotel/:id/rooms", hotelHandler.HandleGetRooms)
// Use the JWT authentication middleware
apiV1.Use(api.JWTAuthentication(userStore))
//Versioned api routes
//User handlers
apiV1.Get("/user", userHandler.HandleGetUsers)
apiV1.Put("user/:id", userHandler.HandlePutUser)
apiV1.Get("/user/:id", userHandler.HandleGetUser)
apiV1.Delete("/user/:id", userHandler.HandleDeleteUser)
//Hotel handlers
apiV1.Post("/hotel", hotelHandler.HandleAddHotel)
//room handlers
apiV1.Get("/room", roomHandler.HandleGetRooms)
apiV1.Post("/room/:id/book", roomHandler.HandleBookRoom)
//TODO: cancel a booking
//booking handlers
apiV1.Get("/booking/:id", bookingHandler.HandleGetBooking)
apiV1.Get("booking/:id/cancel", bookingHandler.HandleCancelBooking)
//admin handlers
admin.Get("/booking", bookingHandler.HandleGetBookings)
listenAddr := os.Getenv("HTTP_LISTEN_ADDRESS")
err = app.Listen(listenAddr)
if err != nil {
return
}
}
func init() {
if err := godotenv.Load(); err != nil {
log.Fatal(err)
}
}