-
Notifications
You must be signed in to change notification settings - Fork 1
/
nlpd.go
45 lines (38 loc) · 888 Bytes
/
nlpd.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/353solutions/nlp"
)
func main() {
http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/tokenize", tokenizeHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
}
func tokenizeHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("can't read request - %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tokens := nlp.Tokenize(string(data))
out, err := json.Marshal(tokens)
if err != nil {
log.Printf("can't marshal output - %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(out)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
// TODO: Sanity checks
fmt.Fprintf(w, "OK\n")
}