diff --git a/cmd/substation/playground.go b/cmd/substation/playground.go index 4445c576..e2685356 100644 --- a/cmd/substation/playground.go +++ b/cmd/substation/playground.go @@ -3,11 +3,13 @@ package main import ( "context" _ "embed" + "encoding/base64" "encoding/json" "fmt" "html/template" "log" "net/http" + "net/url" "os" "os/signal" "strings" @@ -42,6 +44,7 @@ func runPlayground(cmd *cobra.Command, args []string) error { mux.HandleFunc("/test", handleTest) mux.HandleFunc("/demo", handleDemo) mux.HandleFunc("/fmt", handleFmt) + mux.HandleFunc("/share", handleShare) // Add this line server := &http.Server{ Addr: ":8080", @@ -67,9 +70,25 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { data := struct { DefaultConfig string DefaultInput string + DefaultOutput string }{ DefaultConfig: "", DefaultInput: "", + DefaultOutput: "", + } + + // Check for shared data in query string + sharedData := r.URL.Query().Get("share") + if sharedData != "" { + decodedData, err := base64.URLEncoding.DecodeString(sharedData) + if err == nil { + parts := strings.SplitN(string(decodedData), "|", 3) + if len(parts) == 3 { + data.DefaultConfig = parts[0] + data.DefaultInput = parts[1] + data.DefaultOutput = parts[2] + } + } } tmpl := template.Must(template.New("index").Parse(indexHTML)) @@ -187,6 +206,38 @@ func handleFmt(w http.ResponseWriter, r *http.Request) { } } +// Add a new handler for sharing +func handleShare(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + var request struct { + Config string `json:"config"` + Input string `json:"input"` + Output string `json:"output"` + } + + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + http.Error(w, "Invalid request", http.StatusBadRequest) + return + } + + // Combine and encode the data + combined := request.Config + "|" + request.Input + "|" + request.Output + encoded := base64.URLEncoding.EncodeToString([]byte(combined)) + + // Create the shareable URL + shareURL := url.URL{ + Path: "/", + RawQuery: "share=" + encoded, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{"url": shareURL.String()}) +} + const indexHTML = ` @@ -433,6 +484,7 @@ const indexHTML = ` +

Run your configuration, test it, or try a demo. @@ -479,7 +531,6 @@ const indexHTML = `