Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

README

        

# xmate

Package xmate provides missing convenient functionality for net/http.

## Usage

Here is a basic example

```go
package main

import (
"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!")
}
```