27 lines
585 B
Go
27 lines
585 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"palette/internal/lang"
|
|
)
|
|
|
|
// handleGuessLang serves POST /api/guess-language.
|
|
func (a *apiServer) handleGuessLang(w http.ResponseWriter, r *http.Request) {
|
|
setRateLimitHeaders(w, 1, 5)
|
|
if !rateLimitGuess(r) {
|
|
writeRateLimited(w, 1)
|
|
return
|
|
}
|
|
var req struct {
|
|
Content string `json:"content"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeErr(w, http.StatusBadRequest, "invalid json body")
|
|
return
|
|
}
|
|
l := lang.GuessLang(req.Content)
|
|
writeJSON(w, http.StatusOK, map[string]any{"language": l})
|
|
}
|