-
Notifications
You must be signed in to change notification settings - Fork 3
feat: tunnel example #741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
james-rl
wants to merge
2
commits into
main
Choose a base branch
from
james/ex-tunnel
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: tunnel example #741
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,105 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| /** | ||
| --- | ||
| title: Devbox Tunnel (HTTP Server Access) | ||
| slug: devbox-tunnel | ||
| use_case: Create a devbox, start an HTTP server, enable a tunnel, and access the server from the local machine through the tunnel. | ||
| workflow: | ||
| - Create a devbox | ||
| - Start an HTTP server inside the devbox | ||
| - Enable a tunnel for external access | ||
| - Make an HTTP request from the local machine through the tunnel | ||
| - Validate the response | ||
| - Shutdown the devbox | ||
| tags: | ||
| - devbox | ||
| - tunnel | ||
| - networking | ||
| - http | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: yarn tsn -T examples/devbox-tunnel.ts | ||
| test: yarn test:examples | ||
| --- | ||
| */ | ||
|
|
||
| import { RunloopSDK } from '@runloop/api-client'; | ||
| import { wrapRecipe, runAsCli } from './_harness'; | ||
| import type { RecipeContext, RecipeOutput } from './types'; | ||
|
|
||
| const HTTP_SERVER_PORT = 8080; | ||
| const SERVER_STARTUP_DELAY_MS = 2000; | ||
|
|
||
| export async function recipe(ctx: RecipeContext): Promise<RecipeOutput> { | ||
| const { cleanup } = ctx; | ||
|
|
||
| const sdk = new RunloopSDK({ | ||
| bearerToken: process.env['RUNLOOP_API_KEY'], | ||
| }); | ||
|
|
||
| const devbox = await sdk.devbox.create({ | ||
| name: 'devbox-tunnel-example', | ||
| launch_parameters: { | ||
| resource_size_request: 'X_SMALL', | ||
| }, | ||
| }); | ||
| cleanup.add(`devbox:${devbox.id}`, () => sdk.devbox.fromId(devbox.id).shutdown()); | ||
|
|
||
| // Start a simple HTTP server inside the devbox using Python's built-in http.server | ||
| // We use execAsync because the server runs indefinitely until stopped | ||
| const serverExecution = await devbox.cmd.execAsync( | ||
| `python3 -m http.server ${HTTP_SERVER_PORT} --directory /tmp`, | ||
| ); | ||
|
|
||
| // Give the server a moment to start | ||
| await new Promise((resolve) => setTimeout(resolve, SERVER_STARTUP_DELAY_MS)); | ||
|
|
||
| // Enable a tunnel to expose the HTTP server | ||
| // For authenticated tunnels, use auth_mode: 'authenticated' and include the auth_token | ||
| // in your requests via the Authorization header: `Authorization: Bearer ${tunnel.auth_token}` | ||
| const tunnel = await devbox.net.enableTunnel({ auth_mode: 'open' }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be on create |
||
|
|
||
| // Get the tunnel URL for the server port | ||
| const tunnelUrl = await devbox.getTunnelUrl(HTTP_SERVER_PORT); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should happen at the beginning after create |
||
|
|
||
| // Make an HTTP request from the LOCAL MACHINE through the tunnel to the devbox | ||
| // This demonstrates that the tunnel allows external access to the devbox service | ||
| const response = await fetch(tunnelUrl); | ||
| const responseText = await response.text(); | ||
|
|
||
| // Stop the HTTP server | ||
| await serverExecution.kill(); | ||
|
|
||
| return { | ||
| resourcesCreated: [`devbox:${devbox.id}`], | ||
| checks: [ | ||
| { | ||
| name: 'tunnel was created successfully', | ||
| passed: !!tunnel.tunnel_key, | ||
| details: `tunnel_key=${tunnel.tunnel_key}`, | ||
| }, | ||
| { | ||
| name: 'tunnel URL was constructed correctly', | ||
| passed: tunnelUrl.includes(tunnel.tunnel_key) && tunnelUrl.includes(`${HTTP_SERVER_PORT}`), | ||
| details: tunnelUrl, | ||
| }, | ||
| { | ||
| name: 'HTTP request through tunnel succeeded', | ||
| passed: response.ok, | ||
| details: `status=${response.status}`, | ||
| }, | ||
| { | ||
| name: 'response contains directory listing', | ||
| passed: responseText.includes('Directory listing'), | ||
| details: responseText.slice(0, 200), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| export const runDevboxTunnelExample = wrapRecipe({ recipe }); | ||
|
|
||
| if (require.main === module) { | ||
| void runAsCli(runDevboxTunnelExample); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could look for a log? or do a while loop to hit the tunnel