forked from ngrok/ngrok-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Go and Rust forwarding examples (ngrok#774)
- Loading branch information
1 parent
284a64d
commit 29f8467
Showing
6 changed files
with
134 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
:::info | ||
```go | ||
import ( | ||
"context" | ||
"net/url" | ||
|
||
Forwarding to an upstream service is not supported by the Go SDK | ||
"golang.ngrok.com/ngrok" | ||
"golang.ngrok.com/ngrok/config" | ||
) | ||
|
||
::: | ||
func ngrokForwarder(ctx context.Context) (ngrok.Forwarder, error) { | ||
backendUrl, err := url.Parse("https://localhost:8443") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ngrok.ListenAndForward(ctx, | ||
backendUrl, | ||
config.HTTPEndpoint(), | ||
ngrok.WithAuthtokenFromEnv(), | ||
) | ||
} | ||
``` | ||
|
||
For HTTP/2 Use: `config.HTTPEndpoint(config.WithAppProtocol("http2"))` | ||
|
||
Go Package Docs: | ||
|
||
- [https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward](https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward) |
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 |
---|---|---|
@@ -1,5 +1,26 @@ | ||
:::info | ||
```go | ||
import ( | ||
"context" | ||
"net/url" | ||
|
||
Forwarding to a non-local address is not supported by the Go SDK | ||
"golang.ngrok.com/ngrok" | ||
"golang.ngrok.com/ngrok/config" | ||
) | ||
|
||
::: | ||
func ngrokForwarder(ctx context.Context) (ngrok.Forwarder, error) { | ||
backendUrl, err := url.Parse("http://192.168.1.2:80") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ngrok.ListenAndForward(ctx, | ||
backendUrl, | ||
config.HTTPEndpoint(), | ||
ngrok.WithAuthtokenFromEnv(), | ||
) | ||
} | ||
``` | ||
|
||
Go Package Docs: | ||
|
||
- [https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward](https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward) |
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 |
---|---|---|
@@ -1,5 +1,26 @@ | ||
:::info | ||
```go | ||
import ( | ||
"context" | ||
"net/url" | ||
|
||
Forwarding to a non-local address is not supported by the Go SDK | ||
"golang.ngrok.com/ngrok" | ||
"golang.ngrok.com/ngrok/config" | ||
) | ||
|
||
::: | ||
func ngrokForwarder(ctx context.Context) (ngrok.Forwarder, error) { | ||
backendUrl, err := url.Parse("tcp://192.168.1.2:80") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ngrok.ListenAndForward(ctx, | ||
backendUrl, | ||
config.TCPEndpoint(), | ||
ngrok.WithAuthtokenFromEnv(), | ||
) | ||
} | ||
``` | ||
|
||
Go Package Docs: | ||
|
||
- [https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward](https://pkg.go.dev/golang.ngrok.com/ngrok#ListenAndForward) |
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 |
---|---|---|
@@ -1,5 +1,22 @@ | ||
:::info | ||
```rust | ||
use ngrok::prelude::*; | ||
use ngrok::tunnel; | ||
use ngrok::forwarder::Forwarder; | ||
use url::Url; | ||
|
||
Forwarding to an upstream service is not supported by the Rust SDK | ||
async fn forward_ngrok() -> Result<Forwarder<tunnel::HttpTunnel>, Error> { | ||
let sess = ngrok::Session::builder() | ||
.authtoken_from_env() | ||
.connect() | ||
.await?; | ||
sess | ||
.http_endpoint() | ||
.listen_and_forward(Url::parse("https://localhost:8443")?) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
``` | ||
|
||
::: | ||
Rust Crate Docs: | ||
|
||
- [https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.HttpTunnelBuilder.html#method.listen_and_forward](https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.HttpTunnelBuilder.html#method.listen_and_forward) |
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 |
---|---|---|
@@ -1,5 +1,22 @@ | ||
:::info | ||
```rust | ||
use ngrok::prelude::*; | ||
use ngrok::tunnel; | ||
use ngrok::forwarder::Forwarder; | ||
use url::Url; | ||
|
||
Forwarding to a non-local address is not supported by the Rust SDK | ||
async fn forward_ngrok() -> Result<Forwarder<tunnel::HttpTunnel>, Error> { | ||
let sess = ngrok::Session::builder() | ||
.authtoken_from_env() | ||
.connect() | ||
.await?; | ||
sess | ||
.http_endpoint() | ||
.listen_and_forward(Url::parse("http://192.168.1.2:80")?) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
``` | ||
|
||
::: | ||
Rust Crate Docs: | ||
|
||
- [https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.HttpTunnelBuilder.html#method.listen_and_forward](https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.HttpTunnelBuilder.html#method.listen_and_forward) |
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 |
---|---|---|
@@ -1,5 +1,22 @@ | ||
:::info | ||
```rust | ||
use ngrok::prelude::*; | ||
use ngrok::tunnel; | ||
use ngrok::forwarder::Forwarder; | ||
use url::Url; | ||
|
||
Forwarding to a non-local address is not supported by the Rust SDK | ||
async fn forward_ngrok() -> Result<Forwarder<tunnel::TcpTunnel>, Error> { | ||
let sess = ngrok::Session::builder() | ||
.authtoken_from_env() | ||
.connect() | ||
.await?; | ||
sess | ||
.tcp_endpoint() | ||
.listen_and_forward(Url::parse("tcp://127.0.0.1:8090")?) | ||
.await | ||
.map_err(Into::into) | ||
} | ||
``` | ||
|
||
::: | ||
Rust Crate Docs: | ||
|
||
- [https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.TcpTunnelBuilder.html#method.listen_and_forward](https://docs.rs/ngrok/0.14.0-pre.13/ngrok/config/struct.TcpTunnelBuilder.html#method.listen_and_forward) |