Skip to content

Repository files navigation

Tea

GoDocTravisCoveralls githubReport CardDependabot Status

Tea is a utility library intended to help improve the flow of your Go HTTP servers. Tea goes well with Chi, but works well with the standard lib.

Docs

Godoc

Example

package main
import (
"net/http""github.com/go-chi/chi""github.com/blockloop/tea"
)
funcmain() {
r:=chi.NewRouter()
// use the JSON handler provided by tear.NotFound(tea.NotFound)
r.Get("/brewery/{id}", tea.Handler(GetBrewery))
r.Post("/brewery", tea.Handler(PostBrewery))
http.ListenAndServe(":3000", r)
}
funcGetBrewery(w http.ResponseWriter, r*http.Request) (int, interface{}) {
id, err:=tea.URLInt(r, "id", "required,gt=0")
iferr!=nil {
returntea.StatusError(404)
}
brewery, err:=db.GetBrewery(id)
iferr!=nil {
log.WithError(err).Error("failed to get brewery from db")
returntea.StatusError(500)
}
return200, brewery
}
typePostBreweryRequeststruct {
Namestring`json:"name" validate:"required"`Citystring`json:"city" validate:"required"`
}
funcPostBrewery(w http.ResponseWriter, r*http.Request) (int, interface{}) {
varreqPostBreweryRequest// parse the request body as JSON and validate it's struct fieldserr:=tea.Body(r, &req)
iferr!=nil {
returntea.Error(400, err)
}
// create with dbiferr!=nil {
log.WithError(err).Error("failed to create brewery")
returntea.StatusError(500)
}
return200, brewery
}

About

A utility library for better HTTP servers

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages