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