-
Notifications
You must be signed in to change notification settings - Fork 0
/
userDeletion.go
53 lines (43 loc) · 1.57 KB
/
userDeletion.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
package main
import (
"github.com/go-redis/redis/v8"
"log"
"net/http"
"net/http/httptest"
)
// listenForUserDeletion listens for user deletion messages published to the Redis 'user-delete' topic. When receiving
// a user deletion request, each authenticated service is called and asked to delete user data. Each authenticated
// services is required to implement and '/user-delete' endpoint and responsible for handling graceful user deletion.
// The message payload must be the ID of the user to delete.
func listenForUserDeletion() {
// There is no error because go-redis automatically reconnects on error
pubsub := rdb.Subscribe(rdbContext, "user-delete")
// Close the subscription when done
defer func(pubsub *redis.PubSub) {
err := pubsub.Close()
if err != nil {
log.Println("Error closing Redis subscription: ", err)
}
}(pubsub)
// Go channel which receives messages
ch := pubsub.Channel()
// Listen for user delete request on user-delete pubsub channel
for msg := range ch {
// log.Printf(msg.Channel, msg.Payload)
// Make call to each authenticated service to delete the user data
for _, service := range serviceProxies {
if service.Authenticated {
req, _ := http.NewRequest("DELETE", "/user-delete", nil)
// Assume the user ID is the message payload
req.Header.Set("id", msg.Payload)
// http response writer
w := httptest.NewRecorder()
service.ServeHTTP(w, req)
// TODO: Handle a failed user deletion
log.Printf("Response: %v", w.Result())
} else {
log.Println("Service is not authenticated: ", service.Name)
}
}
}
}