-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
209 lines (179 loc) · 4.54 KB
/
routes.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"log"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
func setupFiber() {
app := fiber.New()
app.Use(cors.New())
app.Use(recover.New())
// app.Use(jsonFilter)
setupRoutes(app)
log.Fatal(app.Listen(":3009"))
}
func setupRoutes(app *fiber.App) {
app.Get("/ping", ping)
app.Post("/jobs/create", createJob)
app.Post("/jobs/remove", removeJobApi)
app.Post("/jobs/update", updateAJob)
app.Get("/jobs/list", listJobs)
}
func ping(c *fiber.Ctx) error {
c.SendString("Bathike Unna!")
return nil
}
func jsonFilter(c *fiber.Ctx) error {
if c.Is("json") {
return c.Next()
}
return send400Response(c)
}
func createJob(c *fiber.Ctx) error {
type PMeta struct {
Key string `json:"key"`
Value string `json:"value"`
}
payload := struct {
JobName string `json:"jobName"`
JobTime string `json:"time"`
ActionType int8 `json:"actionType"`
Meta []PMeta `json:"meta"`
}{}
if err := c.BodyParser(&payload); err != nil {
return send400Response(c)
}
if len(payload.JobName) < 1 || len(payload.JobTime) < 1 || payload.ActionType < 1 {
return send400Response(c)
}
jobTime, err := time.Parse(time.RFC3339, payload.JobTime)
if err != nil {
return send400Response(c)
}
var newJob NewJob
newJob.JobName = payload.JobName
newJob.JobTime = uint64(jobTime.Unix())
newJob.ActionType = payload.ActionType
for i := 0; i < len(payload.Meta); i++ {
var newMeta JobMeta
newMeta.Key = payload.Meta[i].Key
newMeta.Value = payload.Meta[i].Value
newJob.Meta = append(newJob.Meta, newMeta)
}
collectionJobs := getCollection(getJobsCollectionName())
_, insertErr := collectionJobs.InsertOne(context.TODO(), newJob)
if insertErr != nil {
return send500Response(c)
}
c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"status": "success",
})
return nil
}
func removeJobApi(c *fiber.Ctx) error {
payload := struct {
JobId string `json:"jobId"`
}{}
if err := c.BodyParser(&payload); err != nil {
return send400Response(c)
}
if len(payload.JobId) < 1 {
return send400Response(c)
}
objId, err := primitive.ObjectIDFromHex(payload.JobId)
if err != nil {
return send400Response(c)
}
collectionJobs := getCollection(getJobsCollectionName())
_, removeErr := collectionJobs.DeleteOne(context.TODO(), bson.M{"_id": objId})
if removeErr != nil {
return send500Response(c)
}
c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"status": "success",
})
return nil
}
func listJobs(c *fiber.Ctx) error {
collectionJobs := getCollection(getJobsCollectionName())
findOptions := options.Find()
skip, _ := strconv.ParseInt(c.Params("skip", "0"), 10, 64)
limit, _ := strconv.ParseInt(c.Params("limit", "20"), 10, 64)
findOptions.SetSkip(skip)
findOptions.SetLimit(limit)
cur, findErr := collectionJobs.Find(context.TODO(), bson.D{{}})
if findErr != nil {
return send500Response(c)
}
var jobs []*Job
// Iterate through the cursor
for cur.Next(context.TODO()) {
var elem Job
err := cur.Decode(&elem)
if err != nil {
continue
}
jobs = append(jobs, &elem)
}
c.JSON(fiber.Map{
"jobs": jobs,
})
return nil
}
func updateAJob(c *fiber.Ctx) error {
type PMeta struct {
Key string `json:"key"`
Value string `json:"value"`
}
payload := struct {
JobId string `json:"jobId"`
JobName string `json:"jobName"`
JobTime string `json:"time"`
ActionType int8 `json:"actionType"`
Meta []PMeta `json:"meta"`
}{}
if err := c.BodyParser(&payload); err != nil {
return send400Response(c)
}
if len(payload.JobId) < 1 {
return send400Response(c)
}
collectionJobs := getCollection(getJobsCollectionName())
id, _ := primitive.ObjectIDFromHex(payload.JobId)
updateQinner := bson.M{
"job_name": payload.JobName,
"action_type": payload.ActionType,
"job_time": payload.JobTime,
"meta": payload.Meta,
}
if len(payload.JobName) < 1 {
delete(updateQinner, "job_name")
}
if payload.ActionType < 1 {
delete(updateQinner, "action_type")
}
if len(payload.JobTime) < 1 {
delete(updateQinner, "job_time")
}
if len(payload.Meta) < 1 {
delete(updateQinner, "meta")
}
updateQ := bson.M{
"$set": updateQinner,
}
_, err := collectionJobs.UpdateOne(context.TODO(), bson.M{"_id": id}, updateQ)
if err != nil {
return send500Response(c)
}
c.Status(fiber.StatusAccepted).JSON(fiber.Map{
"status": "success",
})
return nil
}