-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
115 lines (85 loc) · 2.55 KB
/
service.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
// Copyright (c) 2013, Danilo Cabello.
// Use of this source code is governed by LICENSE file.
package blass
import (
"github.com/cabello/bloom"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"strconv"
)
var filters map[string]*bloom.Filter = make(map[string]*bloom.Filter)
func createFilter(w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
if _, ok := filters[name]; ok {
w.WriteHeader(http.StatusConflict)
return
}
parsedCapacity, _ := strconv.ParseInt(r.FormValue("capacity"), 10, 0)
capacity := int(parsedCapacity)
errorRate, _ := strconv.ParseFloat(r.FormValue("errorRate"), 64)
filters[name] = bloom.New(capacity, errorRate)
w.WriteHeader(http.StatusCreated)
}
func retrieveFilter(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["filterName"]
filter, ok := filters[name]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "{ \"capacity\": %+v, \"errorRate\": %+v }\n", filter.Capacity, filter.ErrorRate)
}
func deleteFilter(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["filterName"]
if _, ok := filters[name]; !ok {
w.WriteHeader(http.StatusNotFound)
return
}
delete(filters, name)
w.WriteHeader(http.StatusNoContent)
}
func createEntry(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filterName := vars["filterName"]
entryName := r.FormValue("name")
filter, ok := filters[filterName]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
filter.Add([]byte(entryName))
w.WriteHeader(http.StatusCreated)
}
func retrieveEntry(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
filterName := vars["filterName"]
entryName := vars["entryName"]
filter, ok := filters[filterName]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
if !filter.Contains([]byte(entryName)) {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
return
}
func ListenAndServe(address string) {
r := mux.NewRouter()
r.HandleFunc("/v1/filters", createFilter).Methods("POST")
r.HandleFunc("/v1/filters/{filterName}", retrieveFilter).Methods("GET")
r.HandleFunc("/v1/filters/{filterName}", deleteFilter).Methods("DELETE")
r.HandleFunc("/v1/filters/{filterName}/entries", createEntry).Methods("POST")
r.HandleFunc("/v1/filters/{filterName}/entries/{entryName}", retrieveEntry).Methods("GET")
http.Handle("/", r)
log.Print("Server starting on ", address)
http.ListenAndServe(address, nil)
}