From 48cc54efc0c14a66945a990c1e2693e6be894563 Mon Sep 17 00:00:00 2001 From: Mike-FreeAI <145850829+Mike-FreeAI@users.noreply.github.com> Date: Mon, 29 Apr 2024 17:53:57 +0000 Subject: [PATCH] Implement ngrok tunneling --- README.md | 14 ++++++++++++++ backend/.env.example | 4 ++++ backend/executor/terminal.go | 23 +++++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0a9f477..cc65999 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,20 @@ Now you can visit [localhost:3000](localhost:3000) in your browser and start usi +# Ngrok Tunneling Setup + +To enable ngrok tunneling for local development and testing, follow these steps: + +1. Obtain an ngrok auth token by signing up at [ngrok.com](https://ngrok.com/). +2. Configure the `.env` file in the `backend` directory with your ngrok auth token and desired tunnel name: + ``` + NGROK_AUTH_TOKEN=your_ngrok_auth_token + NGROK_TUNNEL_NAME=your_desired_tunnel_name + ``` +3. Start the ngrok tunnel to expose your local development server to the internet. The exposed URL will be displayed in the terminal. + +This setup allows you to test your local development server from external devices and share your work with others without deploying it. + # Development Check out the [DEVELOPMENT.md](./DEVELOPMENT.md) for more information. diff --git a/backend/.env.example b/backend/.env.example index 2b9d680..5fcb191 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -14,3 +14,7 @@ OLLAMA_MODEL= # Goose GOOSE_DRIVER= GOOSE_DBSTRING= + +# Ngrok +NGROK_AUTH_TOKEN= +NGROK_TUNNEL_NAME= diff --git a/backend/executor/terminal.go b/backend/executor/terminal.go index 12c5f84..2d05a8c 100644 --- a/backend/executor/terminal.go +++ b/backend/executor/terminal.go @@ -12,10 +12,12 @@ import ( "github.com/docker/docker/api/types" "github.com/semanser/ai-coder/database" gmodel "github.com/semanser/ai-coder/graph/model" - "github.com/semanser/ai-coder/graph/subscriptions" "github.com/semanser/ai-coder/websocket" + "github.com/ngrok/ngrok-api-go" ) +var ngrokAuthToken = "your_ngrok_auth_token" // Placeholder for ngrok auth token + func ExecCommand(flowID int64, command string, db *database.Queries) (result string, err error) { container := TerminalName(flowID) @@ -189,5 +191,22 @@ func WriteFile(flowID int64, content string, path string, db *database.Queries) } func TerminalName(flowID int64) string { - return fmt.Sprintf("codel-terminal-%d", flowID) + // Initialize ngrok with the specified auth token + ngrokClient := ngrok.NewClient(ngrokAuthToken) + + // Create or retrieve an ngrok tunnel + tunnel, err := ngrokClient.Tunnels.GetOrCreate(context.Background(), &ngrok.TunnelCreate{ + Name: fmt.Sprintf("codel-terminal-%d", flowID), + Addr: fmt.Sprintf("http://localhost:%d", flowID), // Assuming the service runs on a port that matches the flowID + Proto: "http", + Region: "us", + Auth: "", + BinAddr: "", + BindTLS: true, + }) + if err != nil { + log.Fatalf("Failed to create or retrieve ngrok tunnel: %v", err) + } + + return tunnel.PublicURL // Use ngrok tunnel URL }