mirror of
https://github.com/coalaura/whiskr.git
synced 2025-12-02 20:22:52 +00:00
33 lines
585 B
Go
33 lines
585 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type TokenizeRequest struct {
|
|
String string `json:"string"`
|
|
}
|
|
|
|
func HandleTokenize(tokenizer *Tokenizer) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
debug("parsing tokenize")
|
|
|
|
var raw TokenizeRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
|
|
RespondJson(w, http.StatusBadRequest, map[string]any{
|
|
"error": err.Error(),
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
tokens := tokenizer.Encode(raw.String)
|
|
|
|
RespondJson(w, http.StatusOK, map[string]any{
|
|
"tokens": len(tokens),
|
|
})
|
|
}
|
|
}
|