Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/utilyre/xmate
Package xmate provides missing convenient functionality for net/http
https://github.com/utilyre/xmate
go golang http httperr net-http util utility utils
Last synced: about 8 hours ago
JSON representation
Package xmate provides missing convenient functionality for net/http
- Host: GitHub
- URL: https://github.com/utilyre/xmate
- Owner: utilyre
- License: apache-2.0
- Created: 2023-09-17T09:02:06.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-13T21:31:40.000Z (8 months ago)
- Last Synced: 2024-06-21T14:22:50.554Z (5 months ago)
- Topics: go, golang, http, httperr, net-http, util, utility, utils
- Language: Go
- Homepage: https://pkg.go.dev/github.com/utilyre/xmate/v2
- Size: 35.2 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# xmate
Package xmate provides missing convenient functionality for net/http.
## Usage
Here is a basic example
```go
package mainimport (
"errors"
"log"
"net/http""github.com/utilyre/xmate"
)func main() {
mux := http.NewServeMux()
handler := xmate.ErrorHandler(handleError)mux.HandleFunc("/", handler.HandleFunc(handleIndex))
log.Fatal(http.ListenAndServe(":8080", mux))
}func handleError(w http.ResponseWriter, r *http.Request) {
err := r.Context().Value(xmate.KeyError).(error)if httpErr := new(xmate.HTTPError); errors.As(err, &httpErr) {
_ = xmate.WriteText(w, httpErr.Code, httpErr.Message)
return
}log.Printf("%s %s failed: %v\n", r.Method, r.URL.Path, err)
_ = xmate.WriteText(w, http.StatusInternalServerError, "Internal Server Error")
}func handleIndex(w http.ResponseWriter, r *http.Request) error {
return xmate.WriteText(w, http.StatusOK, "Hello world!")
}
```