chio contains useful functions to use in conjunction with go-chi/chi router.
example
import (
"github.com/go-chi/chi/v5"
"github.com/FallenTaters/chio"
)
func main() {
r := chi.NewRouter()
r.Get(`/`, chio.JSON(handler))
// ...
}
type myStruct struct{
A int `json:"a"`
}
func handler(w http.ResponseWriter, r *http.Request, v myStruct) {
// ...
}
if the body cannot be read and decoded, http.StatusUnprocessableEntity is returned.
example
import (
"github.com/go-chi/chi/v5"
"github.com/FallenTaters/chio"
)
func main() {
r := chi.NewRouter()
r.Get(`/`, handler)
// ...
}
func handler(w http.ResponseWriter, r *http.Request) {
chio.WriteJSON(w, http.StatusOK, myStruct{3})
}
writes the status code and sets the Content-Length header to 0
writes s to the body and sets the Content-Type to text/plain
writes v to the body as WriteJSON and sets the Content-Type to application/json. If marshalling fails, it panics.
writes v to the body as WriteXML and sets the Content-Type to application/xml. If marshalling fails, it panics.
copies data from r to w
writes v to the body
example
import (
"github.com/go-chi/chi/v5"
"github.com/FallenTaters/chio/middleware"
)
func main() {
r := chi.NewRouter()
r.Use(middleware.Recover(middleware.DefaultPanicLogger(os.Stderr)))
// ...
}
func handler(w http.ResponseWriter, r *http.Request) {
chio.WriteJSON(w, http.StatusOK, myStruct{3})
}
A simple middleware handler for adding basic http auth to a route. Authorize should return true if the username and password are correct, otherwise 401 will be returned. The returned contextKey and contextValue can be used to set values on the request context, for example a user struct. Values can be retrieved easily using middleware.GetValue.
Catches a panic and passes the value and a stack trace to f.
Sets a value on the request context for use in later middlewares or handlers. If get returns false, it is assumed something went wrong and the correct response has already been written. If get returns true, v will be set under k in the request context and the underlying handler is called. The value can be retrieved and type-asserted easily using GetValue.