platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
61
selective-vpn-api/app/dns_settings_handlers_upstreams.go
Normal file
61
selective-vpn-api/app/dns_settings_handlers_upstreams.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// EN: `handleDNSUpstreams` is an HTTP handler for dns upstreams.
|
||||
// RU: `handleDNSUpstreams` - HTTP-обработчик для dns upstreams.
|
||||
// ---------------------------------------------------------------------
|
||||
func handleDNSUpstreams(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
writeJSON(w, http.StatusOK, loadDNSUpstreamsConf())
|
||||
case http.MethodPost:
|
||||
var cfg DNSUpstreams
|
||||
if r.Body != nil {
|
||||
defer r.Body.Close()
|
||||
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&cfg); err != nil && err != io.EOF {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := saveDNSUpstreamsConf(cfg); err != nil {
|
||||
http.Error(w, "write error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"ok": true,
|
||||
"cfg": loadDNSUpstreamsConf(),
|
||||
})
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func handleDNSUpstreamPool(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
items := loadDNSUpstreamPoolState()
|
||||
writeJSON(w, http.StatusOK, DNSUpstreamPoolState{Items: items})
|
||||
case http.MethodPost:
|
||||
var body DNSUpstreamPoolState
|
||||
if r.Body != nil {
|
||||
defer r.Body.Close()
|
||||
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil && err != io.EOF {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := saveDNSUpstreamPoolState(body.Items); err != nil {
|
||||
http.Error(w, "write error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, DNSUpstreamPoolState{Items: loadDNSUpstreamPoolState()})
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user