Move request handlers out of main.go

This commit is contained in:
Alexey Yerin 2021-01-22 23:42:13 +03:00
parent 0d438fb5a5
commit 4c82aced4b
4 changed files with 78 additions and 55 deletions

32
api.go Normal file
View File

@ -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))
}
}

57
main.go
View File

@ -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)))
}

11
utils.go Normal file
View File

@ -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)))
}

33
web.go Normal file
View File

@ -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})
}
}