mirror of
https://github.com/DevVoxel/vectordns-server.git
synced 2026-02-27 01:40:12 +00:00
Initial commit: Go DNS microservice boilerplate
This commit is contained in:
41
internal/handler/dns.go
Normal file
41
internal/handler/dns.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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,
|
||||
})
|
||||
}
|
||||
14
internal/handler/health.go
Normal file
14
internal/handler/health.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Health(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"status": "ok",
|
||||
"version": "0.1.0",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user