Added gofiber
git-svn-id: file:///srv/svn/repo/mai/trunk@15 e410bdd4-646f-c54f-a7ce-fffcc4f439ae
This commit is contained in:
39
web/main.go
39
web/main.go
@@ -2,11 +2,42 @@ package main
|
||||
|
||||
import (
|
||||
"codeberg.org/SimpleWeb/SimplyTranslate/engines"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// TODO: port web frontend to Go.
|
||||
|
||||
func main() {
|
||||
engine := &engines.GoogleTranslate{}
|
||||
print(engine.DisplayName())
|
||||
app := fiber.New()
|
||||
|
||||
app.All("/api/translate", func(c *fiber.Ctx) error {
|
||||
from := ""
|
||||
to := ""
|
||||
engine := ""
|
||||
text := ""
|
||||
if c.Method() == "GET" {
|
||||
engine = c.Query("engine")
|
||||
text = c.Query("text")
|
||||
from = c.Query("from")
|
||||
to = c.Query("to")
|
||||
} else if c.Method() == "POST" {
|
||||
engine = c.FormValue("engine")
|
||||
text = c.FormValue("text")
|
||||
from = c.FormValue("from")
|
||||
to = c.FormValue("to")
|
||||
} else {
|
||||
return c.SendStatus(400)
|
||||
}
|
||||
if engine == "" {
|
||||
engine = "google"
|
||||
}
|
||||
if to == "" {
|
||||
return c.SendStatus(400)
|
||||
}
|
||||
if result, err := engines.Engines[engine].Translate(text, from, to); err != nil {
|
||||
return c.SendStatus(500)
|
||||
} else {
|
||||
return c.JSON(result)
|
||||
}
|
||||
})
|
||||
|
||||
app.Listen(":3000")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user