-
Notifications
You must be signed in to change notification settings - Fork 145
/
router.go
85 lines (72 loc) · 2.54 KB
/
router.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
package router
import (
"io"
"net/http"
log "github.com/sirupsen/logrus"
"fmt"
"github.com/gorilla/mux"
sns "github.com/p4tin/goaws/app/gosns"
sqs "github.com/p4tin/goaws/app/gosqs"
)
// New returns a new router
func New() http.Handler {
r := mux.NewRouter()
r.HandleFunc("/", actionHandler).Methods("GET", "POST")
r.HandleFunc("/{account}", actionHandler).Methods("GET", "POST")
r.HandleFunc("/queue/{queueName}", actionHandler).Methods("GET", "POST")
r.HandleFunc("/{account}/{queueName}", actionHandler).Methods("GET", "POST")
r.HandleFunc("/SimpleNotificationService/{id}.pem", pemHandler).Methods("GET")
r.HandleFunc("/health", health).Methods("GET")
return r
}
var routingTable = map[string]http.HandlerFunc{
// SQS
"ListQueues": sqs.ListQueues,
"CreateQueue": sqs.CreateQueue,
"GetQueueAttributes": sqs.GetQueueAttributes,
"SetQueueAttributes": sqs.SetQueueAttributes,
"SendMessage": sqs.SendMessage,
"SendMessageBatch": sqs.SendMessageBatch,
"ReceiveMessage": sqs.ReceiveMessage,
"DeleteMessage": sqs.DeleteMessage,
"DeleteMessageBatch": sqs.DeleteMessageBatch,
"GetQueueUrl": sqs.GetQueueUrl,
"PurgeQueue": sqs.PurgeQueue,
"DeleteQueue": sqs.DeleteQueue,
"ChangeMessageVisibility": sqs.ChangeMessageVisibility,
// SNS
"ListTopics": sns.ListTopics,
"CreateTopic": sns.CreateTopic,
"DeleteTopic": sns.DeleteTopic,
"Subscribe": sns.Subscribe,
"ConfirmSubscription": sns.ConfirmSubscription,
"SetSubscriptionAttributes": sns.SetSubscriptionAttributes,
"GetSubscriptionAttributes": sns.GetSubscriptionAttributes,
"ListSubscriptionsByTopic": sns.ListSubscriptionsByTopic,
"ListSubscriptions": sns.ListSubscriptions,
"Unsubscribe": sns.Unsubscribe,
"Publish": sns.Publish,
}
func health(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
fmt.Fprint(w, "OK")
}
func actionHandler(w http.ResponseWriter, req *http.Request) {
log.WithFields(
log.Fields{
"action": req.FormValue("Action"),
"url": req.URL,
}).Debug("Handling URL request")
fn, ok := routingTable[req.FormValue("Action")]
if !ok {
log.Println("Bad Request - Action:", req.FormValue("Action"))
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "Bad Request")
return
}
http.HandlerFunc(fn).ServeHTTP(w, req)
}
func pemHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(sns.PemKEY)
}