https://github.com/jcyamacho-go/httputil
helpers to create http rest services
https://github.com/jcyamacho-go/httputil
error-handling golang http query-params rest-api
Last synced: 20 days ago
JSON representation
helpers to create http rest services
- Host: GitHub
- URL: https://github.com/jcyamacho-go/httputil
- Owner: jcyamacho-go
- License: mit
- Created: 2023-02-12T02:54:32.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T02:14:53.000Z (almost 3 years ago)
- Last Synced: 2024-06-22T09:52:20.663Z (over 1 year ago)
- Topics: error-handling, golang, http, query-params, rest-api
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# httputil
helpers to create http rest services
## features
- bind json body
- bind form body
- bind xml body
- bind query values
- bind `url.Values`
- custom **error response writer**
- write json response
- write xml response
- write text response
- write blob response
- pprof handler and middleware
## examples
- [example](./example/main.go) project
- handler with json body
```go
var ErrBadRequest = httputil.NewHTTPError(http.StatusBadRequest)
func CreateTaskHandler(svc TaskService) http.Handler {
return httputil.NewHandler(func(w http.ResponseWriter, r *http.Request) error {
var req CreateTaskRequest
if err := httputil.BindJSON(r, &req); err != nil {
return ErrBadRequest.WithCause(err)
}
res, err := svc.Create(r.Context(), req)
if err != nil {
return err
}
return httputil.WriteJSON(w, http.StatusCreated, res)
})
}
```
- handler with query params
```go
func SearchTaskHandler(svc TaskService) http.Handler {
return httputil.NewHandler(func(w http.ResponseWriter, r *http.Request) error {
var req SearchTaskRequest
if err := httputil.BindQuery(r, &req, httputil.WithTagName("json")); err != nil {
return ErrBadRequest.WithCause(err)
}
res, err := svc.Search(r.Context(), req)
if err != nil {
return err
}
return httputil.WriteJSON(w, http.StatusOK, res)
})
}
```
- custom error encoder
```go
func errorEncoder(w http.ResponseWriter, _ *http.Request, err error) {
_ = httputil.WriteString(w, http.StatusInternalServerError, err.Error())
}
func CreateTaskHandler(svc TaskService) http.Handler {
return httputil.NewHandler(func(w http.ResponseWriter, r *http.Request) error {
// ...
}).WithErrorEncoder(errorEncoder)
}
```
- pprof handler
```go
r := chi.NewRouter()
r.Handle("/debug/*", pprof.Handler())
```
- pprof middleware
```go
r := chi.NewRouter()
r.Use(pprof.Middleware)
```