Tutorial6 min read
Deploy a Go HTTP Server with OpenClaw
Go's performance and simplicity make it excellent for production OpenClaw backends. Fast startup, tiny binaries.
Go is excellent for OpenClaw backends — fast startup, tiny binaries, and great performance. Here's how to deploy.
The Go HTTP Server
// main.go
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
type ChatRequest struct {
Message string `json:"message"`
UserID string `json:"userId"`
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}
func chatHandler(w http.ResponseWriter, r *http.Request) {
var req ChatRequest
json.NewDecoder(r.Body).Decode(&req)
// OpenClaw chat call
response := openclaw.Chat(req.Message)
json.NewEncoder(w).Encode(map[string]string{"response": response})
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
http.HandleFunc("/health", healthHandler)
http.HandleFunc("/chat", chatHandler)
fmt.Printf("Server starting on port %s\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
Build and Deploy
CGO_ENABLED=0 go build -o bot .
fly launch --no-deploy
fly secrets set OPENAI_API_KEY=sk-...
fly deploy
Health Check
Configure fly.toml:
[health_checks]
port = 8080
interval = "10s"
timeout = "5s"