Пример #1
0
func TokenizeHandler(t tokenizer.Tokenizer) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		s := req.URL.Query().Get(":string")
		log.Println("RESTful Tokenize:", s)
		token, err := t.Tokenize(s)
		if err != nil {
			http.Error(w, err.Error(), 500)
		}
		fmt.Fprint(w, token)
	}
}
Пример #2
0
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)
	}
}
Пример #3
0
// 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)
		}
	}
}
Пример #4
0
func Tokenize(t tokenizer.Tokenizer) wsHandler {
	return func(ws *websocket.Conn) {
		log.Println("New websocket connection")
		log.Println("    Location:  ", ws.Config().Location)
		log.Println("    Origin:    ", ws.Config().Origin)
		log.Println("    Protocol:  ", ws.Config().Protocol)
		dec := json.NewDecoder(ws)
		enc := json.NewEncoder(ws)
		for {
			var err error
			var request JsonTokenizeRequest
			// 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 := TokenizeReponse{Status: invalidRequest, Error: err.Error()}
				enc.Encode(&response)
				log.Println("Invalid request - websocket disconnecting")
				return
			}
			data := make(map[string]string)
			for fieldname, text := range request.Data {
				data[fieldname], err = t.Tokenize(text)
				if err != nil {
					// TODO: Do something nicer with this error?
					log.Panic(err)
				}
			}
			response := TokenizeReponse{
				ReqId:  request.ReqId,
				Status: success,
				Data:   data,
			}
			enc.Encode(response)
		}
	}
}