Pass a struct instead of settings directly

This commit is contained in:
Alexey Yerin 2021-01-18 23:22:20 +03:00
parent fc80e5334b
commit ef9c46bdbe
2 changed files with 26 additions and 24 deletions

11
main.go
View File

@ -28,6 +28,11 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
type TranslateSettings struct{
proxy *url.URL
userAgent string
}
func main() { func main() {
bind := pflag.StringP("bind", "b", ":5000", "Address to bind the server to, [addr]:port") bind := pflag.StringP("bind", "b", ":5000", "Address to bind the server to, [addr]:port")
templateDir := pflag.String("template-dir", "./templates", "Templates directory") templateDir := pflag.String("template-dir", "./templates", "Templates directory")
@ -47,6 +52,8 @@ func main() {
proxyUrl = u proxyUrl = u
} }
settings := TranslateSettings{proxyUrl, *userAgent}
tmpl := template.Must(template.ParseGlob(path.Join(*templateDir, "*.html"))) tmpl := template.Must(template.ParseGlob(path.Join(*templateDir, "*.html")))
r := mux.NewRouter() r := mux.NewRouter()
@ -61,7 +68,7 @@ func main() {
to := req.FormValue("to") to := req.FormValue("to")
text := req.FormValue("text") text := req.FormValue("text")
translation, err := Translate(from, to, text, *userAgent, proxyUrl) translation, err := Translate(&settings, from, to, text)
if err != nil { if err != nil {
writeError(w, 500, err) writeError(w, 500, err)
} }
@ -93,7 +100,7 @@ func main() {
return return
} }
translation, err := Translate(from, to, text, *userAgent, proxyUrl) translation, err := Translate(&settings, from, to, text)
if err != nil { if err != nil {
writeError(w, 500, err) writeError(w, 500, err)
} }

View File

@ -10,35 +10,30 @@ import (
"golang.org/x/net/html" "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 // 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 // Escape parameters before embedding in URL
fromLang = url.QueryEscape(fromLang) fromLang = url.QueryEscape(fromLang)
toLang = url.QueryEscape(toLang) toLang = url.QueryEscape(toLang)
text = url.QueryEscape(text) 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 { if err != nil {
return "", fmt.Errorf("Request failed: %v", err) return "", fmt.Errorf("Request failed: %v", err)
} }