refactor out into packages for easier unit test writing

This commit is contained in:
Jon Lundy
2020-12-04 11:36:24 -07:00
parent 9db6377526
commit c00d091ed2
18 changed files with 559 additions and 338 deletions

37
pkg/app/dns/dns.go Normal file
View File

@@ -0,0 +1,37 @@
package app_dns
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"github.com/go-chi/chi"
)
type app struct {
resolver *net.Resolver
}
func New(ctx context.Context) *app {
return &app{resolver: net.DefaultResolver}
}
func (app *app) getDNS(w http.ResponseWriter, r *http.Request) {
domain := chi.URLParam(r, "domain")
w.Header().Set("Content-Type", "text/plain")
res, err := app.resolver.LookupTXT(r.Context(), domain)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
return
}
fmt.Fprintln(w, strings.Join(res, "\n"))
}
func (app *app) Routes(r *chi.Mux) {
r.MethodFunc("GET", "/dns/{domain}", app.getDNS)
}