Skip to content

Teajey/rsvp

Repository files navigation

RSVP

My "functional" wrapper around Golang's net/http server stuff

The default net/http interface:

type Handler interface {
	ServeHTTP(ResponseWriter, *Request)
}

RSVP's interface:

type Handler interface {
	ServeHTTP(h http.Header, r *http.Request) Response
}

Features

  • Content Negotiation. RSVP will attempt to provide the data in a supported media-type that is requested via the Accept header, or even the URL's file extension in the case of GET requests:
    • application/json
    • text/html
    • text/plain
    • application/octet-stream
    • application/xml
    • application/vnd.golang.gob (Golang's encoding/gob)
    • application/vnd.msgpack (optional extension behind -tags=rsvp_msgpack)
    • Others?

It's easy for me to lose track of what I've written to http.ResponseWriter. Occasionally receiving the old http: multiple response.WriteHeader calls

With this library I just return a value, which I can only ever do once, to execute an HTTP response write. Why write responses with a weird mutable reference from goodness knows where? YEUCH!

Having to remember to return separately from resolving the response? *wretch*

if r.Method != http.MethodPut {
	http.Error(w, "Use PUT", http.StatusMethodNotAllowed)
	return
}

Not with RSVP 🫠

if r.Method != http.MethodPut {
	return rsvp.Response{Status: http.StatusMethodNotAllowed, Body: "Use PUT"}
}

(Wrapping this with your own convenience method, i.e. func ErrorMethodNotAllowed(message string) rsvp.Response is encouraged. You get to decide for yourself how errors are represented)

Examples

About

An http.Handler wrapper with a return value and content negotiation

Topics

Resources

Stars

Watchers

Forks