rest

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

REST wrapper (JSON)

Implementation

Consider the example in the mock folder as your entity manager. In 5 lines, you can implement the REST handlers for your API:

package main

import (
    "github.com/go-chi/chi"
    "github.com/induzo/rest"
    "github.com/induzo/mock"
)

func main() {
    r := chi.NewRouter()

    // Subrouters:
    m := mock.NewMgr()
    r.Route("/e", func(r chi.Router) {
        r.Get("/", rest.GETListHandler(m))
        r.Post("/", rest.POSTHandler(m))
        r.Get("/{ID}", rest.GETHandler(m))
        r.Patch("/{ID}", rest.PATCHHandler(m))
    })


    srv := &http.Server{
       ReadTimeout:  10 * time.Second,
       WriteTimeout: 7200 * time.Second,
       IdleTimeout:  10 * time.Second,
       Addr:         8080,
       Handler:      r,
    }

    ListenAndServe(srv, conf.ForceInsecureTLS)
}

Benchmarks (i7, 16GB)

    goos: linux
    goarch: amd64
    pkg: github.com/induzo/crud/rest
    BenchmarkPOSTHandler-8            300000              4190 ns/op            1912 B/op         20 allocs/op
    BenchmarkGETListHandler-8         300000              4694 ns/op            2131 B/op         31 allocs/op
    BenchmarkGETHandler-8             500000              2856 ns/op            1138 B/op         14 allocs/op
    BenchmarkDELETEHandler-8         3000000               596 ns/op              80 B/op          2 allocs/op
    BenchmarkPUTHandler-8             300000              4241 ns/op            1842 B/op         21 allocs/op
    BenchmarkPATCHHandler-8           500000              2772 ns/op            1544 B/op         17 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DELETEHandler

func DELETEHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

DELETEHandler will delete a specific entity

func GETHandler

func GETHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

GETHandler returns a unique entity

func GETListHandler

func GETListHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

GETListHandler will handle data from request and returns bytes to be written to response

func GetTestContextWithID

func GetTestContextWithID(
	ctx context.Context,
	ID xid.ID,
) context.Context

GetTestContextWithID will return a context with an xid as chi URL Params

func ListModifiersFromURL

func ListModifiersFromURL(u *url.URL) crud.ListModifiers

ListModifiersFromURL will parse the url into CRUD ListModifiers

func PATCHHandler

func PATCHHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

PATCHHandler will update specific data for a specific entity Following https://tools.ietf.org/html/rfc7386 Content-Type: application/merge-patch+json

func POSTHandler

func POSTHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

POSTHandler will handle data from request and returns bytes to be written to response

func PUTHandler

func PUTHandler(
	cmgr crud.MgrI,
) func(w http.ResponseWriter, r *http.Request)

PUTHandler will update all data for a specific entity

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL