$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-goOpenUI generative UI with Go (net/http) backend. Direct OpenAI API streaming via HTTP.
| 1 | # OpenUI Forge — Go |
| 2 | |
| 3 | Build generative UI apps with a React frontend + Go backend. Streams OpenAI API responses directly via net/http. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui go", "openui golang", "openui go backend" |
| 8 | - "generative ui go", "go streaming ui backend" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended) + React >= 18.3.1 (19+ recommended) (frontend) |
| 13 | - Go >= 1.24 (backend; 1.23 and older are out of security support as of Go 1.26) |
| 14 | - `OPENAI_API_KEY` environment variable set |
| 15 | |
| 16 | ## Quick Start |
| 17 | |
| 18 | 1. Create the React frontend and install OpenUI deps: |
| 19 | ```bash |
| 20 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod |
| 21 | ``` |
| 22 | 2. Generate the system prompt: |
| 23 | ```bash |
| 24 | npx @openuidev/cli generate ./src/lib/library.ts --out backend/system-prompt.txt |
| 25 | ``` |
| 26 | 3. Create the Go backend (see Full Code below) |
| 27 | 4. Run: `go run main.go` on `:8080`, frontend on `:3000` |
| 28 | |
| 29 | ## Full Code |
| 30 | |
| 31 | ### Backend: `backend/go.mod` |
| 32 | |
| 33 | ``` |
| 34 | module openui-backend |
| 35 | |
| 36 | go 1.24 |
| 37 | |
| 38 | require ( |
| 39 | github.com/joho/godotenv v1.5.1 |
| 40 | ) |
| 41 | ``` |
| 42 | |
| 43 | ### Backend: `backend/main.go` |
| 44 | |
| 45 | ```go |
| 46 | package main |
| 47 | |
| 48 | import ( |
| 49 | "bufio" |
| 50 | "bytes" |
| 51 | "encoding/json" |
| 52 | "fmt" |
| 53 | "log" |
| 54 | "net/http" |
| 55 | "os" |
| 56 | |
| 57 | _ "github.com/joho/godotenv/autoload" |
| 58 | ) |
| 59 | |
| 60 | var systemPrompt string |
| 61 | |
| 62 | func init() { |
| 63 | data, err := os.ReadFile("system-prompt.txt") |
| 64 | if err != nil { |
| 65 | log.Fatal("system-prompt.txt not found: ", err) |
| 66 | } |
| 67 | systemPrompt = string(data) |
| 68 | } |
| 69 | |
| 70 | func corsMiddleware(next http.Handler) http.Handler { |
| 71 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 72 | w.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000") |
| 73 | w.Header().Set("Vary", "Origin") |
| 74 | w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS") |
| 75 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
| 76 | if r.Method == "OPTIONS" { |
| 77 | w.WriteHeader(204) |
| 78 | return |
| 79 | } |
| 80 | next.ServeHTTP(w, r) |
| 81 | }) |
| 82 | } |
| 83 | |
| 84 | type Message struct { |
| 85 | Role string `json:"role"` |
| 86 | Content string `json:"content"` |
| 87 | } |
| 88 | |
| 89 | type ChatRequest struct { |
| 90 | Messages []Message `json:"messages"` |
| 91 | } |
| 92 | |
| 93 | func chatHandler(w http.ResponseWriter, r *http.Request) { |
| 94 | if r.Method != "POST" { |
| 95 | http.Error(w, "Method not allowed", 405) |
| 96 | return |
| 97 | } |
| 98 | |
| 99 | var req ChatRequest |
| 100 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 101 | http.Error(w, "Bad request", 400) |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | messages := append([]Message{{Role: "system", Content: systemPrompt}}, req.Messages...) |
| 106 | model := os.Getenv("OPENAI_MODEL") |
| 107 | if model == "" { |
| 108 | model = "gpt-5.5" |
| 109 | } |
| 110 | body, _ := json.Marshal(map[string]interface{}{ |
| 111 | "model": model, "stream": true, "messages": messages, |
| 112 | }) |
| 113 | |
| 114 | apiReq, _ := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewReader(body)) |
| 115 | apiReq.Header.Set("Content-Type", "application/json") |
| 116 | apiReq.Header.Set("Authorization", "Bearer "+os.Getenv("OPENAI_API_KEY")) |
| 117 | |
| 118 | resp, err := http.DefaultClient.Do(apiReq) |
| 119 | if err != nil { |
| 120 | http.Error(w, "OpenAI request failed", 502) |
| 121 | return |
| 122 | } |
| 123 | defer resp.Body.Close() |
| 124 | |
| 125 | w.Header().Set("Content-Type", "text/event-stream") |
| 126 | w.Header().Set("Cache-Control", "no-cache") |
| 127 | w.Header().Set("Connection", "keep-alive") |
| 128 | |
| 129 | flusher, ok := w.(http.Flusher) |
| 130 | if !ok { |
| 131 | http.Error(w, "Streaming not supported", 500) |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | // Forward upstream SSE line-by-line so the client sees tokens as they |
| 136 | // arrive instead of waiting for the whole stream to complete. |
| 137 | scanner := bufio.NewScanner(resp.Body) |
| 138 | scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) |
| 139 | for scanner.Scan() { |
| 140 | line := scanner.Bytes() |
| 141 | if _, err := w.Write(line); err != nil { |
| 142 | return |
| 143 | } |
| 144 | if _, err := w.Write([]byte("\n")); err != nil { |
| 145 | return |
| 146 | } |
| 147 | flusher.Flush() |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func main() { |
| 152 | mux := http.NewServeMux() |
| 153 | mux.HandleFunc("/api/chat", chatHandler) |
| 154 | |
| 155 | fmt.Println("Go backend listening on :8080") |
| 156 | log.Fatal(http.ListenAndServe(":8080", corsMiddleware(mux))) |
| 157 | } |
| 158 | ``` |
| 159 | |
| 160 | ### Frontend: `app/chat/page.tsx` |
| 161 | |
| 162 | ```tsx |
| 163 | "use client"; |
| 164 | import { FullScreen } from "@openuidev/react-ui"; |
| 165 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 166 | import { |
| 167 | openAIAdapter, |
| 168 | openAIMessageFormat, |
| 169 | } from "@openuidev/react-headless"; |
| 170 | |
| 171 | export default function ChatPage() { |
| 172 | return ( |
| 173 | <FullScreen |
| 174 | componentLibrary={openuiChatLibrary} |
| 175 | streamProtocol={openAIAdapter()} |
| 176 | messageFormat={openAIMessageFormat} |
| 177 | apiUrl="http://localhost:8080/api/chat" |
| 178 | /> |
| 179 | ); |
| 180 | } |
| 181 | ``` |
| 182 | |
| 183 | > The Go backend forwards OpenAI's SSE stream line-by-line with a `bufio.Scanner` loop, flushing after each line (no `io.Copy`), so the client sees tokens as they arrive. Pair it with `openAIAdapter()` on the frontend. `openAIReadableStreamAdapter()` is for NDJSON (no `data:` prefix) and will silently produce no output here. |
| 184 | > |
| 185 | > An official OpenAI Go SDK exists (`github.com/openai/openai-go/v3`, ~v3.41.0, requires Go 1.22+) as an alternative to hand-rolling the HTTP call. This skill keeps raw `net/ht |