Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arussellsaw/crudley
create CRUD REST APIs in Go by implementing a small interface
https://github.com/arussellsaw/crudley
api crud framework golang http library rest
Last synced: 1 day ago
JSON representation
create CRUD REST APIs in Go by implementing a small interface
- Host: GitHub
- URL: https://github.com/arussellsaw/crudley
- Owner: arussellsaw
- Created: 2020-10-26T15:57:54.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-04-03T08:52:37.000Z (almost 4 years ago)
- Last Synced: 2024-04-15T12:12:46.805Z (9 months ago)
- Topics: api, crud, framework, golang, http, library, rest
- Language: Go
- Homepage:
- Size: 2.89 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Crudley
Crudley is a Go library for building CRUD REST APIs on top of document stores. The goal of the project is to make it less labour intensive to build simple APIs, and allow developers to focus on building important stuff.
The project works by having your types implement a `crudley.Model` interface, which provides the library with the basic functions needed to create, read, update, and delete data. The stores are also implemented via a `crudley.Store` interface, which means the database backend can easily be swapped by creating a new `crudley.Store` implementation, or wrapping an existing one.
There are a couple of different places where you can extend the functionality of crudley, but generally i'd recommend writing bespoke API handlers if you want advanced functionality.
* Hooks, these are functions called by the handlers at certain points in the request lifecycle, at the moment only `Hooks.Authorize` is supported for checking authorization.
* Interfaces, pretty well everything in crudley is an interface, so many components can be extended by wrapping and embedding interfaces, overriding specific functions you want to expand on.
```go
// a little example for using this framework
// some example requests:
//
// create a record
// curl -XPOST localhost:3000/api/cool-model/ -d '{"name":"toot-toot"}'
//
// get a list of all records
// curl localhost:3000/api/cool-model/ |jq
//
// get a record by ID
// curl localhost:3000/api/cool-model/{some_id} | jq
//
// get a record by name
// curl 'localhost:3000/api/cool-model/?name=toot-toot' |jq
//
// rename a record
// curl -XPUT localhost:3000/api/cool-model/{some_id} -d '{"name":"foobar"}'package main
import (
"net/http""github.com/gorilla/mux"
"github.com/arussellsaw/crudley"
"github.com/arussellsaw/crudley/stores/mem"
)type MyCoolModel struct {
// crudley.Base implements most standard fields of the interface for you
// if you want more control or different behaviour you can implement
// your own Base model
crudley.Base
Name string
Count int `rest:"immutable"`
}// New creates a new instance of your model
func (m *MyCoolModel) New(id string) crudley.Model {
return &MyCoolModel{Base: crudley.NewBase(id)}
}// GetName returns the name of your model for database tables/collections
func (m *MyCoolModel) GetName() string {
return "mycoolmodel"
}func main() {
r := mux.NewRouter()
s := mem.NewStore()
r.PathPrefix("/api/cool-model/").
Handler(
http.StripPrefix("/api/cool-model",
crudley.NewPath(&MyCoolModel{}, s),
),
)
http.ListenAndServe(":3000", r)
}
```