-
Notifications
You must be signed in to change notification settings - Fork 11
Handlers
Stephany Henrique Batista edited this page Jan 8, 2023
·
1 revision
- Analyze error after coding
r := chi.NewRouter()
campaignService := campaign.Service{}
hander := handler.Handler{CampaignService: campaignService}
r.Post("/campaigns", hander.CampaignPost)
http.ListenAndServe(":3000", r)
func (h *Handler) CampaignPost(w http.ResponseWriter, r *http.Request) {
var request contract.NewCampaign
render.DecodeJSON(r.Body, &request)
id, err := h.CampaignService.Create(request)
if err != nil {
http.Error(w, err.Error(), 400)
}
render.Status(r, 202)
render.JSON(w, r, map[string]string{"id": id})
}
- Show the post working
- Create a second handler to show all the campaigns sent
type CampaignRepository struct {
Campaigns []campaign.Campaign
}
func (c *CampaignRepository) Save(campaign *campaign.Campaign) error {
c.Campaigns = append(c.Campaigns, *campaign)
return nil
}
func (c *CampaignRepository) Get() []campaign.Campaign {
return c.Campaigns
}
- Show what is shown when a error on database happens
func (c *CampaignRepository) Save(campaign *campaign.Campaign) error {
c.Campaigns = append(c.Campaigns, *campaign)
return errors.New("error to connect to database")
}
- Create a custom error
type ApplicationError struct {
Err string
}
func (a *ApplicationError) Error() string {
return a.Err
}
//On campaign_post.go
if err != nil {
_, ok := err.(*domain.ApplicationError)
if ok {
http.Error(w, "internal server error", 500)
} else {
http.Error(w, err.Error(), 400)
}
}
- Show how to create a midlewares
- Show the interface http.Handler
- Show what happens if the midleware gets on the end of route
func MyMidlewareCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("before")
next.ServeHTTP(w, r)
println("after")
})
}
- Create a log to show the requests
func MyMidlewareCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("Method ", r.Method, "URL", r.RequestURI)
next.ServeHTTP(w, r)
println("end")
})
}
- Show the middlewares that Chi has done
get github.com/go-chi/chi/middleware
- Explain how to create a wrap handlerfunc
- Create a signature to a custom handler func
go type HandlerFunc func(w http.ResponseWriter, r *http.Request) (any, int, error)
func errorHandler(handler handler.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
obj, status, err := handler(w, r)
if err != nil {
_, ok := err.(*domain.ApplicationError)
if ok {
http.Error(w, "internal server error", 500)
} else {
http.Error(w, err.Error(), 400)
}
return
}
render.Status(r, status)
if obj != nil {
render.JSON(w, r, obj)
}
})
}
func (h *Handler) CampaignPost(w http.ResponseWriter, r *http.Request) (any, int, error) {
println("handler")
var request contract.NewCampaign
render.DecodeJSON(r.Body, &request)
id, err := h.CampaignService.Create(request)
if err != nil {
return nil, 0, err
}
return map[string]string{"id": id}, 202, nil
}