/go-rest

Provides some methods and helpers inspired by REST API Tutorial to create a RESTful service with stdlib in go.

Primary LanguageGoMIT LicenseMIT

go-rest

Go Reference Coverage Status Github Action

go-rest is inspired by the REST API Tutorial. It will help to create a RESTful service with stdlib in go. It's considered to be stable, feature complete, and it will not receive any breaking changes.

One idea of the REST API Tutorial is to have "Wrapped Responses" ( see PDF page 21).

// Wrapped.Response

{
  "code": 200,
  "status": "success",
  "data": {

  }
}

This package provides methods and helpers to render wrapped.Responses and to test them.

// Example Usage

package main

import (
	"net/http"

	"github.com/go-chi/chi/v5"

	"github.com/lanz-dev/go-rest/handler"
	"github.com/lanz-dev/go-rest/rest"
)

func main() {
	c := chi.NewMux() // go-rest doesn't depend on chi!
	c.Route("/api", func(r chi.Router) {
		r.NotFound(handler.NotFoundHandler)
		r.MethodNotAllowed(handler.MethodNotAllowedHandler)

		r.Get("/demo", func(w http.ResponseWriter, r *http.Request) {
			yourStructOrNil := "demo"
			rest.Ok(w, r, yourStructOrNil)
		})
	})
}