add /api/engines; move stuff to utils (closes #17)

This commit is contained in:
Arya Kiran 2023-10-25 18:39:31 +05:30
parent 5984fdf1d9
commit d8c35ab057
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
7 changed files with 113 additions and 50 deletions

View file

@ -15,6 +15,23 @@ const docTemplate = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/api/engines": {
"get": {
"description": "Lists available Engines.",
"summary": "List engines",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/api/source_languages": { "/api/source_languages": {
"get": { "get": {
"summary": "Show list of available source languages for engine", "summary": "Show list of available source languages for engine",

View file

@ -4,6 +4,23 @@
"contact": {} "contact": {}
}, },
"paths": { "paths": {
"/api/engines": {
"get": {
"description": "Lists available Engines.",
"summary": "List engines",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/api/source_languages": { "/api/source_languages": {
"get": { "get": {
"summary": "Show list of available source languages for engine", "summary": "Show list of available source languages for engine",

View file

@ -22,6 +22,17 @@ definitions:
info: info:
contact: {} contact: {}
paths: paths:
/api/engines:
get:
description: Lists available Engines.
responses:
"200":
description: OK
schema:
additionalProperties:
type: string
type: object
summary: List engines
/api/source_languages: /api/source_languages:
get: get:
parameters: parameters:

View file

@ -99,3 +99,18 @@ func HandleTranslate(c *fiber.Ctx) error {
return c.JSON(data) return c.JSON(data)
} }
} }
// HandleEngines godoc
//
// @Summary List engines
// @Description Lists available Engines.
// @Success 200 {object} map[string]string
// @Router /api/engines [get]
func HandleEngines(c *fiber.Ctx) error {
engines := utils.EngineList()
serializedData := make(map[string]interface{}, len(engines))
for engineId, engineName := range engines {
serializedData[engineId] = engineName
}
return c.JSON(serializedData)
}

View file

@ -9,54 +9,6 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func envTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
}
func engineList() map[string]string {
engines := map[string]string{"all": "All Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"}
if envTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
delete(engines, "google")
} else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
delete(engines, "deepl")
} else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
delete(engines, "duckduckgo")
} else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
delete(engines, "libre")
} else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
delete(engines, "mymemory")
} else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
delete(engines, "reverso")
} else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false {
delete(engines, "watson")
} else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
delete(engines, "yandex")
}
return engines
}
// DeduplicateLists deduplicates a slice of List based on the Id field
func deDuplicateLists(input []libmozhi.List) []libmozhi.List {
// Create a map to store unique Ids
uniqueIds := make(map[string]struct{})
result := []libmozhi.List{}
// Iterate over the input slice
for _, item := range input {
// Check if the Id is unique
if _, found := uniqueIds[item.Id]; !found {
// Add the Id to the map and append the List to the result slice
uniqueIds[item.Id] = struct{}{}
result = append(result, item)
}
}
return result
}
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) { func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
sl := []libmozhi.List{} sl := []libmozhi.List{}
tl := []libmozhi.List{} tl := []libmozhi.List{}
@ -66,11 +18,11 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List)
sl = append(sl, temp...) sl = append(sl, temp...)
tl = append(tl, temp2...) tl = append(tl, temp2...)
} }
return deDuplicateLists(sl), deDuplicateLists(tl) return utils.DeDuplicateLists(sl), utils.DeDuplicateLists(tl)
} }
func HandleIndex(c *fiber.Ctx) error { func HandleIndex(c *fiber.Ctx) error {
engines := engineList() engines := utils.EngineList()
var enginesAsArray []string var enginesAsArray []string
for engine := range engines { for engine := range engines {
enginesAsArray = append(enginesAsArray, engine) enginesAsArray = append(enginesAsArray, engine)

View file

@ -89,6 +89,7 @@ func Serve(port string) {
api.All("/translate", pages.HandleTranslate) api.All("/translate", pages.HandleTranslate)
api.Get("/source_languages", pages.HandleSourceLanguages) api.Get("/source_languages", pages.HandleSourceLanguages)
api.Get("/target_languages", pages.HandleTargetLanguages) api.Get("/target_languages", pages.HandleTargetLanguages)
api.Get("/engines", pages.HandleEngines)
api.Get("/tts", pages.HandleTTS) api.Get("/tts", pages.HandleTTS)
api.Get("/version", func(c *fiber.Ctx) error { api.Get("/version", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{ return c.JSON(fiber.Map{

View file

@ -3,6 +3,8 @@ package utils
import ( import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"regexp" "regexp"
"os"
"codeberg.org/aryak/libmozhi"
) )
func GetQueryOrFormValue(c *fiber.Ctx, key string) string { func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
@ -13,6 +15,13 @@ func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
} }
} }
func EnvTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
}
func Sanitize(str string, strip string) string { func Sanitize(str string, strip string) string {
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`) nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`) nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
@ -23,3 +32,44 @@ func Sanitize(str string, strip string) string {
} }
return "" return ""
} }
func EngineList() map[string]string {
engines := map[string]string{"all": "All Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"}
if EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
delete(engines, "google")
} else if EnvTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
delete(engines, "deepl")
} else if EnvTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
delete(engines, "duckduckgo")
} else if EnvTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || EnvTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
delete(engines, "libre")
} else if EnvTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
delete(engines, "mymemory")
} else if EnvTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
delete(engines, "reverso")
} else if EnvTrueNoExist("MOZHI_WATSON_ENABLED") == false {
delete(engines, "watson")
} else if EnvTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
delete(engines, "yandex")
}
return engines
}
// DeduplicateLists deduplicates a slice of List based on the Id field
func DeDuplicateLists(input []libmozhi.List) []libmozhi.List {
// Create a map to store unique Ids
uniqueIds := make(map[string]struct{})
result := []libmozhi.List{}
// Iterate over the input slice
for _, item := range input {
// Check if the Id is unique
if _, found := uniqueIds[item.Id]; !found {
// Add the Id to the map and append the List to the result slice
uniqueIds[item.Id] = struct{}{}
result = append(result, item)
}
}
return result
}