diff --git a/docs/docs.go b/docs/docs.go index 8fe574b..66f09a8 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -15,6 +15,23 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/api/engines": { + "get": { + "description": "Lists available Engines.", + "summary": "List engines", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } + } + } + } + }, "/api/source_languages": { "get": { "summary": "Show list of available source languages for engine", diff --git a/docs/swagger.json b/docs/swagger.json index e656508..244566a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4,6 +4,23 @@ "contact": {} }, "paths": { + "/api/engines": { + "get": { + "description": "Lists available Engines.", + "summary": "List engines", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } + } + } + } + }, "/api/source_languages": { "get": { "summary": "Show list of available source languages for engine", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 5009c5f..be0307e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -22,6 +22,17 @@ definitions: info: contact: {} paths: + /api/engines: + get: + description: Lists available Engines. + responses: + "200": + description: OK + schema: + additionalProperties: + type: string + type: object + summary: List engines /api/source_languages: get: parameters: diff --git a/pages/api.go b/pages/api.go index d14d82f..73345df 100644 --- a/pages/api.go +++ b/pages/api.go @@ -99,3 +99,18 @@ func HandleTranslate(c *fiber.Ctx) error { 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) +} diff --git a/pages/index.go b/pages/index.go index 07db11b..dcb221d 100644 --- a/pages/index.go +++ b/pages/index.go @@ -9,54 +9,6 @@ import ( "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) { sl := []libmozhi.List{} tl := []libmozhi.List{} @@ -66,11 +18,11 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) sl = append(sl, temp...) tl = append(tl, temp2...) } - return deDuplicateLists(sl), deDuplicateLists(tl) + return utils.DeDuplicateLists(sl), utils.DeDuplicateLists(tl) } func HandleIndex(c *fiber.Ctx) error { - engines := engineList() + engines := utils.EngineList() var enginesAsArray []string for engine := range engines { enginesAsArray = append(enginesAsArray, engine) diff --git a/serve/serve.go b/serve/serve.go index d1b638a..510fd34 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -89,6 +89,7 @@ func Serve(port string) { api.All("/translate", pages.HandleTranslate) api.Get("/source_languages", pages.HandleSourceLanguages) api.Get("/target_languages", pages.HandleTargetLanguages) + api.Get("/engines", pages.HandleEngines) api.Get("/tts", pages.HandleTTS) api.Get("/version", func(c *fiber.Ctx) error { return c.JSON(fiber.Map{ diff --git a/utils/main.go b/utils/main.go index 07a191e..98746f2 100644 --- a/utils/main.go +++ b/utils/main.go @@ -3,6 +3,8 @@ package utils import ( "github.com/gofiber/fiber/v2" "regexp" + "os" + "codeberg.org/aryak/libmozhi" ) 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 { nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`) nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`) @@ -23,3 +32,44 @@ func Sanitize(str string, strip string) string { } 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 +}