-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (128 loc) · 2.78 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/codegangsta/negroni"
)
type filterJson struct {
Name string
Patterns []string
}
type filterListJson struct {
Name string
FilterList []string
}
type filter struct {
name string
patterns []string
filterChannel chan string
returnChannel chan filterResult
broker broker
}
type filterList struct {
name string
list []filter
}
type filterResult struct {
Name string
Result bool
}
type categorizer struct {
filters []filter
broker broker
}
func NewCategorizer() Categorizer {
meatFilter := filter{
name: "meat",
patterns: []string{"chuck", "mignon", "roast", "steak"},
filterChannel: make(chan string),
}
beefFilter := filter{
name: "beef",
patterns: []string{"london broil", "chuck", "tri-tip", "beef"},
filterChannel: make(chan string),
}
b := broker{filterChannels: make([]chan string, 0)}
return &categorizer{filter: []filter{meatFilter, beefFilter}, broker: b}
}
func (c *categorizer) Initialize() {
for _, f := range c.filters {
c.broker.AddFilter(f)
}
}
type broker struct {
filterChannels []chan string
currentInProcess int
}
func (b *broker) AddFilter(f filter) {
b.filterChannels = append(b.filterChannels, f.filterChannel)
}
type categoryResult struct {
Item string
TagList []string
}
func (b *broker) Filter(item string) {
for _, c := range b.filterChannels {
fmt.Println("about to send ", item)
c <- item
b.currentInProcess++
}
}
func (f *filter) ListenOnChannel() {
go func() {
select {
case c := <-f.filterChannel:
result := matchPattern(c, f.patterns)
f.returnChannel <- filterResult{Name: f.name, Result: result}
}
}()
}
func (f *filter) setFilterChan(fc chan string) {
f.filterChannel = fc
}
func matchPattern(s string, patterns []string) bool {
for _, p := range patterns {
if strings.Contains(strings.ToLower(s), strings.ToLower(p)) {
return true
}
}
return false
}
func Serve() {
mux := http.NewServeMux()
mux.Handle("/categorize", c)
n := negroni.Classic()
n.UseHandler(mux)
n.Run(":3001")
}
func (c *categorizer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
returnChan := make(chan filterResult)
meatFilter.ListenOnChannel()
beefFilter.ListenOnChannel()
item := r.FormValue("item")
b.Filter(item)
tagList := make([]string, 0)
for {
select {
case result := <-returnChan:
tagList = append(tagList, result.Name)
b.currentInProcess--
if b.currentInProcess == 0 {
fmt.Printf("the tag list is %v\n", tagList)
b, err := json.Marshal(&categoryResult{Item: item, TagList: tagList})
if err != nil {
fmt.Println("oh no")
return
}
w.Write(b)
return
}
}
}
}
func main() {
c := &categorizer{}
c.Serve()
}