62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
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)
|
|
}
|
|
}
|