unfinished mess
This commit is contained in:
parent
6c3b17ba0d
commit
2e2e3b1ec8
26 changed files with 364 additions and 171 deletions
|
@ -6,7 +6,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mr-karan/doggo/internal/app"
|
||||
"github.com/mr-karan/doggo/internal/hub"
|
||||
"github.com/mr-karan/doggo/pkg/utils"
|
||||
"github.com/mr-karan/logf"
|
||||
|
||||
|
@ -31,7 +31,7 @@ func main() {
|
|||
initConfig()
|
||||
|
||||
// Initialize app.
|
||||
app := app.New(logger, buildString)
|
||||
app := hub.New(logger, buildString)
|
||||
|
||||
// Register router instance.
|
||||
r := chi.NewRouter()
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mr-karan/doggo/internal/app"
|
||||
"github.com/mr-karan/doggo/internal/hub"
|
||||
"github.com/mr-karan/doggo/pkg/models"
|
||||
"github.com/mr-karan/doggo/pkg/resolvers"
|
||||
)
|
||||
|
@ -21,7 +21,7 @@ type httpResp struct {
|
|||
|
||||
func handleIndexAPI(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
app = r.Context().Value("app").(app.App)
|
||||
app = r.Context().Value("app").(hub.Hub)
|
||||
)
|
||||
|
||||
sendResponse(w, http.StatusOK, fmt.Sprintf("Welcome to Doggo API. Version: %s", app.Version))
|
||||
|
@ -35,21 +35,21 @@ func handleHealthCheck(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func handleLookup(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
app = r.Context().Value("app").(app.App)
|
||||
app = r.Context().Value("app").(hub.Hub)
|
||||
)
|
||||
|
||||
// Read body.
|
||||
b, err := ioutil.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Error("error reading request body")
|
||||
app.Log.WithError(err).Error("error reading request body")
|
||||
sendErrorResponse(w, fmt.Sprintf("Invalid JSON payload"), http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
// Prepare query flags.
|
||||
var qFlags models.QueryFlags
|
||||
if err := json.Unmarshal(b, &qFlags); err != nil {
|
||||
app.Logger.WithError(err).Error("error unmarshalling payload")
|
||||
app.Log.WithError(err).Error("error unmarshalling payload")
|
||||
sendErrorResponse(w, fmt.Sprintf("Invalid JSON payload"), http.StatusBadRequest, nil)
|
||||
return
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
|
|||
// Load Nameservers.
|
||||
err = app.LoadNameservers()
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Error("error loading nameservers")
|
||||
app.Log.WithError(err).Error("error loading nameservers")
|
||||
sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
@ -82,10 +82,10 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
|
|||
SearchList: app.ResolverOpts.SearchList,
|
||||
Ndots: app.ResolverOpts.Ndots,
|
||||
Timeout: app.QueryFlags.Timeout * time.Second,
|
||||
Logger: app.Logger,
|
||||
Logger: app.Log,
|
||||
})
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Error("error loading resolver")
|
||||
app.Log.WithError(err).Error("error loading resolver")
|
||||
sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
|
|||
for _, rslv := range app.Resolvers {
|
||||
resp, err := rslv.Lookup(q)
|
||||
if err != nil {
|
||||
app.Logger.WithError(err).Error("error looking up DNS records")
|
||||
app.Log.WithError(err).Error("error looking up DNS records")
|
||||
sendErrorResponse(w, fmt.Sprintf("Error looking up for records."), http.StatusInternalServerError, nil)
|
||||
return
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ func handleLookup(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// wrap is a middleware that wraps HTTP handlers and injects the "app" context.
|
||||
func wrap(app app.App, next http.HandlerFunc) http.HandlerFunc {
|
||||
func wrap(app hub.Hub, next http.HandlerFunc) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), "app", app)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue