1 parent
952587df4f
commit
efcb19aa2f
19 files changed
+276
-189
No files matched your search
@@ -0,0 +1,45 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user