add /api/engines; move stuff to utils (closes #17)
This commit is contained in:
parent
5984fdf1d9
commit
d8c35ab057
7 changed files with 113 additions and 50 deletions
17
docs/docs.go
17
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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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:
|
||||
|
|
15
pages/api.go
15
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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue