feat: API Handlers
This commit is contained in:
parent
b753631012
commit
f389c9c876
8 changed files with 249 additions and 71 deletions
|
|
@ -1,82 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mr-karan/doggo/internal/app"
|
||||
"github.com/mr-karan/doggo/pkg/utils"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/mr-karan/doggo/pkg/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
logger = utils.InitLogger()
|
||||
k = koanf.New(".")
|
||||
ko = koanf.New(".")
|
||||
// Version and date of the build. This is injected at build-time.
|
||||
buildVersion = "unknown"
|
||||
buildDate = "unknown"
|
||||
)
|
||||
|
||||
type resp struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
initConfig()
|
||||
|
||||
// Initialize app.
|
||||
app := app.New(logger, buildVersion)
|
||||
|
||||
// Register handles.
|
||||
r := chi.NewRouter()
|
||||
r.Get("/", wrap(app, handleIndex))
|
||||
r.Get("/ping/", wrap(app, handleHealthCheck))
|
||||
r.Post("/lookup/", wrap(app, handleLookup))
|
||||
|
||||
// Setup middlewares.
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
sendSuccessResponse("Welcome to Doggo DNS!", w)
|
||||
return
|
||||
})
|
||||
|
||||
r.Get("/ping/", func(w http.ResponseWriter, r *http.Request) {
|
||||
sendSuccessResponse("PONG", w)
|
||||
return
|
||||
})
|
||||
|
||||
r.Post("/lookup/", func(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
})
|
||||
|
||||
http.ListenAndServe(":3000", r)
|
||||
}
|
||||
|
||||
// sendResponse sends an HTTP success response.
|
||||
func sendResponse(data interface{}, statusText string, status int, w http.ResponseWriter) {
|
||||
w.WriteHeader(status)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
out, err := json.Marshal(resp{Status: statusText, Data: data})
|
||||
if err != nil {
|
||||
sendErrorResponse("Internal Server Error", http.StatusInternalServerError, nil, w)
|
||||
return
|
||||
// HTTP Server.
|
||||
srv := &http.Server{
|
||||
Addr: ko.String("server.address"),
|
||||
Handler: r,
|
||||
ReadTimeout: ko.Duration("server.read_timeout") * time.Millisecond,
|
||||
WriteTimeout: ko.Duration("server.write_timeout") * time.Millisecond,
|
||||
IdleTimeout: ko.Duration("server.keepalive_timeout") * time.Millisecond,
|
||||
}
|
||||
|
||||
_, _ = w.Write(out)
|
||||
}
|
||||
|
||||
// sendSuccessResponse sends an HTTP success (200 OK) response.
|
||||
func sendSuccessResponse(data interface{}, w http.ResponseWriter) {
|
||||
sendResponse(data, "success", http.StatusOK, w)
|
||||
}
|
||||
|
||||
// sendErrorResponse sends an HTTP error response.
|
||||
func sendErrorResponse(message string, status int, data interface{}, w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
|
||||
resp := resp{Status: "error",
|
||||
Message: message,
|
||||
Data: data}
|
||||
|
||||
out, _ := json.Marshal(resp)
|
||||
|
||||
_, _ = w.Write(out)
|
||||
logger.WithFields(logrus.Fields{
|
||||
"address": srv.Addr,
|
||||
}).Info("starting server")
|
||||
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
logger.Fatalf("couldn't start server: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue