From 637b0149319308f531d233f271de88cf1e4d6057 Mon Sep 17 00:00:00 2001 From: "Jonathan Leibiusky @xetorthio" Date: Thu, 18 May 2017 13:16:34 -0300 Subject: [PATCH] Allow external stacks. Also default to `stack.yml` if file was not specified. --- handlers/new_session.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/handlers/new_session.go b/handlers/new_session.go index fc344de..43f6e99 100644 --- a/handlers/new_session.go +++ b/handlers/new_session.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "net/http" + "path" + "strings" "github.com/play-with-docker/play-with-docker/config" "github.com/play-with-docker/play-with-docker/services" @@ -27,6 +29,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) { stack := req.Form.Get("stack") if stack != "" { + stack = formatStack(stack) if ok, err := stackExists(stack); err != nil { log.Printf("Error retrieving stack: %s", err) rw.WriteHeader(http.StatusInternalServerError) @@ -61,13 +64,24 @@ func NewSession(rw http.ResponseWriter, req *http.Request) { } } +func formatStack(stack string) string { + if !strings.HasSuffix(stack, ".yml") { + // If it doesn't end with ".yml", assume it hasn't been specified, then default to "stack.yml" + stack = path.Join(stack, "stack.yml") + } + if strings.HasPrefix(stack, "/") { + // The host is anonymous, then use our own stack repo. + stack = fmt.Sprintf("%s%s", "https://raw.githubusercontent.com/play-with-docker/stacks/master", stack) + } + return stack +} + func stackExists(stack string) (bool, error) { - resp, err := http.Head("https://raw.githubusercontent.com/play-with-docker/stacks/master" + stack) + resp, err := http.Head(stack) if err != nil { return false, err } defer resp.Body.Close() return resp.StatusCode == 200, nil - }