From ef9c46bdbeb4fdbcf7fab6b0f57bfe0a82798993 Mon Sep 17 00:00:00 2001 From: Alexey Yerin Date: Mon, 18 Jan 2021 23:22:20 +0300 Subject: [PATCH] Pass a struct instead of settings directly --- main.go | 11 +++++++++-- translate.go | 39 +++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index a64f068..f59fdf2 100644 --- a/main.go +++ b/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) } diff --git a/translate.go b/translate.go index f3b35cd..6354e79 100644 --- a/translate.go +++ b/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) }