Skip to content
/ rsvp Public

Opinionated Go web framework that wraps http.Handler with an interface that generates responses from a return value, and performs content negotiation

Notifications You must be signed in to change notification settings

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.msgpack (optional)
    • Other?

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, and I can only ever do it 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

Opinionated Go web framework that wraps http.Handler with an interface that generates responses from a return value, and performs content negotiation

Topics

Resources

Stars

Watchers

Forks