1
1
package handlers
2
2
3
3
import (
4
- "context"
5
4
"os"
6
5
"time"
7
6
8
7
"github.com/gofiber/fiber/v2"
9
- "go.mongodb.org/mongo-driver/bson "
8
+ "github.com/google/uuid "
10
9
"go.mongodb.org/mongo-driver/mongo"
11
10
)
12
11
@@ -34,14 +33,18 @@ func (h *Handler) RootHandler(c *fiber.Ctx) error {
34
33
35
34
// ApiV1Handler handles the "/api/v1" endpoint
36
35
func (h * Handler ) ApiV1Handler (c * fiber.Ctx ) error {
37
- return c .JSON (fiber.Map {
38
- "message" : "Welcome to API v1" ,
39
- "endpoints" : []string {
40
- "/api/v1/health" ,
41
- "/api/v1/users" ,
42
- "/api/v1/tasks" ,
43
- },
44
- })
36
+ return c .JSON (fiber.Map {
37
+ "status" : "active" ,
38
+ "message" : "Welcome to NAQA API v1" ,
39
+ "version" : "1.0.0" ,
40
+ "env" : os .Getenv ("APP_ENV" ),
41
+ "server_time" : time .Now ().Format (time .RFC3339 ),
42
+ "request_id" : c .Get ("X-Request-ID" , uuid .New ().String ()),
43
+ "endpoints" : []string {
44
+ "/api/v1/stocks" ,
45
+ "/api/v1/health" ,
46
+ },
47
+ })
45
48
}
46
49
47
50
// HealthCheckHandler handles the health check endpoint
@@ -58,51 +61,3 @@ func (h *Handler) HandleSomething(c *fiber.Ctx) error {
58
61
"message" : "Handler example" ,
59
62
})
60
63
}
61
-
62
- // GetUsersHandler handles the /api/v1/users endpoint
63
- func (h * Handler ) GetUsersHandler (c * fiber.Ctx ) error {
64
- collection := h .db .Collection ("users" )
65
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
66
- defer cancel ()
67
-
68
- cursor , err := collection .Find (ctx , bson.M {})
69
- if err != nil {
70
- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
71
- "error" : "Error fetching users" ,
72
- })
73
- }
74
- defer cursor .Close (ctx )
75
-
76
- var users []fiber.Map
77
- if err := cursor .All (ctx , & users ); err != nil {
78
- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
79
- "error" : "Error parsing users data" ,
80
- })
81
- }
82
-
83
- return c .JSON (users )
84
- }
85
-
86
- // GetTasksHandler handles the /api/v1/tasks endpoint
87
- func (h * Handler ) GetTasksHandler (c * fiber.Ctx ) error {
88
- collection := h .db .Collection ("tasks" )
89
- ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
90
- defer cancel ()
91
-
92
- cursor , err := collection .Find (ctx , bson.M {})
93
- if err != nil {
94
- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
95
- "error" : "Error fetching tasks" ,
96
- })
97
- }
98
- defer cursor .Close (ctx )
99
-
100
- var tasks []fiber.Map
101
- if err := cursor .All (ctx , & tasks ); err != nil {
102
- return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
103
- "error" : "Error parsing tasks data" ,
104
- })
105
- }
106
-
107
- return c .JSON (tasks )
108
- }
0 commit comments