-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
52 lines (42 loc) · 1.31 KB
/
Copy pathhandlers.go
File metadata and controls
52 lines (42 loc) · 1.31 KB
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
package main
import (
"fmt"
"io"
"net/http"
)
type speechProcessor interface {
ProcessText(text io.Reader) error
GenerateRandomText(maxWords uint) string
}
// Handlers contains all the shared resources accross handlers
type Handlers struct {
processor speechProcessor
maxWords uint
}
const textPlainContentType = "text/plain"
// Learn will parse the body of text on the request and process the ngrams
func (h *Handlers) Learn(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
// If it's not text plain, don't process it
if req.Header.Get("Content-Type") != textPlainContentType {
http.Error(w, "Invalid content type", http.StatusUnprocessableEntity)
return
}
if err := h.processor.ProcessText(req.Body); err != nil {
http.Error(w, fmt.Sprintf("Error processing text: %v", err), http.StatusUnprocessableEntity)
return
}
}
// Generate will return randomly-generated text based on all the ngrams that
// have been learned since starting the program.
func (h *Handlers) Generate(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(w, "Invalid method", http.StatusMethodNotAllowed)
return
}
var text = h.processor.GenerateRandomText(h.maxWords)
w.Write([]byte(text))
}