-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (63 loc) · 1.45 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
package main
import (
"context"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/pyrolass/hotel-reservation-go/common"
"github.com/pyrolass/hotel-reservation-go/routes"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var config = fiber.Config{
ErrorHandler: func(c *fiber.Ctx, err error) error {
if apiError, ok := err.(common.ApiError); ok {
return c.Status(apiError.Code).JSON(
map[string]any{
"error": apiError.Message,
},
)
}
return c.Status(500).JSON(
map[string]any{
"error": err.Error(),
},
)
},
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
uri := os.Getenv("MONGODB_URI")
if uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
println("Connected to MongoDB!")
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
app := fiber.New(config)
port := ":3000"
router := app.Group("api/v1")
router.Get(
"/ping",
func(c *fiber.Ctx) error {
return c.JSON(
map[string]any{
"message": "pong",
},
)
},
)
routes.UserRoutes(router, client)
routes.HotelRoutes(router, client)
app.Listen(port)
}