mirror of
https://github.com/DevVoxel/vectordns-server.git
synced 2026-02-27 09:47:37 +00:00
42 lines
1003 B
Go
42 lines
1003 B
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/vectordns/server/internal/dns"
|
|
)
|
|
|
|
func DNSLookup(w http.ResponseWriter, r *http.Request) {
|
|
var req dns.LookupRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body", "INVALID_BODY")
|
|
return
|
|
}
|
|
|
|
if req.Domain == "" {
|
|
writeError(w, http.StatusBadRequest, "domain is required", "MISSING_DOMAIN")
|
|
return
|
|
}
|
|
|
|
resp, err := dns.Lookup(req)
|
|
if err != nil {
|
|
slog.Error("dns lookup failed", "domain", req.Domain, "error", err)
|
|
writeError(w, http.StatusInternalServerError, "dns lookup failed", "LOOKUP_FAILED")
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(resp)
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, msg, code string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"error": msg,
|
|
"code": code,
|
|
})
|
|
}
|