~yerinalexey/gtranslate

ref: b67c4398fcbf66760c38d3f83e180a1c4f1fe426 gtranslate/api.go -rw-r--r-- 686 bytes
b67c4398Alexey Yerin web: add a nicer error page 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import (
	"fmt"
	"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)
			return
		}

		fmt.Fprint(w, translation)
	}
}