2021-01-17 20:09:57 +00:00
|
|
|
// gtranslate - Better front-end for Google Translate
|
|
|
|
// Copyright (C) 2021-present Alexey Yerin
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-18 19:52:33 +00:00
|
|
|
"fmt"
|
2021-01-17 20:09:57 +00:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-01-18 20:02:00 +00:00
|
|
|
"net/url"
|
2021-01-17 20:09:57 +00:00
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
bind := pflag.StringP("bind", "b", ":5000", "Address to bind the server to, [addr]:port")
|
|
|
|
templateDir := pflag.String("template-dir", "./templates", "Templates directory")
|
|
|
|
staticDir := pflag.String("static-dir", "./static", "Static files directory")
|
2021-01-18 20:02:00 +00:00
|
|
|
proxy := pflag.String("proxy", "", "Proxy URL, with no scheme http is assumed")
|
2021-01-17 20:09:57 +00:00
|
|
|
pflag.Parse()
|
|
|
|
|
2021-01-18 20:02:00 +00:00
|
|
|
var proxyUrl *url.URL
|
|
|
|
if *proxy != "" {
|
|
|
|
u, err := url.Parse(*proxy)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Invalid proxy URL:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUrl = u
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:09:57 +00:00
|
|
|
tmpl := template.Must(template.ParseGlob(path.Join(*templateDir, "*.html")))
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2021-01-18 20:02:00 +00:00
|
|
|
translation, err := Translate(from, to, text, proxyUrl)
|
2021-01-17 20:09:57 +00:00
|
|
|
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")
|
2021-01-18 19:52:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:02:00 +00:00
|
|
|
translation, err := Translate(from, to, text, proxyUrl)
|
2021-01-18 19:52:33 +00:00
|
|
|
if err != nil {
|
|
|
|
writeError(w, 500, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write([]byte(translation))
|
|
|
|
}).Methods("POST")
|
2021-01-17 20:09:57 +00:00
|
|
|
|
|
|
|
// Static files
|
|
|
|
fs := http.FileServer(http.Dir(*staticDir))
|
|
|
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
|
|
|
|
|
|
|
|
log.Println("Starting on", *bind)
|
|
|
|
log.Fatalln(http.ListenAndServe(*bind, r))
|
|
|
|
}
|
2021-01-18 19:52:33 +00:00
|
|
|
|
|
|
|
func writeError(w http.ResponseWriter, status int, err interface{}) {
|
|
|
|
w.WriteHeader(status)
|
|
|
|
w.Write([]byte(fmt.Sprintln(err)))
|
|
|
|
}
|