[53] | 1 | package main
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bytes"
|
---|
| 5 | "net/http"
|
---|
| 6 | "net/url"
|
---|
| 7 | "time"
|
---|
| 8 | "flag"
|
---|
| 9 |
|
---|
| 10 | "git.chaotic.ninja/yakumo.izuru/simplytranslate/engines"
|
---|
| 11 | "github.com/gofiber/fiber/v2"
|
---|
| 12 | "github.com/gofiber/template/html/v2"
|
---|
| 13 | "gopkg.in/ini.v1"
|
---|
| 14 | )
|
---|
| 15 |
|
---|
| 16 | var conf struct {
|
---|
| 17 | listen string
|
---|
| 18 | staticpath string
|
---|
| 19 | tmplpath string
|
---|
| 20 | }
|
---|
| 21 | func readConf(file string) error {
|
---|
| 22 | cfg, err := ini.Load(file)
|
---|
| 23 | if err != nil {
|
---|
| 24 | return err
|
---|
| 25 | }
|
---|
| 26 | conf.listen = cfg.Section("").Key("listen").String()
|
---|
| 27 | conf.staticpath = cfg.Section("").Key("rootdir").String()
|
---|
| 28 | conf.tmplpath = cfg.Section("").Key("tmplpath").String()
|
---|
| 29 |
|
---|
| 30 | return nil
|
---|
| 31 | }
|
---|
| 32 | func main() {
|
---|
| 33 | var configfile string
|
---|
| 34 | flag.StringVar(&configfile, "f", "", "Configuration file")
|
---|
| 35 | flag.Parse()
|
---|
| 36 |
|
---|
| 37 | if configfile != "" {
|
---|
| 38 | readConf(configfile)
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | conf.listen = "127.0.0.1:5000"
|
---|
| 42 | conf.staticpath = "./static"
|
---|
| 43 | conf.tmplpath = "./views"
|
---|
| 44 |
|
---|
| 45 | engine := html.New(conf.tmplpath, ".html")
|
---|
| 46 | engine.AddFunc("inc", func(i int) int { return i + 1 })
|
---|
| 47 |
|
---|
| 48 | app := fiber.New(fiber.Config{
|
---|
| 49 | Views: engine,
|
---|
| 50 | })
|
---|
| 51 |
|
---|
| 52 | app.All("/", func(c *fiber.Ctx) error {
|
---|
| 53 | engine := c.Cookies("engine")
|
---|
| 54 | if c.Query("engine") != "" {
|
---|
| 55 | engine = c.Query("engine")
|
---|
| 56 | }
|
---|
| 57 | if _, ok := engines.Engines[engine]; !ok {
|
---|
| 58 | engine = "google"
|
---|
| 59 | }
|
---|
| 60 | targetLanguages, err := engines.Engines[engine].TargetLanguages()
|
---|
| 61 | if err != nil {
|
---|
| 62 | return c.SendStatus(500)
|
---|
| 63 | }
|
---|
| 64 | sourceLanguages, err := engines.Engines[engine].SourceLanguages()
|
---|
| 65 | if err != nil {
|
---|
| 66 | return c.SendStatus(500)
|
---|
| 67 | }
|
---|
| 68 | originalText := ""
|
---|
| 69 | translatedText := ""
|
---|
| 70 | from := ""
|
---|
| 71 | to := ""
|
---|
| 72 | ttsFrom := ""
|
---|
| 73 | ttsTo := ""
|
---|
| 74 | sourceLanguage := ""
|
---|
| 75 |
|
---|
| 76 | var translation engines.TranslationResult
|
---|
| 77 | if c.Method() == "POST" {
|
---|
| 78 | from = c.FormValue("from")
|
---|
| 79 | to = c.FormValue("to")
|
---|
| 80 | originalText = c.FormValue("text")
|
---|
| 81 | if result, err := engines.Engines[engine].Translate(originalText, from, to); err != nil {
|
---|
| 82 | return c.SendStatus(500)
|
---|
| 83 | } else {
|
---|
| 84 | translatedText = result.TranslatedText
|
---|
| 85 | translation = result
|
---|
| 86 | sourceLanguage = result.SourceLanguage
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | ttsFromURL, _ := url.Parse("api/tts")
|
---|
| 90 | fromQuery := url.Values{}
|
---|
| 91 | fromQuery.Add("lang", from)
|
---|
| 92 | fromQuery.Add("text", originalText)
|
---|
| 93 | ttsFromURL.RawQuery = fromQuery.Encode()
|
---|
| 94 | ttsFrom = ttsFromURL.String()
|
---|
| 95 |
|
---|
| 96 | ttsToURL, _ := url.Parse("api/tts")
|
---|
| 97 | toQuery := url.Values{}
|
---|
| 98 | toQuery.Add("lang", to)
|
---|
| 99 | toQuery.Add("text", translatedText)
|
---|
| 100 | ttsToURL.RawQuery = toQuery.Encode()
|
---|
| 101 | ttsTo = ttsToURL.String()
|
---|
| 102 |
|
---|
| 103 | fromCookie := new(fiber.Cookie)
|
---|
| 104 | fromCookie.Name = "from"
|
---|
| 105 | fromCookie.Value = from
|
---|
| 106 | fromCookie.Expires = time.Now().Add(time.Hour * 24 * 365)
|
---|
| 107 | c.Cookie(fromCookie)
|
---|
| 108 |
|
---|
| 109 | toCookie := new(fiber.Cookie)
|
---|
| 110 | toCookie.Name = "to"
|
---|
| 111 | toCookie.Value = to
|
---|
| 112 | toCookie.Expires = time.Now().Add(time.Hour * 24 * 365)
|
---|
| 113 | c.Cookie(toCookie)
|
---|
| 114 |
|
---|
| 115 | engineCookie := new(fiber.Cookie)
|
---|
| 116 | engineCookie.Name = "engine"
|
---|
| 117 | engineCookie.Value = engine
|
---|
| 118 | engineCookie.Expires = time.Now().Add(time.Hour * 24 * 365)
|
---|
| 119 | c.Cookie(engineCookie)
|
---|
| 120 | } else if c.Method() == "GET" {
|
---|
| 121 | from = c.Cookies("from")
|
---|
| 122 | to = c.Cookies("to")
|
---|
| 123 | } else {
|
---|
| 124 | return c.SendStatus(400)
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | if from == "" {
|
---|
| 128 | from = "auto"
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | enginesNames := map[string]string{}
|
---|
| 132 | for k, v := range engines.Engines {
|
---|
| 133 | enginesNames[k] = v.DisplayName()
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return c.Render("index", fiber.Map{
|
---|
| 137 | "Engine": engine,
|
---|
| 138 | "enginesNames": enginesNames,
|
---|
| 139 | "SourceLanguages": sourceLanguages,
|
---|
| 140 | "TargetLanguages": targetLanguages,
|
---|
| 141 | "OriginalText": originalText,
|
---|
| 142 | "Translation": translation,
|
---|
| 143 | "From": from,
|
---|
| 144 | "To": to,
|
---|
| 145 | "TtsFrom": ttsFrom,
|
---|
| 146 | "TtsTo": ttsTo,
|
---|
| 147 | "SourceLanguage": sourceLanguage,
|
---|
| 148 | })
|
---|
| 149 | })
|
---|
| 150 |
|
---|
| 151 | app.All("/api/translate", func(c *fiber.Ctx) error {
|
---|
| 152 | from := ""
|
---|
| 153 | to := ""
|
---|
| 154 | engine := ""
|
---|
| 155 | text := ""
|
---|
| 156 | if c.Method() == "GET" {
|
---|
| 157 | engine = c.Query("engine")
|
---|
| 158 | text = c.Query("text")
|
---|
| 159 | from = c.Query("from")
|
---|
| 160 | to = c.Query("to")
|
---|
| 161 | } else if c.Method() == "POST" {
|
---|
| 162 | engine = c.FormValue("engine")
|
---|
| 163 | text = c.FormValue("text")
|
---|
| 164 | from = c.FormValue("from")
|
---|
| 165 | to = c.FormValue("to")
|
---|
| 166 | } else {
|
---|
| 167 | return c.SendStatus(400)
|
---|
| 168 | }
|
---|
| 169 | if _, ok := engines.Engines[engine]; !ok || engine == "" {
|
---|
| 170 | engine = "google"
|
---|
| 171 | }
|
---|
| 172 | if to == "" {
|
---|
| 173 | return c.SendStatus(400)
|
---|
| 174 | }
|
---|
| 175 | if result, err := engines.Engines[engine].Translate(text, from, to); err != nil {
|
---|
| 176 | return c.SendStatus(500)
|
---|
| 177 | } else {
|
---|
| 178 | return c.JSON(result)
|
---|
| 179 | }
|
---|
| 180 | })
|
---|
| 181 |
|
---|
| 182 | app.Get("/api/source_languages", func(c *fiber.Ctx) error {
|
---|
| 183 | engine := c.Query("engine")
|
---|
| 184 | if _, ok := engines.Engines[engine]; !ok || engine == "" {
|
---|
| 185 | engine = "google"
|
---|
| 186 | }
|
---|
| 187 | if result, err := engines.Engines[engine].SourceLanguages(); err != nil {
|
---|
| 188 | return c.SendStatus(500)
|
---|
| 189 | } else {
|
---|
| 190 | return c.JSON(result)
|
---|
| 191 | }
|
---|
| 192 | })
|
---|
| 193 |
|
---|
| 194 | app.Get("/api/target_languages", func(c *fiber.Ctx) error {
|
---|
| 195 | engine := c.Query("engine")
|
---|
| 196 | if _, ok := engines.Engines[engine]; !ok || engine == "" {
|
---|
| 197 | engine = "google"
|
---|
| 198 | }
|
---|
| 199 | if result, err := engines.Engines[engine].TargetLanguages(); err != nil {
|
---|
| 200 | return c.SendStatus(500)
|
---|
| 201 | } else {
|
---|
| 202 | return c.JSON(result)
|
---|
| 203 | }
|
---|
| 204 | })
|
---|
| 205 |
|
---|
| 206 | app.Get("/api/tts", func(c *fiber.Ctx) error {
|
---|
| 207 | engine := c.Query("engine")
|
---|
| 208 | if _, ok := engines.Engines[engine]; !ok || engine == "" {
|
---|
| 209 | engine = "google"
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | text := c.Query("text")
|
---|
| 213 | if text == "" {
|
---|
| 214 | return c.SendStatus(400)
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | lang := c.Query("lang")
|
---|
| 218 |
|
---|
| 219 | if url, err := engines.Engines[engine].Tts(text, lang); err != nil {
|
---|
| 220 | return c.SendStatus(500)
|
---|
| 221 | } else {
|
---|
| 222 | if response, err := http.Get(url); err != nil {
|
---|
| 223 | return c.SendStatus(500)
|
---|
| 224 | } else {
|
---|
| 225 | defer response.Body.Close()
|
---|
| 226 | var buf bytes.Buffer
|
---|
| 227 | response.Write(&buf)
|
---|
| 228 | c.Context().SetContentType("audio/mpeg")
|
---|
| 229 | return c.Send(buf.Bytes())
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | })
|
---|
| 233 |
|
---|
| 234 | app.Post("/switchlanguages", func(c *fiber.Ctx) error {
|
---|
| 235 | if c.Cookies("from") != "" {
|
---|
| 236 | fromCookie := new(fiber.Cookie)
|
---|
| 237 | fromCookie.Name = "from"
|
---|
| 238 | fromCookie.Value = c.Cookies("to")
|
---|
| 239 | fromCookie.Expires = time.Now().Add(24 * time.Hour * 365)
|
---|
| 240 |
|
---|
| 241 | toCookie := new(fiber.Cookie)
|
---|
| 242 | toCookie.Name = "to"
|
---|
| 243 | toCookie.Value = c.Cookies("from")
|
---|
| 244 | toCookie.Expires = time.Now().Add(24 * time.Hour * 365)
|
---|
| 245 |
|
---|
| 246 | c.Cookie(fromCookie)
|
---|
| 247 | c.Cookie(toCookie)
|
---|
| 248 | }
|
---|
| 249 | return c.Redirect("/")
|
---|
| 250 | })
|
---|
| 251 | app.Static("/static", conf.staticpath, fiber.Static{
|
---|
| 252 | Compress: true,
|
---|
| 253 | ByteRange: true,
|
---|
| 254 | Browse: true,
|
---|
| 255 | })
|
---|
| 256 | app.Listen(conf.listen)
|
---|
| 257 | }
|
---|