func DetokenizeHandler(t tokenizer.Tokenizer) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { s := req.URL.Query().Get(":token") log.Println("RESTful Detokenize:", s) orig, err := t.Detokenize(s) switch { case err == tokenizer.TokenNotFound: http.Error(w, "Token not found", 404) case err != nil: http.Error(w, err.Error(), 500) } fmt.Fprint(w, orig) } }
// A websocket handler for detokenization func Detokenize(t tokenizer.Tokenizer) wsHandler { return func(ws *websocket.Conn) { dec := json.NewDecoder(ws) enc := json.NewEncoder(ws) for { var request DetokenizeRequest // Read one request from the socket and attempt to decode switch err := dec.Decode(&request); true { case err == io.EOF: log.Println("Websocket disconnecting") return case err != nil: // Request could not be decoded - return error response := DetokenizeReponse{Status: invalidRequest, Error: err.Error()} enc.Encode(&response) return } data := make(map[string]foundToken) for fieldname, token := range request.Data { ft := foundToken{ Token: token, } text, err := t.Detokenize(token) switch { case nil == err: ft.Text = text ft.Found = true case err == tokenizer.TokenNotFound: ft.Found = false case err != nil: log.Panic(err) } data[fieldname] = ft } response := DetokenizeReponse{ ReqId: request.ReqId, Status: success, Data: data, } enc.Encode(response) } } }