Skip to content

Commit

Permalink
Add Go and Rust forwarding examples (ngrok#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzilladev authored Jun 5, 2024
1 parent 284a64d commit 29f8467
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 18 deletions.
29 changes: 26 additions & 3 deletions examples/go-sdk/http-forward-https.mdx
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)
27 changes: 24 additions & 3 deletions examples/go-sdk/http-nonlocal.mdx
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)
27 changes: 24 additions & 3 deletions examples/go-sdk/tcp-nonlocal.mdx
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)
23 changes: 20 additions & 3 deletions examples/rust-sdk/http-forward-https.mdx
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)
23 changes: 20 additions & 3 deletions examples/rust-sdk/http-nonlocal.mdx
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)
23 changes: 20 additions & 3 deletions examples/rust-sdk/tcp-nonlocal.mdx
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)

0 comments on commit 29f8467

Please sign in to comment.