diff --git a/helloworld/helloworld.go b/helloworld/helloworld.go index 2c994f0f5..3d6107b58 100644 --- a/helloworld/helloworld.go +++ b/helloworld/helloworld.go @@ -22,6 +22,7 @@ import ( "os" "os/exec" "os/signal" + "path/filepath" "sort" "strconv" "strings" @@ -35,6 +36,9 @@ func Curl(url string) (string, error) { return string(res), err } +const bucketDir = "/mnt/bucket" +const bucketServePath = "/bucket" + var GlobalDebug = (os.Getenv("DEBUG") != "") var envs = []string{} var msg = "" @@ -56,6 +60,7 @@ func Debug(doit bool, format string, args ...interface{}) { // Just print an cool essage to the Writer that's passed in func PrintMessage(w io.Writer, showAll bool) { + // http://patorjk.com/software/taag/#p=display&f=Graceful&t=Code%0AEngine fmt.Fprintf(w, "%s:\n", msg) fmt.Fprintln(w, `. ___ __ ____ ____`) fmt.Fprintln(w, `./ __)/ \( \( __)`) @@ -84,6 +89,16 @@ func PrintMessage(w io.Writer, showAll bool) { } fmt.Fprintf(w, "%s\n", env) } + + if IsBucketMounted() { + fmt.Fprintf(w, "--------------\n") + fmt.Fprintf(w, "\n") + fmt.Fprintf(w, "Deteceted a mounted COS bucket under '%s'.\n", bucketDir) + fmt.Fprintf(w, "Feel free to explore!\n") + if !IsJob() { + fmt.Fprintf(w, "https://%s%s\n", AppURL(), bucketServePath) + } + } } // This func will handle all incoming HTTP requests @@ -145,13 +160,82 @@ func HandleHTTP(w http.ResponseWriter, r *http.Request) { // But if there is a body, echo it back to the client. if len(body) == 0 { w.Header().Add("Content-Type", "text/plain") - // http://patorjk.com/software/taag/#p=display&f=Graceful&t=Code%0AEngine PrintMessage(w, showAll) } else { fmt.Fprintf(w, string(body)+"\n") } } +func IsJob() bool { + // If we're being run as a Batch Jobthen the JOB_INDEX env var will be set. + return os.Getenv("JOB_INDEX") != "" +} + +func AppURL() string { + return fmt.Sprintf("%s.%s.%s", os.Getenv("CE_APP"), os.Getenv("CE_SUBDOMAIN"), os.Getenv("CE_DOMAIN")) +} + +func IsBucketMounted() bool { + info, err := os.Stat(bucketDir) + return err == nil && info.IsDir() +} + +func NewBucketRouter() http.Handler { + mux := http.NewServeMux() + + mux.HandleFunc("/", BucketLandingPageHandler) + mux.HandleFunc("/upload", FileUploadHandler) + + fileServer := http.FileServer(http.Dir(bucketDir)) + mux.Handle("/files/", http.StripPrefix("/files/", fileServer)) + + return http.StripPrefix(bucketServePath, mux) +} + +func BucketLandingPageHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprintf(w, ` +
Use this form to upload files to the bucket mounted at '%s'.
+ +