-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (79 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
package main
import (
"api/api"
"api/db"
"context"
"flag"
"fmt"
"log"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const userColl = "users"
var config = fiber.Config{
ErrorHandler: api.ErrorHandler,
}
func main() {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(db.DBURI))
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{
Hotel: hotelStore,
Room: roomStore,
User: userStore,
Booking: bookingStore,
}
hotelHandler = api.NewHotelHandler(store)
roomHandler = api.NewRoomHandler(store)
bookingHandler = api.NewBookingHandler(store)
userHandler = api.NewUserHandler(userStore)
authHandler = api.NewAuthHandler(userStore)
app = fiber.New(config)
auth = app.Group("/api")
apiv1 = app.Group("/api/v1", api.JWTAuthentication(userStore))
admin = apiv1.Group("/admin", api.AdminAuth)
)
fmt.Println(client)
listenAddr := flag.String("listenAddr", ":5000", "Listen address of API server")
flag.Parse()
if err != nil {
panic(err)
}
// A handler should only do:
// - serialization of incoming request (JSON)
// - do some data fetching from db
// - call some buisness logic
// - return the data back to the user
// auth handlers
auth.Post("/auth", authHandler.HandleAuthentication)
// Versioned api route
// user handlers
apiv1.Post("/user", userHandler.HandlePostUser)
apiv1.Get("/user", userHandler.HandleGetUsers)
apiv1.Get("/user/:id", userHandler.HandleGetUser)
apiv1.Delete("/user/:id", userHandler.HandleDeleteUser)
apiv1.Put("/user/:id", userHandler.HandlePutUser)
// hotel handlers
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)
// admin handlers
admin.Get("/bookings", bookingHandler.HandleGetBookings)
// booking handlers
apiv1.Get("/booking/:id", bookingHandler.HandleGetBooking)
apiv1.Get("/booking/:id/cancel", bookingHandler.HandleCancelingBooking)
app.Listen(*listenAddr)
}
func handleFoo(c *fiber.Ctx) error {
return c.JSON(map[string]string{"msg": "working fine"})
}