mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-04-26 15:07:39 +00:00
139 lines
4.3 KiB
Swift
139 lines
4.3 KiB
Swift
import Combine
|
|
import Foundation
|
|
import Logging
|
|
|
|
/// Demo code showing how to use the VibeTunnel server
|
|
enum TunnelServerExample {
|
|
private static let logger = Logger(label: "VibeTunnel.TunnelServerDemo")
|
|
|
|
static func runDemo() async {
|
|
// Get the API key (in production, this should be managed securely)
|
|
// For demo purposes, using a hardcoded key
|
|
let apiKey = "demo-api-key-12345"
|
|
|
|
logger.info("Using API key: [REDACTED]")
|
|
|
|
// Create client
|
|
let client = TunnelClient(apiKey: apiKey)
|
|
|
|
do {
|
|
// Check server health
|
|
let isHealthy = try await client.checkHealth()
|
|
logger.info("Server healthy: \(isHealthy)")
|
|
|
|
// Create a new session
|
|
let session = try await client.createSession(
|
|
workingDirectory: "/tmp",
|
|
shell: "/bin/zsh"
|
|
)
|
|
logger.info("Created session: \(session.sessionId)")
|
|
|
|
// Execute a command
|
|
let response = try await client.executeCommand(
|
|
sessionId: session.sessionId,
|
|
command: "echo 'Hello from VibeTunnel!'"
|
|
)
|
|
logger.info("Command output: \(response.output ?? "none")")
|
|
|
|
// List all sessions
|
|
let sessions = try await client.listSessions()
|
|
logger.info("Active sessions: \(sessions.count)")
|
|
|
|
// Close the session
|
|
try await client.closeSession(id: session.sessionId)
|
|
logger.info("Session closed")
|
|
} catch {
|
|
logger.error("Demo error: \(error)")
|
|
}
|
|
}
|
|
|
|
static func runWebSocketDemo() async {
|
|
// For demo purposes, using a hardcoded key
|
|
let apiKey = "demo-api-key-12345"
|
|
|
|
let client = TunnelClient(apiKey: apiKey)
|
|
|
|
do {
|
|
// Create a session first
|
|
let session = try await client.createSession()
|
|
logger.info("Created session for WebSocket: \(session.sessionId)")
|
|
|
|
// Connect WebSocket
|
|
guard let wsClient = client.connectWebSocket(sessionId: session.sessionId) else {
|
|
logger.error("Failed to create WebSocket client")
|
|
return
|
|
}
|
|
wsClient.connect()
|
|
|
|
// Subscribe to messages
|
|
let cancellable = wsClient.messages.sink { message in
|
|
switch message.type {
|
|
case .output:
|
|
logger.info("Output: \(message.data ?? "")")
|
|
case .error:
|
|
logger.error("Error: \(message.data ?? "")")
|
|
default:
|
|
logger.info("Message: \(message.type) - \(message.data ?? "")")
|
|
}
|
|
}
|
|
|
|
// Send some commands
|
|
try await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
|
|
wsClient.sendCommand("pwd")
|
|
|
|
try await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
|
|
wsClient.sendCommand("ls -la")
|
|
|
|
try await Task.sleep(nanoseconds: 2_000_000_000) // 2 seconds
|
|
|
|
// Disconnect
|
|
wsClient.disconnect()
|
|
cancellable.cancel()
|
|
} catch {
|
|
logger.error("WebSocket demo error: \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - cURL Examples
|
|
|
|
// Here are some example cURL commands to test the server:
|
|
//
|
|
// # Set your API key
|
|
// export API_KEY="your-api-key-here"
|
|
//
|
|
// # Health check (no auth required)
|
|
// curl http://localhost:8080/health
|
|
//
|
|
// # Get server info
|
|
// curl -H "X-API-Key: $API_KEY" http://localhost:8080/info
|
|
//
|
|
// # Create a new session
|
|
// curl -X POST http://localhost:8080/sessions \
|
|
// -H "X-API-Key: $API_KEY" \
|
|
// -H "Content-Type: application/json" \
|
|
// -d '{
|
|
// "workingDirectory": "/tmp",
|
|
// "shell": "/bin/zsh"
|
|
// }'
|
|
//
|
|
// # List all sessions
|
|
// curl -H "X-API-Key: $API_KEY" http://localhost:8080/sessions
|
|
//
|
|
// # Execute a command
|
|
// curl -X POST http://localhost:8080/execute \
|
|
// -H "X-API-Key: $API_KEY" \
|
|
// -H "Content-Type: application/json" \
|
|
// -d '{
|
|
// "sessionId": "your-session-id",
|
|
// "command": "ls -la"
|
|
// }'
|
|
//
|
|
// # Get session info
|
|
// curl -H "X-API-Key: $API_KEY" http://localhost:8080/sessions/your-session-id
|
|
//
|
|
// # Close a session
|
|
// curl -X DELETE -H "X-API-Key: $API_KEY" http://localhost:8080/sessions/your-session-id
|
|
//
|
|
// # WebSocket connection (using websocat tool)
|
|
// websocat -H "X-API-Key: $API_KEY" ws://localhost:8080/ws/terminal
|