Pass a struct instead of settings directly
This commit is contained in:
parent
fc80e5334b
commit
ef9c46bdbe
11
main.go
11
main.go
|
@ -28,6 +28,11 @@ import (
|
|||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
type TranslateSettings struct{
|
||||
proxy *url.URL
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func main() {
|
||||
bind := pflag.StringP("bind", "b", ":5000", "Address to bind the server to, [addr]:port")
|
||||
templateDir := pflag.String("template-dir", "./templates", "Templates directory")
|
||||
|
@ -47,6 +52,8 @@ func main() {
|
|||
proxyUrl = u
|
||||
}
|
||||
|
||||
settings := TranslateSettings{proxyUrl, *userAgent}
|
||||
|
||||
tmpl := template.Must(template.ParseGlob(path.Join(*templateDir, "*.html")))
|
||||
|
||||
r := mux.NewRouter()
|
||||
|
@ -61,7 +68,7 @@ func main() {
|
|||
to := req.FormValue("to")
|
||||
text := req.FormValue("text")
|
||||
|
||||
translation, err := Translate(from, to, text, *userAgent, proxyUrl)
|
||||
translation, err := Translate(&settings, from, to, text)
|
||||
if err != nil {
|
||||
writeError(w, 500, err)
|
||||
}
|
||||
|
@ -93,7 +100,7 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
translation, err := Translate(from, to, text, *userAgent, proxyUrl)
|
||||
translation, err := Translate(&settings, from, to, text)
|
||||
if err != nil {
|
||||
writeError(w, 500, err)
|
||||
}
|
||||
|
|
39
translate.go
39
translate.go
|
@ -10,35 +10,30 @@ import (
|
|||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func getWithProxy(url string, userAgent string, proxy *url.URL) (*http.Response, error) {
|
||||
client := &http.Client{}
|
||||
|
||||
if proxy != nil {
|
||||
tr := &http.Transport{ Proxy: http.ProxyURL(proxy) }
|
||||
client.Transport = tr
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header["User-Agent"] = []string{userAgent}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Requests and parses translation from Google
|
||||
func Translate(fromLang, toLang, text string, userAgent string, proxy *url.URL) (string, error) {
|
||||
func Translate(settings *TranslateSettings, fromLang, toLang, text string) (string, error) {
|
||||
// Escape parameters before embedding in URL
|
||||
fromLang = url.QueryEscape(fromLang)
|
||||
toLang = url.QueryEscape(toLang)
|
||||
text = url.QueryEscape(text)
|
||||
|
||||
url := fmt.Sprintf("https://translate.google.com/m?sl=%s&tl=%s&q=%s", fromLang, toLang, text)
|
||||
reqUrl := fmt.Sprintf("https://translate.google.com/m?sl=%s&tl=%s&q=%s", fromLang, toLang, text)
|
||||
|
||||
resp, err := getWithProxy(url, userAgent, proxy)
|
||||
client := &http.Client{}
|
||||
|
||||
if settings.proxy != nil {
|
||||
tr := &http.Transport{ Proxy: http.ProxyURL(settings.proxy) }
|
||||
client.Transport = tr
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", reqUrl, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req.Header["User-Agent"] = []string{settings.userAgent}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Request failed: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue