source: code/trunk/web/main.go@ 15

Last change on this file since 15 was 15, checked in by manerakai, 22 months ago

Added gofiber

File size: 850 bytes
RevLine 
[9]1package main
2
3import (
4 "codeberg.org/SimpleWeb/SimplyTranslate/engines"
[15]5 "github.com/gofiber/fiber/v2"
[9]6)
7
[15]8func main() {
9 app := fiber.New()
[9]10
[15]11 app.All("/api/translate", func(c *fiber.Ctx) error {
12 from := ""
13 to := ""
14 engine := ""
15 text := ""
16 if c.Method() == "GET" {
17 engine = c.Query("engine")
18 text = c.Query("text")
19 from = c.Query("from")
20 to = c.Query("to")
21 } else if c.Method() == "POST" {
22 engine = c.FormValue("engine")
23 text = c.FormValue("text")
24 from = c.FormValue("from")
25 to = c.FormValue("to")
26 } else {
27 return c.SendStatus(400)
28 }
29 if engine == "" {
30 engine = "google"
31 }
32 if to == "" {
33 return c.SendStatus(400)
34 }
35 if result, err := engines.Engines[engine].Translate(text, from, to); err != nil {
36 return c.SendStatus(500)
37 } else {
38 return c.JSON(result)
39 }
40 })
41
42 app.Listen(":3000")
[9]43}
Note: See TracBrowser for help on using the repository browser.