Skip to content

Commit

Permalink
fix: studio handle clicking on "listening on ..." message (#948)
Browse files Browse the repository at this point in the history
We now output

```
Listening on http://localhost:3333
Opening URL in your browser:  https://app.speakeasy.com/org/speakeasy-self/speakeasy-self/studio/3333#stu-e6e482886b6a4e913a183c61fed1d248
```

They could click on the localhost one so we redirect in that case
  • Loading branch information
mfbx9da4 committed Sep 17, 2024
1 parent 4e36f69 commit 82e2ed5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/studio/launchStudio.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func LaunchStudio(ctx context.Context, workflow *run.Workflow) error {
}

mux := http.NewServeMux()
mux.HandleFunc("/", handler(handlers.root))
mux.HandleFunc("/health", handler(handlers.health))

mux.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -98,14 +99,14 @@ func LaunchStudio(ctx context.Context, workflow *run.Workflow) error {

serverURL := auth.GetWorkspaceBaseURL(ctx)

url := fmt.Sprintf("%s/studio/%d#%s", serverURL, port, secret)
handlers.StudioURL = fmt.Sprintf("%s/studio/%d#%s", serverURL, port, secret)

listeningMessage := fmt.Sprintf("Listening on http://localhost:%d\n", port)

if err := browser.OpenURL(url); err != nil {
fmt.Println(listeningMessage+"Please open the following URL in your browser:\n\t", url)
if err := browser.OpenURL(handlers.StudioURL); err != nil {
fmt.Println(listeningMessage+"Please open the following URL in your browser: ", handlers.StudioURL)
} else {
fmt.Println(listeningMessage+"Opening URL in your browser:\n\t", url)
fmt.Println(listeningMessage+"Opening URL in your browser: ", handlers.StudioURL)
}

// After ten seconds, if the health check hasn't been seen then kill the server
Expand Down Expand Up @@ -143,7 +144,7 @@ func corsMiddleware(next http.Handler) http.Handler {

func authMiddleware(secret string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Secret-Key") != secret {
if r.Header.Get("X-Secret-Key") != secret && r.URL.Path != "/" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
Expand Down
7 changes: 7 additions & 0 deletions internal/studio/studioHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type StudioHandlers struct {
SourceID string
OverlayPath string
Ctx context.Context
StudioURL string

mutex sync.Mutex
mutexCondition *sync.Cond
Expand Down Expand Up @@ -206,6 +207,12 @@ func (h *StudioHandlers) health(ctx context.Context, w http.ResponseWriter, r *h
return nil
}

func (h *StudioHandlers) root(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
// In case the user navigates to the root of the studio, redirect them to the studio URL
http.Redirect(w, r, h.StudioURL, http.StatusSeeOther)
return nil
}

func (h *StudioHandlers) updateSource(r *http.Request) error {
var err error

Expand Down

0 comments on commit 82e2ed5

Please sign in to comment.