From 4c82aced4b1aeb42e743f5f77a6560a7040a8df2 Mon Sep 17 00:00:00 2001 From: Alexey Yerin Date: Fri, 22 Jan 2021 23:42:13 +0300 Subject: [PATCH] Move request handlers out of main.go --- api.go | 32 +++++++++++++++++++++++++++++++ main.go | 57 ++------------------------------------------------------ utils.go | 11 +++++++++++ web.go | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 55 deletions(-) create mode 100644 api.go create mode 100644 utils.go create mode 100644 web.go diff --git a/api.go b/api.go new file mode 100644 index 0000000..b0faf7b --- /dev/null +++ b/api.go @@ -0,0 +1,32 @@ +package main + +import ( + "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) + } + + w.Write([]byte(translation)) + } +} diff --git a/main.go b/main.go index 60ed973..a4883b4 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ package main import ( - "fmt" "html/template" "log" "net/http" @@ -58,55 +57,8 @@ func main() { r := mux.NewRouter() - r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - if req.Method == "GET" { - tmpl.ExecuteTemplate(w, "index", nil) - return - } - - from := req.FormValue("from") - to := req.FormValue("to") - text := req.FormValue("text") - - translation, err := Translate(&settings, from, to, text) - if err != nil { - writeError(w, 500, err) - } - - type indexPageData struct { - From string - To string - Text string - Translation string - } - - tmpl.ExecuteTemplate(w, "index", indexPageData{from, to, text, translation}) - }).Methods("GET", "POST") - - r.HandleFunc("/api", 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) - } - - w.Write([]byte(translation)) - }).Methods("POST") + r.HandleFunc("/", CreateWebHandler(tmpl, &settings)).Methods("GET", "POST") + r.HandleFunc("/api", CreateApiHandler(&settings)).Methods("POST") // Static files fs := http.FileServer(http.Dir(*staticDir)) @@ -115,8 +67,3 @@ func main() { log.Println("Starting on", *bind) log.Fatalln(http.ListenAndServe(*bind, r)) } - -func writeError(w http.ResponseWriter, status int, err interface{}) { - w.WriteHeader(status) - w.Write([]byte(fmt.Sprintln(err))) -} diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..2f572b0 --- /dev/null +++ b/utils.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "net/http" +) + +func writeError(w http.ResponseWriter, status int, err interface{}) { + w.WriteHeader(status) + w.Write([]byte(fmt.Sprintln(err))) +} diff --git a/web.go b/web.go new file mode 100644 index 0000000..9b1ff12 --- /dev/null +++ b/web.go @@ -0,0 +1,33 @@ +package main + +import ( + "html/template" + "net/http" +) + +func CreateWebHandler(tmpl *template.Template, settings *TranslateSettings) func(w http.ResponseWriter, req *http.Request) { + return func(w http.ResponseWriter, req *http.Request) { + if req.Method == "GET" { + tmpl.ExecuteTemplate(w, "index", nil) + return + } + + from := req.FormValue("from") + to := req.FormValue("to") + text := req.FormValue("text") + + translation, err := Translate(settings, from, to, text) + if err != nil { + writeError(w, 500, err) + } + + type indexPageData struct { + From string + To string + Text string + Translation string + } + + tmpl.ExecuteTemplate(w, "index", indexPageData{from, to, text, translation}) + } +}