-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
91 lines (80 loc) · 2.09 KB
/
main.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
package main
import (
"os"
"log"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/krakendio/bloomfilter/v2/rpc/client"
)
type RevokeRequest struct {
Key string `json:"key"`
Subject string `json:"subject"`
}
func StatusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status": 200, "message": "OK"}`))
}
type check struct {
Status int `json:"status"`
Subject string `json:"subject"`
Exists bool `json:"exists"`
}
func CheckHandler(w http.ResponseWriter, r *http.Request) {
bloomServer, err := client.New(os.Getenv("BLOOM_SERVER"))
if err != nil {
panic(err)
}
defer bloomServer.Close()
var req RevokeRequest
err2 := json.NewDecoder(r.Body).Decode(&req)
if err2 != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
subject := req.Key + "-" + req.Subject
exists, _ := bloomServer.Check([]byte(subject))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(check{
Status: 200,
Subject: subject,
Exists: exists,
})
}
func AddHandler(w http.ResponseWriter, r *http.Request) {
bloomServer, err := client.New(os.Getenv("BLOOM_SERVER"))
if err != nil {
panic(err)
}
defer bloomServer.Close()
var req RevokeRequest
err2 := json.NewDecoder(r.Body).Decode(&req)
if err2 != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
subject := req.Key + "-" + req.Subject
err = bloomServer.Add([]byte(subject))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println("Added:", subject)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"status": 201, "message": "Added"}`))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", StatusHandler).Methods("GET")
r.HandleFunc("/check", CheckHandler).Methods("POST")
r.HandleFunc("/add", AddHandler).Methods("POST")
srv := &http.Server{
Addr: ":3005",
Handler: r,
}
log.Println("Starting Revoker on port 3005")
log.Fatal(srv.ListenAndServe())
}