Allow querying and changeing i18n language via API.
This commit is contained in:
parent
0530f5dff2
commit
2139bb9c79
|
@ -45,6 +45,7 @@ type configuration struct {
|
||||||
CoreDNS coreDNSConfig `yaml:"coredns"`
|
CoreDNS coreDNSConfig `yaml:"coredns"`
|
||||||
Filters []filter `yaml:"filters"`
|
Filters []filter `yaml:"filters"`
|
||||||
UserRules []string `yaml:"user_rules"`
|
UserRules []string `yaml:"user_rules"`
|
||||||
|
Language string `yaml:"language"` // two-letter ISO 639-1 language code
|
||||||
|
|
||||||
sync.RWMutex `yaml:"-"`
|
sync.RWMutex `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,7 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
"bootstrap_dns": config.CoreDNS.BootstrapDNS,
|
"bootstrap_dns": config.CoreDNS.BootstrapDNS,
|
||||||
"upstream_dns": config.CoreDNS.UpstreamDNS,
|
"upstream_dns": config.CoreDNS.UpstreamDNS,
|
||||||
"version": VersionString,
|
"version": VersionString,
|
||||||
|
"language": config.Language,
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonVal, err := json.Marshal(data)
|
jsonVal, err := json.Marshal(data)
|
||||||
|
@ -876,6 +877,8 @@ func registerControlHandlers() {
|
||||||
http.HandleFunc("/control/querylog_disable", optionalAuth(ensurePOST(handleQueryLogDisable)))
|
http.HandleFunc("/control/querylog_disable", optionalAuth(ensurePOST(handleQueryLogDisable)))
|
||||||
http.HandleFunc("/control/set_upstream_dns", optionalAuth(ensurePOST(handleSetUpstreamDNS)))
|
http.HandleFunc("/control/set_upstream_dns", optionalAuth(ensurePOST(handleSetUpstreamDNS)))
|
||||||
http.HandleFunc("/control/test_upstream_dns", optionalAuth(ensurePOST(handleTestUpstreamDNS)))
|
http.HandleFunc("/control/test_upstream_dns", optionalAuth(ensurePOST(handleTestUpstreamDNS)))
|
||||||
|
http.HandleFunc("/control/i18n/change_language", optionalAuth(ensurePOST(handleI18nChangeLanguage)))
|
||||||
|
http.HandleFunc("/control/i18n/current_language", optionalAuth(ensureGET(handleI18nCurrentLanguage)))
|
||||||
http.HandleFunc("/control/stats_top", optionalAuth(ensureGET(corednsplugin.HandleStatsTop)))
|
http.HandleFunc("/control/stats_top", optionalAuth(ensureGET(corednsplugin.HandleStatsTop)))
|
||||||
http.HandleFunc("/control/stats", optionalAuth(ensureGET(corednsplugin.HandleStats)))
|
http.HandleFunc("/control/stats", optionalAuth(ensureGET(corednsplugin.HandleStats)))
|
||||||
http.HandleFunc("/control/stats_history", optionalAuth(ensureGET(corednsplugin.HandleStatsHistory)))
|
http.HandleFunc("/control/stats_history", optionalAuth(ensureGET(corednsplugin.HandleStatsHistory)))
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// --------------------
|
||||||
|
// internationalization
|
||||||
|
// --------------------
|
||||||
|
var allowedLanguages = map[string]bool{
|
||||||
|
"en": true,
|
||||||
|
"ru": true,
|
||||||
|
"vi": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isLanguageAllowed(language string) bool {
|
||||||
|
l := strings.ToLower(language)
|
||||||
|
if allowedLanguages[l] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
_, err := fmt.Fprintf(w, config.Language)
|
||||||
|
if err != nil {
|
||||||
|
errortext := fmt.Sprintf("Unable to write response json: %s", err)
|
||||||
|
log.Println(errortext)
|
||||||
|
http.Error(w, errortext, http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
errorText := fmt.Sprintf("failed to read request body: %s", err)
|
||||||
|
log.Println(errorText)
|
||||||
|
http.Error(w, errorText, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
language := strings.TrimSpace(string(body))
|
||||||
|
if language == "" {
|
||||||
|
errorText := fmt.Sprintf("empty language specified")
|
||||||
|
log.Println(errorText)
|
||||||
|
http.Error(w, errorText, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !isLanguageAllowed(language) {
|
||||||
|
errorText := fmt.Sprintf("unknown language specified: %s", language)
|
||||||
|
log.Println(errorText)
|
||||||
|
http.Error(w, errorText, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
httpUpdateConfigReloadDNSReturnOK(w, r)
|
||||||
|
}
|
28
openapi.yaml
28
openapi.yaml
|
@ -46,6 +46,7 @@ paths:
|
||||||
- 1.1.1.1
|
- 1.1.1.1
|
||||||
- 1.0.0.1
|
- 1.0.0.1
|
||||||
version: "v0.1"
|
version: "v0.1"
|
||||||
|
language: "en"
|
||||||
/enable_protection:
|
/enable_protection:
|
||||||
post:
|
post:
|
||||||
tags:
|
tags:
|
||||||
|
@ -193,6 +194,33 @@ paths:
|
||||||
8.8.8.8: OK
|
8.8.8.8: OK
|
||||||
8.8.4.4: OK
|
8.8.4.4: OK
|
||||||
"192.168.1.104:53535": "Couldn't communicate with DNS server"
|
"192.168.1.104:53535": "Couldn't communicate with DNS server"
|
||||||
|
/i18n/change_language:
|
||||||
|
post:
|
||||||
|
tags:
|
||||||
|
- i18n
|
||||||
|
operationId: changeLanguage
|
||||||
|
summary: "Change current language. Argument must be an ISO 639-1 two-letter code"
|
||||||
|
consumes:
|
||||||
|
- text/plain
|
||||||
|
parameters:
|
||||||
|
- in: body
|
||||||
|
name: language
|
||||||
|
description: "New language. It must be known to the server and must be an ISO 639-1 two-letter code"
|
||||||
|
schema:
|
||||||
|
type: string
|
||||||
|
example: en
|
||||||
|
/i18n/current_language:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- i18n
|
||||||
|
operationId: currentLanguage
|
||||||
|
summary: "Get currently set language. Result is ISO 639-1 two-letter code. Empty result means default language."
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: OK
|
||||||
|
examples:
|
||||||
|
text/plain:
|
||||||
|
en
|
||||||
/stats_top:
|
/stats_top:
|
||||||
get:
|
get:
|
||||||
tags:
|
tags:
|
||||||
|
|
Loading…
Reference in New Issue