-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
112 lines (95 loc) · 2.79 KB
/
handlers.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
"github.com/Hands-On-Restful-Web-services-with-Go/chapter9/longRunningTaskV2/models"
"github.com/go-redis/redis"
"github.com/google/uuid"
"github.com/streadway/amqp"
)
// JobServer holds handler functions
type JobServer struct {
Queue amqp.Queue
Channel *amqp.Channel
Conn *amqp.Connection
redisClient *redis.Client
}
func (s *JobServer) asyncDBHandler(w http.ResponseWriter, r *http.Request) {
jobID, err := uuid.NewRandom()
queryParams := r.URL.Query()
// Ex: client_time: 1569174071
unixTime, err := strconv.ParseInt(queryParams.Get("client_time"), 10, 64)
clientTime := time.Unix(unixTime, 0)
handleError(err, "Error while converting client time")
jsonBody, err := json.Marshal(models.Job{ID: jobID,
Type: "A",
ExtraData: models.Log{ClientTime: clientTime},
})
handleError(err, "JSON body creation failed")
if s.publish(jsonBody) == nil {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(jsonBody)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func (s *JobServer) asyncCallbackHandler(w http.ResponseWriter, r *http.Request) {
jobID, err := uuid.NewRandom()
jsonBody, err := json.Marshal(models.Job{ID: jobID,
Type: "B",
ExtraData: "",
})
handleError(err, "JSON body creation failed")
if s.publish(jsonBody) == nil {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(jsonBody)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func (s *JobServer) asyncMailHandler(w http.ResponseWriter, r *http.Request) {
jobID, err := uuid.NewRandom()
jsonBody, err := json.Marshal(models.Job{ID: jobID,
Type: "C",
ExtraData: "",
})
handleError(err, "JSON body creation failed")
err = s.publish(jsonBody)
if err == nil {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(jsonBody)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
func (s *JobServer) publish(jsonBody []byte) error {
message := amqp.Publishing{
ContentType: "application/json",
Body: jsonBody,
}
err := s.Channel.Publish(
"", // exchange
queueName, // routing key(Queue)
false, // mandatory
false, // immediate
message,
)
handleError(err, "Error while generating JobID")
return err
}
func (s *JobServer) statusHandler(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
// fetch UUID from query
uuid := queryParams.Get("uuid")
w.Header().Set("Content-Type", "application/json")
jobStatus := s.redisClient.Get(uuid)
status := map[string]string{"ID": uuid, "Status": jobStatus.Val()}
response, err := json.Marshal(status)
handleError(err, "Cannot create response for client")
w.Write(response)
}