MCPcopy Index your code
hub / github.com/dolanor/rip

github.com/dolanor/rip @v0.9.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.9.2 ↗ · + Follow
435 symbols 1,404 edges 52 files 56 documented · 13%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

RIP ⚰ Go Reference Dagger Go Report Card

REST in peace

gopher resting in peace

TL;DR

Look at the examples.

The smallest/simplest is the one with the GORM provider.

Why?

Creating RESTful API in Go is in a way simple and fun in the first time, but also repetitive and error prone the more resources you handle.
Copy pasting nearly the same code for each resource you want to GET or POST to except for the request and response types is not that cool, and interface{} neither.
Let's get the best of both worlds with GENERICS 🎆 everybody screams 😱

How?

The idea would be to use the classic net/http package with handlers created from Go types.

http.HandleFunc(rip.HandleEntities("/users", NewUserProvider(), nil))

and it would generate all the necessary boilerplate to have some sane (IMO) HTTP routes.

// HandleEntities associates an urlPath with an entity provider, and handles all HTTP requests in a RESTful way:
//
//  POST   /entities/    : creates the entity
//  GET    /entities/:id : get the entity
//  PUT    /entities/:id : updates the entity (needs to pass the full entity data)
//  DELETE /entities/:id : deletes the entity
//  GET    /entities/    : lists the entities (accepts page and page_size query param)
//
// It also handles fields
//
//  GET    /entities/:id/name : get only the name field of the entity
//  PUT    /entities/:id/name : updates only the name entity field

given that UserProvider implements the rip.EntityProvider interface

type EntityProvider[Ent any] interface {
    Create(ctx context.Context, ent Ent) (Ent, error)
    Get(ctx context.Context, id string) (Ent, error)
    Update(ctx context.Context, ent Ent) error
    Delete(ctx context.Context, id string) error
    List(ctx context.Context, offset, limit int) ([]Ent, error)
}

A few providers are already available in providers and allow for 1-line implementation of a CRUD backend for a custom type. With:

    p := gormprovider.New[Album](db, logger)

You have a provider you can pass to rip.HandleEntities() and get a full REST server running, querying and saving data in a database.

⚠️: Disclaimer, the API is not stable yet, use or contribute at your own risks

* Final code may differ from actual shown footage

Play with it

go run github.com/dolanor/rip/examples/srv-example@latest
// open your browser to http://localhost:8888/users/ and start editing users

Features

  • support for multiple encoding automatically selected with Accept and Content-Type headers, or entity extension /entities/1.json
  • JSON
  • protobuf
  • YAML
  • XML
  • msgpack
  • HTML (read version)
  • HTML forms (write version)
  • middlewares
  • automatic generation of HTML forms for live editing of entities

Encoding

You can add your own encoding for your own mime type (I plan on adding some domain type encoding for specific entities, see #13). It is quite easy to create if your encoding API follows generic standard library encoding packages like encoding/json. Here is how encoding/json codec is implemented for RIP

Talks

I gave a talk at GoLab 2023. I presented it again at FOSDEM 2024.

The slides are in my talks repository

(The FOSDEM talk present the more up-to-date API (per-route handler options), demo video (instead of live coding), + a live 3D demo, BUT, I couldn't display my note, so a lot of hesitation and parasite words, sorry about that)

TODO

  • [x] middleware support
  • [ ] I'd like to have more composability in the entity provider (some are read-only, some can't list, some are write only…), haven't figured out the right way to design that, yet.
  • [ ] it should work for nested entities
  • [ ] improve the error API
  • [ ] support for hypermedia discoverability
  • [x] support for multiple data representation
  • [ ] add automatic OpenAPI schema
  • [ ] add automatic API client

Thanks

  • logo from Thierry Pfeiffer

Extension points exported contracts — how you extend this code

Encoder (Interface)
Encoder writes encoded value to an output stream. [4 implementers]
encoding/encoding.go
EntityProvider (Interface)
start EntityProvider OMIT EntityProvider provides entities. An entity is an identifiable resource. Its id should be mars [2 …
entity.go
HandleFuncer (Interface)
HandleFuncer is an interface that matches http.ServeMux's HandleFunc method [1 implementers]
encoding/html/html.go
HTTPServeMux (Interface)
HTTPServeMux is an interface for HTTP multiplexers
router.go
InputOutputFunc (FuncType)
start BackendFunc OMIT InputOutputFunc is a function that takes a ctx and an input, and it can return an output or an er
handler.go
ErrorSourceHeader (Interface)
ErrorSourcePointer allows for a user to document the request header that is creating the error.
error.go
UserRepo (Interface)
(no doc) [1 implementers]
examples/hexagonal-architecture/domain/user_repository.go
EntityRouteOption (FuncType)
(no doc)
route_options.go

Core symbols most depended-on inside this repo

writeError
called by 38
error.go
NewRouteOptions
called by 14
route_options.go
WithCodecs
called by 14
route_options.go
Decode
called by 14
encoding/encoding.go
Error
called by 12
error.go
Encode
called by 10
encoding/encoding.go
HandleFunc
called by 9
router.go
contentNegociateBestHeaderValue
called by 9
header.go

Shape

Function 281
Method 90
Struct 40
Interface 10
FuncType 9
TypeAlias 5

Languages

Go57%
TypeScript43%

Modules by API surface

encoding/html/htmx.org@2.0.2.min.js188 symbols
handler.go23 symbols
error.go19 symbols
route_options.go13 symbols
route.go13 symbols
encoding/html/form.go13 symbols
encoding/protobuf/testdata/user.pb.go12 symbols
encoding/encoding.go12 symbols
router.go11 symbols
examples/hexagonal-architecture/adapters/sqladapter/user_repository.go11 symbols
encoding/html/html.go10 symbols
handle_resource_example_test.go9 symbols

For agents

$ claude mcp add rip \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact