Move request handlers out of main.go
This commit is contained in:
parent
0d438fb5a5
commit
4c82aced4b
|
@ -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
57
main.go
|
@ -17,7 +17,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -58,55 +57,8 @@ func main() {
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
|
||||||
r.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
r.HandleFunc("/", CreateWebHandler(tmpl, &settings)).Methods("GET", "POST")
|
||||||
if req.Method == "GET" {
|
r.HandleFunc("/api", CreateApiHandler(&settings)).Methods("POST")
|
||||||
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")
|
|
||||||
|
|
||||||
// Static files
|
// Static files
|
||||||
fs := http.FileServer(http.Dir(*staticDir))
|
fs := http.FileServer(http.Dir(*staticDir))
|
||||||
|
@ -115,8 +67,3 @@ func main() {
|
||||||
log.Println("Starting on", *bind)
|
log.Println("Starting on", *bind)
|
||||||
log.Fatalln(http.ListenAndServe(*bind, r))
|
log.Fatalln(http.ListenAndServe(*bind, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeError(w http.ResponseWriter, status int, err interface{}) {
|
|
||||||
w.WriteHeader(status)
|
|
||||||
w.Write([]byte(fmt.Sprintln(err)))
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)))
|
||||||
|
}
|
|
@ -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})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue