2021-01-22 20:42:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-15 16:46:21 +00:00
|
|
|
"fmt"
|
2021-01-22 20:42:13 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateApiHandler(settings *TranslateSettings) func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
from := req.FormValue("from")
|
|
|
|
if from == "" {
|
|
|
|
writeError(w, 400, "'from' field is missing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
to := req.FormValue("to")
|
|
|
|
if to == "" {
|
|
|
|
writeError(w, 400, "'to' field is missing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
text := req.FormValue("text")
|
|
|
|
if text == "" {
|
|
|
|
writeError(w, 400, "'text' field is missing")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
translation, err := Translate(settings, from, to, text)
|
|
|
|
if err != nil {
|
|
|
|
writeError(w, 500, err)
|
2021-03-15 16:42:17 +00:00
|
|
|
return
|
2021-01-22 20:42:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:46:21 +00:00
|
|
|
fmt.Fprint(w, translation)
|
2021-01-22 20:42:13 +00:00
|
|
|
}
|
|
|
|
}
|