Overview
gorestapi is a Go package that mounts a ready-made JSON REST API on top of a gocrud service. A single handler covers list, read, create, update, and delete for every route you register — with no code generation and no framework lock-in.
How it works
- Define your structs and register them with a gocrud service.
- Configure one
Routeper URL path segment. - Mount the handler on any
http.ServeMux.
import (
"github.com/mikolajgasior/gorestapi"
svccrud "github.com/mikolajgasior/gocrud/pkg/service"
)
svc := svccrud.New(registry, db, gocrud.DialectPostgres)
handler := gorestapi.New(svc, gorestapi.Options{
Routes: map[string]gorestapi.Route{
"notes": {},
},
})
mux := http.NewServeMux()
mux.Handle("/api/", http.StripPrefix("/api", http.HandlerFunc(handler.Serve)))
Each registered route automatically exposes:
| Method | URL | Operation |
|---|---|---|
GET |
/{path}/ |
List |
GET |
/{path}/{id} |
Read |
PUT |
/{path}/ |
Create |
PUT |
/{path}/{id} |
Update |
DELETE |
/{path}/{id} |
Delete |
Supported databases
gorestapi delegates all persistence to gocrud, which supports:
| Database | Driver |
|---|---|
| PostgreSQL | github.com/lib/pq |
| SQLite | modernc.org/sqlite |
Installation
go get github.com/mikolajgasior/gorestapi
Demo
See Demo for a complete runnable example covering two resources, ownership hooks, and server-side filters.
Handler reference
See Handler for the full Route, Options, and response format reference.