add README to example

This commit is contained in:
Curt Tudor 2024-03-26 09:00:02 -06:00
parent 335481f388
commit fb8c49d85e
No known key found for this signature in database
GPG Key ID: B3CD225AF4EC8E96
3 changed files with 75 additions and 4 deletions

View File

@ -0,0 +1,73 @@
# "http-server" SDK Example
This `http-server` example is a minimal `zrok` application that surfaces a basic http server over a private zrok share.
## Implementation
```js
let root = zrok.Load()
```
The `root` is a structure that contains all of the user's environment detail and allows the SDK application to access the `zrok` service instance and the underlying OpenZiti network.
```js
let shr = await zrok.CreateShare(root, new zrok.ShareRequest(zrok.PROXY_BACKEND_MODE, zrok.PRIVATE_SHARE_MODE, "http-server", ["private"]));
console.log(`access your private HTTP Server on another machine using: 'zrok access private ${shr.Token}'`)
```
The `zrok.CreateShare` call uses the loaded `environment` root along with the details of the share request (`zrok.ShareRequest`) to create the share that will be used to access the `http-server`.
We are using the `zrok.PROXY_BACKEND_MODE` to handle tcp traffic. This time we are using `zrok.PRIVATE_SHARE_MODE` to take advantage of a private share that is running. With that we set which frontends to listen on, so we use whatever is configured, `private` here.
Further down we emit where to access the service.
Then we create a NodeJS Express web server that is configured to listen for ONLY incoming Ziti connections. No TCP connections from the open internet are possible.
```js
let app = zrok.express( shr.Token );
...
app.listen(undefined, () => {
console.log(`private HTTP Server is now listening for incoming requests`)
})
...
app.get('/', function(_: Request, res: any){
...
}
```
Then we create a signal handler that catches CTRL-C. When that signal is caught, we tear down teh zrok share, and exit the process.
```js
process.on('SIGINT', async () => {
console.log("Now deleting your private zrok share...")
await zrok.DeleteShare(root, shr)
process.exit(15);
});
```
## How to execute this example
You should first set up and enable your local `zrok` environment. Then execute the following cmds:
```cmd
npm install
node dist/index.js http-server
```
When the example app begins execution, you should see output on the console that resembles the following:
```
Now creating your private zrok share...
access your private HTTP Server on another machine using: 'zrok access private <SOME_TOKEN>'
private HTTP Server is now listening for incoming requests
```
Later, when you are done using the http-server, you can press CTRL_C, and the app will shut down, like this:
```
Now deleting your private zrok share...
```

View File

@ -1,11 +1,10 @@
{
"dependencies": {
"@openziti/ziti-sdk-nodejs": "^0.14.2",
"commander": "^11.1.0",
"express": "^4.18.2",
"path": "^0.12.7",
"readline-sync": "^1.4.10",
"zrok": "../../sdk/dist"
"@openziti/zrok": "^0.1.0"
},
"devDependencies": {
"@types/node": "^20.9.0",

View File

@ -1,11 +1,10 @@
{
"dependencies": {
"@openziti/ziti-sdk-nodejs": "^0.14.2",
"commander": "^11.1.0",
"express": "^4.18.2",
"path": "^0.12.7",
"readline-sync": "^1.4.10",
"zrok": "../../sdk/dist"
"@openziti/zrok": "^0.1.0"
},
"devDependencies": {
"@types/node": "^20.9.0",