mirror of
https://codeberg.org/aryak/mozhi
synced 2025-06-04 00:41:07 +05:30
add /api/engines; move stuff to utils (closes #17)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user