rrda

REST API allowing to perform DNS queries over HTTP
Log | Files | Refs | README | LICENSE

commit 8638159407a650940a5a9658b95ea819c7b4a9e1
parent d0aba26dd7b16b66b6f8238e07ad6a6d13578e6c
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri, 14 Jan 2022 16:21:31 +0100

Switch to using chi for URL routing.

Diffstat:
MREADME.md | 2+-
Mgo.mod | 2+-
Mgo.sum | 4++--
Mrrda.go | 22+++++++++++-----------
4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/README.md b/README.md @@ -25,8 +25,8 @@ RRDA is a recursive acronym for "RRDA REST DNS API". RRDA requires the following Go libraries: +- chi: lightweight, idiomatic and composable router - https://github.com/go-chi/chi - dns: DNS library in Go - https://github.com/miekg/dns -- pat: pattern muxer for Go - https://github.com/bmizerany/pat ## Installation diff --git a/go.mod b/go.mod @@ -3,7 +3,7 @@ module rrda go 1.13 require ( - github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f + github.com/go-chi/chi/v5 v5.0.7 github.com/miekg/dns v1.1.45 golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d ) diff --git a/go.sum b/go.sum @@ -1,5 +1,5 @@ -github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f h1:gOO/tNZMjjvTKZWpY7YnXC72ULNLErRtp94LountVE8= -github.com/bmizerany/pat v0.0.0-20210406213842-e4b6760bdd6f/go.mod h1:8rLXio+WjiTceGBHIoTvn60HIbs7Hm7bcHjyrSqYB9c= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/miekg/dns v1.1.45 h1:g5fRIhm9nx7g8osrAvgb16QJfmyMsyOCb+J7LSv+Qzk= github.com/miekg/dns v1.1.45/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= diff --git a/rrda.go b/rrda.go @@ -18,7 +18,7 @@ import ( "encoding/json" "flag" "fmt" - "github.com/bmizerany/pat" + "github.com/go-chi/chi/v5" "github.com/miekg/dns" "golang.org/x/net/idna" "io" @@ -132,9 +132,9 @@ Redo: // Handler for DNS queries func query(w http.ResponseWriter, r *http.Request) { - server := r.URL.Query().Get(":server") - domain := dns.Fqdn(r.URL.Query().Get(":domain")) - querytype := r.URL.Query().Get(":querytype") + server := chi.URLParam(r, "server") + domain := dns.Fqdn(chi.URLParam(r, "domain")) + querytype := chi.URLParam(r, "querytype") if domain, err := idna.ToASCII(domain); err == nil { // Valid domain name (ASCII or IDN) if _, isDomain := dns.IsDomainName(domain); isDomain { // Well-formed domain name @@ -153,8 +153,8 @@ func query(w http.ResponseWriter, r *http.Request) { // Handler for reverse DNS queries func ptr(w http.ResponseWriter, r *http.Request) { - server := r.URL.Query().Get(":server") - ip := r.URL.Query().Get(":ip") + server := chi.URLParam(r, "server") + ip := chi.URLParam(r, "ip") if arpa, err := dns.ReverseAddr(ip); err == nil { // Valid IP address (IPv4 or IPv6) resolve(w, r, server, arpa, dns.TypePTR) @@ -190,19 +190,19 @@ func main() { fmt.Println("Listening on ("+mode+" mode):", address) - m := pat.New() - m.Get("/:server/x/:ip", http.HandlerFunc(ptr)) - m.Get("/:server/:domain/:querytype", http.HandlerFunc(query)) + r := chi.NewRouter() + r.Get("/{server}/x/{ip}", ptr) + r.Get("/{server}/{domain}/{querytype}", query) if *fastcgi { listener, _ := net.Listen("tcp", address) - if err := fcgi.Serve(listener, m); err != nil { + if err := fcgi.Serve(listener, r); err != nil { fmt.Println("\nERROR:", err) os.Exit(1) } } else { - if err := http.ListenAndServe(address, m); err != nil { + if err := http.ListenAndServe(address, r); err != nil { fmt.Println("\nERROR:", err) os.Exit(1) }