46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"dns_service/utils"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func SettingsFile(w http.ResponseWriter, req *http.Request) {
|
|
configPath := os.Getenv("DNSMASQ_CONFIG")
|
|
if configPath == "" {
|
|
http.Error(w, "DNSMASQ_CONFIG environment variable not set", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
switch req.Method {
|
|
case "GET":
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
http.Error(w, "failed to read dnsmasq config file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.Write(data)
|
|
case "POST":
|
|
bodyBytes, err := io.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, "failed to read request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := os.WriteFile(configPath, bodyBytes, 0644); err != nil {
|
|
http.Error(w, "failed to write dnsmasq config file", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if err := utils.RestartDnsmasq(); err != nil {
|
|
http.Error(w, "failed to restart dnsmasq", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
w.Write(bodyBytes)
|
|
default:
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
}
|
|
}
|