Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-34: Improved middleware documentation and READMEs
- Loading branch information
Showing
10 changed files
with
200 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Edit Text BadgerDB Example | ||
|
||
This is the Edit Text example where all changes are persisted using the BadgerDB middleware. | ||
By using the BadgerDB middleware, both clients and database can be updated with a single event. | ||
|
||
* It exposes a single resource: `example.shared`. | ||
* It allows setting the resource's `message` property through the `set` method. | ||
* The middleware adds a GetResource handler that loads the resource from the database. | ||
* The middleware adds a ApplyChange handler that updates the database on change events. | ||
* It persists all changes to a local BadgerDB database under `./db`. | ||
* It serves a web client at http://localhost:8082 | ||
|
||
## Prerequisite | ||
|
||
* Have [NATS Server](https://nats.io/download/nats-io/gnatsd/) and [Resgate](https://github.com/resgateio/resgate) running | ||
|
||
## Install and run | ||
|
||
Clone go-res repository and run example: | ||
```bash | ||
git clone https://github.com/jirenius/go-res | ||
cd go-res/examples/edit-text-badgerdb | ||
go run main.go | ||
``` | ||
|
||
Open the client | ||
``` | ||
http://localhost:8082 | ||
``` | ||
|
||
## Things to try out | ||
|
||
**BadgerDB persistance** | ||
Run the client and make edits to the text. Restart the service and observe all changes are persisted. | ||
|
||
## Web resources | ||
|
||
Resources can be retrieved using ordinary HTTP GET requests, and methods can be called using HTTP POST requests. | ||
|
||
### Get model | ||
``` | ||
GET http://localhost:8080/api/example/shared | ||
``` | ||
|
||
### Update model | ||
``` | ||
POST http://localhost:8080/api/example/shared/set | ||
``` | ||
*Body* | ||
``` | ||
{ "message": "Updated through HTTP" } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Package middleware provides middleware for the res package: | ||
https://github.com/jirenius/go-res | ||
Middleware can be used for adding handler functions to a res.Handler, | ||
to perform tasks such as: | ||
* storing, loading and updating persisted data | ||
* synchronize changes between multiple service instances | ||
* add additional logging | ||
* provide helpers for complex live queries | ||
Currently, only the BadgerDB middleware is created, to demonstrate | ||
database persistance. | ||
Usage | ||
Add middleware to a resource: | ||
s.Handle("user.$id", | ||
middlware.BadgerDB{DB: db}, | ||
) | ||
*/ | ||
package middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"github.com/dgraph-io/badger" | ||
res "github.com/jirenius/go-res" | ||
"github.com/jirenius/go-res/middleware" | ||
) | ||
|
||
func ExampleBadgerDB() { | ||
db := &badger.DB{} // Dummy. Use badger.Open | ||
|
||
s := res.NewService("directory") | ||
s.Handle("user.$id", | ||
res.Model, | ||
middleware.BadgerDB{DB: db}, | ||
/* ... */ | ||
) | ||
} | ||
|
||
func ExampleBadgerDB_WithType() { | ||
db := &badger.DB{} // Dummy. Use badger.Open | ||
|
||
type User struct { | ||
ID int | ||
Name string | ||
} | ||
|
||
s := res.NewService("directory") | ||
badgerDB := middleware.BadgerDB{DB: db} | ||
s.Handle("user.$id", | ||
res.Collection, | ||
badgerDB.WithType(User{}), | ||
res.Set(func(r res.CallRequest) { | ||
_ = r.RequireValue().(User) | ||
/* ... */ | ||
r.OK(nil) | ||
}), | ||
) | ||
} | ||
|
||
func ExampleBadgerDB_WithDefault() { | ||
db := &badger.DB{} // Dummy. Use badger.Open | ||
|
||
s := res.NewService("directory") | ||
badgerDB := middleware.BadgerDB{DB: db} | ||
s.Handle("users", | ||
res.Collection, | ||
// Default to an empty slice of references | ||
badgerDB.WithType([]res.Ref{}).WithDefault([]res.Ref{}), | ||
/* ... */ | ||
) | ||
} |