-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from jirenius/feature/g-52-hello-world-example
GH-52: Added 01-hello-world example.
- Loading branch information
Showing
31 changed files
with
1,041 additions
and
766 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Hello World Example | ||
|
||
**Tags:** *Models* | ||
|
||
## Description | ||
Simple service serving a message to the world. | ||
|
||
## Prerequisite | ||
|
||
* [Download](https://golang.org/dl/) and install Go | ||
* [Install](https://resgate.io/docs/get-started/installation/) *NATS Server* and *Resgate* (done with 3 docker commands). | ||
|
||
## Install and run | ||
|
||
```text | ||
git clone https://github.com/jirenius/go-res | ||
cd go-res/examples/01-hello-world | ||
go run main.go | ||
``` | ||
|
||
## Things to try out | ||
|
||
### Access API through HTTP | ||
* Open the browser and go to: | ||
```text | ||
http://localhost:8080/api/example/model | ||
``` | ||
|
||
### Access API through WebSocket | ||
* Open *Chrome* and go to [resgate.io - resource viewer](https://resgate.io/viewer). | ||
* Type in the resource ID below, and click *View*: | ||
```text | ||
example.model | ||
``` | ||
> **Note** | ||
> | ||
> Chrome allows websites to connect to *localhost*, while other browsers may give a security error. | ||
### Real time update on static resource | ||
* Stop the project, and change the `"Hello, World!"` message in *main.go*. | ||
* Restart the project and observe how the message is updated in the viewer (see [above](#access-api-through-websocket)). | ||
|
||
### Get resource with ResClient | ||
* In the [resgate.io - resource viewer](https://resgate.io/viewer), open the DevTools console (*Ctrl+Shift+J*). | ||
* Type the following command, and press *Enter*: | ||
```javascript | ||
client.get("example.model").then(m => console.log(m.message)); | ||
``` | ||
> **Note** | ||
> | ||
> The resource viewer app stores the *ResClient* instance in the global `client` variable, for easy access. | ||
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,16 @@ | ||
package main | ||
|
||
import res "github.com/jirenius/go-res" | ||
|
||
func main() { | ||
s := res.NewService("example") | ||
s.Handle("model", | ||
res.Access(res.AccessGranted), | ||
res.GetModel(func(r res.ModelRequest) { | ||
r.Model(map[string]string{ | ||
"message": "Hello, World!", | ||
}) | ||
}), | ||
) | ||
s.ListenAndServe("nats://localhost:4222") | ||
} |
File renamed without changes.
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,60 @@ | ||
# Edit Text Example | ||
|
||
**Tags:** *Models*, *Call methods*, *Client subscriptions* | ||
|
||
## Description | ||
A simple text field that can be edited by multiple clients simultaneously. | ||
|
||
## Prerequisite | ||
|
||
* [Download](https://golang.org/dl/) and install Go | ||
* [Install](https://resgate.io/docs/get-started/installation/) *NATS Server* and *Resgate* (done with 3 docker commands). | ||
|
||
## Install and run | ||
|
||
```text | ||
git clone https://github.com/jirenius/go-res | ||
cd go-res/examples/02-edit-text | ||
go run main.go | ||
``` | ||
|
||
Open the client | ||
``` | ||
http://localhost:8082 | ||
``` | ||
|
||
## Things to try out | ||
|
||
### Realtime updates | ||
* Open the client in two separate tabs. | ||
* Edit the message in one tab, and observe realtime updates in both. | ||
|
||
### System reset | ||
* Stop the service. | ||
* Edit the default text (`"Hello, Go World!"`) in *main.go*. | ||
* Restart the service to observe resetting of the message in all clients. | ||
|
||
## API | ||
|
||
Request | Resource | Description | ||
--- | --- | --- | ||
*get* | `text.shared` | Simple model. | ||
*call* | `text.shared.set` | Sets the model's *message* property. | ||
|
||
## REST API | ||
|
||
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/text/shared | ||
``` | ||
|
||
### Update model | ||
``` | ||
POST http://localhost:8080/api/text/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Edit Text Example</title> | ||
<link rel="icon" href="data:,"> | ||
<script src="https://cdn.jsdelivr.net/npm/resclient@latest/dist/resclient.min.js"></script> | ||
</head> | ||
<body> | ||
<p>Try running the client in two separate tabs!</p> | ||
<p>Web resource: <a href="http://localhost:8080/api/text/shared" target="_blank">http://localhost:8080/api/text/shared</a></p> | ||
<hr /> | ||
<div id="root"><input id="input" /></div> | ||
<script> | ||
const ResClient = resclient.default; | ||
|
||
// Creating the client instance. | ||
let client = new ResClient('ws://localhost:8080'); | ||
|
||
// Get the model from the service. | ||
client.get('text.shared').then(model => { | ||
// Set value of the input element | ||
input.value = model.message; | ||
|
||
// Listen for user input and call set to update the remote model. | ||
input.addEventListener('input', () => { | ||
model.set({ message: input.value }); | ||
}); | ||
|
||
// Listen for model change events. | ||
// The model will be unsubscribed after calling model.off | ||
model.on('change', () => { | ||
input.value = model.message; | ||
}); | ||
}).catch(err => { | ||
root.textContent = err.code == 'system.connectionError' | ||
? "Connection error. Are NATS Server and Resgate running?" | ||
: err.message; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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
File renamed without changes.
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,54 @@ | ||
# Edit Text Persisted Example | ||
|
||
**Tags:** *Models*, *Call methods*, *Client subscriptions*, *Persistence* | ||
|
||
## Description | ||
This is the Edit Text example where all changes are persisted using the BadgerDB middleware. With a database middleware, both clients and database can be updated with a single event. | ||
|
||
## Prerequisite | ||
|
||
* [Download](https://golang.org/dl/) and install Go | ||
* [Install](https://resgate.io/docs/get-started/installation/) *NATS Server* and *Resgate* (done with 3 docker commands). | ||
|
||
## Install and run | ||
|
||
```text | ||
git clone https://github.com/jirenius/go-res | ||
cd go-res/examples/03-edit-text-persisted | ||
go run main.go | ||
``` | ||
|
||
Open the client | ||
``` | ||
http://localhost:8083 | ||
``` | ||
|
||
## Things to try out | ||
|
||
### BadgerDB persistance | ||
Run the client and make edits to the text. Restart the service and observe all changes are persisted. | ||
|
||
## API | ||
|
||
Request | Resource | Description | ||
--- | --- | --- | ||
*get* | `text.shared` | Simple model. | ||
*call* | `text.shared.set` | Sets the model's *message* property. | ||
|
||
## REST API | ||
|
||
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/text/shared | ||
``` | ||
|
||
### Update model | ||
``` | ||
POST http://localhost:8080/api/text/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Edit Text Persisted Example</title> | ||
<link rel="icon" href="data:,"> | ||
<script src="https://cdn.jsdelivr.net/npm/resclient@latest/dist/resclient.min.js"></script> | ||
</head> | ||
<body> | ||
<p>Try running the client in two separate tabs!</p> | ||
<p>Web resource: <a href="http://localhost:8080/api/text/shared" target="_blank">http://localhost:8080/api/text/shared</a></p> | ||
<hr /> | ||
<div id="root"><input id="input" /></div> | ||
<script> | ||
const ResClient = resclient.default; | ||
|
||
// Creating the client instance. | ||
let client = new ResClient('ws://localhost:8080'); | ||
|
||
// Get the model from the service. | ||
client.get('text.shared').then(model => { | ||
// Set value of the input element | ||
input.value = model.message; | ||
|
||
// Listen for user input and call set to update the remote model. | ||
input.addEventListener('input', () => { | ||
model.set({ message: input.value }); | ||
}); | ||
|
||
// Listen for model change events. | ||
// The model will be unsubscribed after calling model.off | ||
model.on('change', () => { | ||
input.value = model.message; | ||
}); | ||
}).catch(err => { | ||
root.textContent = err.code == 'system.connectionError' | ||
? "Connection error. Are NATS Server and Resgate running?" | ||
: err.message; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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
File renamed without changes.
Oops, something went wrong.